mirror of
https://gitlab.com/hashborgir/d2tools.git
synced 2025-10-14 00:44:23 -05:00
Begin Refactor
This commit is contained in:
197
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Foreignkeys.php
vendored
Normal file
197
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Foreignkeys.php
vendored
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace RedUNIT\Sqlite;
|
||||
|
||||
use RedUNIT\Sqlite as Sqlite;
|
||||
use RedBeanPHP\Facade as R;
|
||||
|
||||
/**
|
||||
* Foreignkeys
|
||||
*
|
||||
* Tests creation and validity of foreign keys,
|
||||
* foreign key constraints and indexes in SQLite.
|
||||
* Also tests whether the correct contraint action has been selected.
|
||||
*
|
||||
* @file RedUNIT/Sqlite/Foreignkeys.php
|
||||
* @desc Tests the creation of foreign keys.
|
||||
* @author Gabor de Mooij and the RedBeanPHP Community
|
||||
* @license New BSD/GPLv2
|
||||
*
|
||||
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
|
||||
* This source file is subject to the New BSD/GPLv2 License that is bundled
|
||||
* with this source code in the file license.txt.
|
||||
*/
|
||||
class Foreignkeys extends Sqlite
|
||||
{
|
||||
/**
|
||||
* addIndex should not trigger exception...
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIndexException()
|
||||
{
|
||||
R::nuke();
|
||||
$book = R::dispense( 'book' );
|
||||
$book->title = 'a';
|
||||
R::store( $book );
|
||||
try {
|
||||
R::getWriter()->addIndex( 'book' , '\'', 'title' );
|
||||
pass();
|
||||
} catch( \Exception $e ) {
|
||||
fail();
|
||||
}
|
||||
R::getWriter()->addIndex( 'book' , '\'', 'title' );
|
||||
pass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test foreign keys with SQLite.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testForeignKeysWithSQLite()
|
||||
{
|
||||
$book = R::dispense( 'book' );
|
||||
$page = R::dispense( 'page' );
|
||||
$cover = R::dispense( 'cover' );
|
||||
list( $g1, $g2 ) = R::dispense( 'genre', 2 );
|
||||
$g1->name = '1';
|
||||
$g2->name = '2';
|
||||
$book->ownPage = array( $page );
|
||||
$book->cover = $cover;
|
||||
$book->sharedGenre = array( $g1, $g2 );
|
||||
R::store( $book );
|
||||
$fkbook = R::getAll( 'pragma foreign_key_list(book)' );
|
||||
$fkgenre = R::getAll( 'pragma foreign_key_list(book_genre)' );
|
||||
$fkpage = R::getAll( 'pragma foreign_key_list(page)' );
|
||||
asrt( $fkpage[0]['from'], 'book_id' );
|
||||
asrt( $fkpage[0]['to'], 'id' );
|
||||
asrt( $fkpage[0]['table'], 'book' );
|
||||
asrt( count( $fkgenre ), 2 );
|
||||
if ( $fkgenre[0]['from'] == 'book' ) {
|
||||
asrt( $fkgenre[0]['to'], 'id' );
|
||||
asrt( $fkgenre[0]['table'], 'book' );
|
||||
}
|
||||
if ( $fkgenre[0]['from'] == 'genre' ) {
|
||||
asrt( $fkgenre[0]['to'], 'id' );
|
||||
asrt( $fkgenre[0]['table'], 'genre' );
|
||||
}
|
||||
asrt( $fkbook[0]['from'], 'cover_id' );
|
||||
asrt( $fkbook[0]['to'], 'id' );
|
||||
asrt( $fkbook[0]['table'], 'cover' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Constrain test for SQLite Writer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testConstrain()
|
||||
{
|
||||
R::nuke();
|
||||
$sql = 'CREATE TABLE book ( id INTEGER PRIMARY KEY AUTOINCREMENT ) ';
|
||||
R::exec( $sql );
|
||||
$sql = 'CREATE TABLE page ( id INTEGER PRIMARY KEY AUTOINCREMENT ) ';
|
||||
R::exec( $sql );
|
||||
$sql = 'CREATE TABLE book_page (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
book_id INTEGER,
|
||||
page_id INTEGER
|
||||
) ';
|
||||
R::exec( $sql );
|
||||
$sql = 'PRAGMA foreign_key_list("book_page")';
|
||||
$fkList = R::getAll( $sql );
|
||||
asrt( count( $fkList), 0 );
|
||||
$writer = R::getWriter();
|
||||
$writer->addFK( 'book_page', 'book', 'book_id', 'id', TRUE );
|
||||
$writer->addFK( 'book_page', 'page', 'page_id', 'id', TRUE );
|
||||
$sql = 'PRAGMA foreign_key_list("book_page")';
|
||||
$fkList = R::getAll( $sql );
|
||||
asrt( count( $fkList), 2 );
|
||||
$writer->addFK( 'book_page', 'book', 'book_id', 'id', TRUE );
|
||||
$writer->addFK( 'book_page', 'page', 'page_id', 'id', TRUE );
|
||||
$sql = 'PRAGMA foreign_key_list("book_page")';
|
||||
$fkList = R::getAll( $sql );
|
||||
asrt( count( $fkList), 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding foreign keys.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddingForeignKeys()
|
||||
{
|
||||
R::nuke();
|
||||
|
||||
$sql = 'CREATE TABLE book ( id INTEGER PRIMARY KEY AUTOINCREMENT ) ';
|
||||
R::exec( $sql );
|
||||
$sql = 'CREATE TABLE page ( id INTEGER PRIMARY KEY AUTOINCREMENT, book_id INTEGER ) ';
|
||||
R::exec( $sql );
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 0 );
|
||||
$writer = R::getWriter();
|
||||
$writer->addFK('page', 'book', 'book_id', 'id', TRUE);
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 1 );
|
||||
$writer->addFK('page', 'book', 'book_id', 'id', TRUE);
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 1 );
|
||||
$writer->addFK('page', 'book', 'book_id', 'id', FALSE);
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 1 );
|
||||
R::nuke();
|
||||
$sql = 'CREATE TABLE book ( id INTEGER PRIMARY KEY AUTOINCREMENT ) ';
|
||||
R::exec( $sql );
|
||||
$sql = 'CREATE TABLE page ( id INTEGER PRIMARY KEY AUTOINCREMENT, book_id INTEGER ) ';
|
||||
R::exec( $sql );
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 0 );
|
||||
$writer = R::getWriter();
|
||||
$writer->addFK('page', 'book', 'book_id', 'id', FALSE);
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 1 );
|
||||
$writer->addFK('page', 'book', 'book_id', 'id', TRUE);
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 1 );
|
||||
$writer->addFK('page', 'book', 'book_id', 'id', FALSE);
|
||||
asrt( count( R::getAll(' PRAGMA foreign_key_list("page") ') ), 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether we can manually create indexes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddingIndex()
|
||||
{
|
||||
R::nuke();
|
||||
$sql = 'CREATE TABLE song (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
album_id INTEGER,
|
||||
category TEXT
|
||||
) ';
|
||||
R::exec( $sql );
|
||||
$writer = R::getWriter();
|
||||
$indexes = R::getAll('PRAGMA index_list("song") ');
|
||||
asrt( count( $indexes ), 0 );
|
||||
$writer->addIndex( 'song', 'index1', 'album_id' );
|
||||
$indexes = R::getAll('PRAGMA index_list("song") ');
|
||||
asrt( count( $indexes ), 1 );
|
||||
$writer->addIndex( 'song', 'index1', 'album_id' );
|
||||
$indexes = R::getAll('PRAGMA index_list("song") ');
|
||||
asrt( count( $indexes ), 1 );
|
||||
$writer->addIndex( 'song', 'index2', 'category' );
|
||||
$indexes = R::getAll('PRAGMA index_list("song") ');
|
||||
asrt( count( $indexes ), 2 );
|
||||
try {
|
||||
$writer->addIndex( 'song', 'index1', 'nonexistant' );
|
||||
pass();
|
||||
} catch ( \Exception $ex ) {
|
||||
fail();
|
||||
}
|
||||
$indexes = R::getAll('PRAGMA index_list("song") ');
|
||||
asrt( count( $indexes ), 2 );
|
||||
try {
|
||||
$writer->addIndex( 'nonexistant', 'index1', 'nonexistant' );
|
||||
pass();
|
||||
} catch ( \Exception $ex ) {
|
||||
fail();
|
||||
}
|
||||
$indexes = R::getAll('PRAGMA index_list("song") ');
|
||||
asrt( count( $indexes ), 2 );
|
||||
}
|
||||
}
|
60
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Parambind.php
vendored
Normal file
60
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Parambind.php
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace RedUNIT\Sqlite;
|
||||
|
||||
use RedUNIT\Sqlite as Sqlite;
|
||||
use RedBeanPHP\Facade as R;
|
||||
|
||||
/**
|
||||
* Parambind
|
||||
*
|
||||
* Tests the parameter binding functionality in RedBeanPHP.
|
||||
* These test scenarios include for instance: NULL handling,
|
||||
* binding parameters in LIMIT clauses and so on.
|
||||
*
|
||||
* @file RedUNIT/Sqlite/Parambind.php
|
||||
* @desc Tests\PDO parameter binding.
|
||||
* @author Gabor de Mooij and the RedBeanPHP Community
|
||||
* @license New BSD/GPLv2
|
||||
*
|
||||
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
|
||||
* This source file is subject to the New BSD/GPLv2 License that is bundled
|
||||
* with this source code in the file license.txt.
|
||||
*/
|
||||
class Parambind extends Sqlite
|
||||
{
|
||||
/**
|
||||
* Test parameter binding with SQLite.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParamBindWithSQLite()
|
||||
{
|
||||
$toolbox = R::getToolBox();
|
||||
$adapter = $toolbox->getDatabaseAdapter();
|
||||
$writer = $toolbox->getWriter();
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$pdo = $adapter->getDatabase();
|
||||
asrt( (int) $adapter->getCell( "SELECT 123" ), 123 );
|
||||
asrt( (int) $adapter->getCell( "SELECT ?", array( "987" ) ), 987 );
|
||||
asrt( (int) $adapter->getCell( "SELECT ?+?", array( "987", "2" ) ), 989 );
|
||||
asrt( (int) $adapter->getCell(
|
||||
"SELECT :numberOne+:numberTwo",
|
||||
array(
|
||||
":numberOne" => 42,
|
||||
":numberTwo" => 50 )
|
||||
),
|
||||
92
|
||||
);
|
||||
$pair = $adapter->getAssoc( "SELECT 'thekey','thevalue' " );
|
||||
asrt( is_array( $pair ), TRUE );
|
||||
asrt( count( $pair ), 1 );
|
||||
asrt( isset( $pair["thekey"] ), TRUE );
|
||||
asrt( $pair["thekey"], "thevalue" );
|
||||
testpack( 'Test whether we can properly bind and receive NULL values' );
|
||||
asrt( $adapter->getCell( 'SELECT :nil ', array( ':nil' => 'NULL' ) ), 'NULL' );
|
||||
asrt( $adapter->getCell( 'SELECT :nil ', array( ':nil' => NULL ) ), NULL );
|
||||
asrt( $adapter->getCell( 'SELECT ? ', array( 'NULL' ) ), 'NULL' );
|
||||
asrt( $adapter->getCell( 'SELECT ? ', array( NULL ) ), NULL );
|
||||
}
|
||||
}
|
63
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Rebuild.php
vendored
Normal file
63
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Rebuild.php
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace RedUNIT\Sqlite;
|
||||
|
||||
use RedUNIT\Sqlite as Sqlite;
|
||||
use RedBeanPHP\Facade as R;
|
||||
|
||||
/**
|
||||
* Rebuild
|
||||
*
|
||||
* SQLite cannot ALTER tables like other databases can.
|
||||
* To implement fluid mode in RedBeanPHP we have to
|
||||
* rebuild the entire table whenever we add or remove a column.
|
||||
* This test class tests whether rebuilding tables works properly,
|
||||
* i.e. we get the same table plus/minus some column...
|
||||
*
|
||||
* @file RedUNIT/Sqlite/Rebuild.php
|
||||
* @desc Test rebuilding of tables for SQLite
|
||||
* @author Gabor de Mooij and the RedBeanPHP Community
|
||||
* @license New BSD/GPLv2
|
||||
*
|
||||
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
|
||||
* This source file is subject to the New BSD/GPLv2 License that is bundled
|
||||
* with this source code in the file license.txt.
|
||||
*/
|
||||
class Rebuild extends Sqlite
|
||||
{
|
||||
/**
|
||||
* Test SQLite table rebuilding.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRebuilder()
|
||||
{
|
||||
$toolbox = R::getToolBox();
|
||||
$adapter = $toolbox->getDatabaseAdapter();
|
||||
$writer = $toolbox->getWriter();
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$pdo = $adapter->getDatabase();
|
||||
$book = R::dispense( 'book' );
|
||||
$page = R::dispense( 'page' );
|
||||
$book->xownPage[] = $page;
|
||||
$id = R::store( $book );
|
||||
$book = R::load( 'book', $id );
|
||||
asrt( count( $book->xownPage ), 1 );
|
||||
asrt( (int) R::getCell( 'SELECT COUNT(*) FROM page' ), 1 );
|
||||
R::trash( $book );
|
||||
asrt( (int) R::getCell( 'SELECT COUNT(*) FROM page' ), 0 );
|
||||
$book = R::dispense( 'book' );
|
||||
$page = R::dispense( 'page' );
|
||||
$book->xownPage[] = $page;
|
||||
$id = R::store( $book );
|
||||
$book = R::load( 'book', $id );
|
||||
asrt( count( $book->xownPage ), 1 );
|
||||
asrt( (int) R::getCell( 'SELECT COUNT(*) FROM page' ), 1 );
|
||||
$book->added = 2;
|
||||
R::store( $book );
|
||||
$book->added = 'added';
|
||||
R::store( $book );
|
||||
R::trash( $book );
|
||||
asrt( (int) R::getCell( 'SELECT COUNT(*) FROM page' ), 0 );
|
||||
}
|
||||
}
|
134
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Setget.php
vendored
Normal file
134
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Setget.php
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace RedUNIT\Sqlite;
|
||||
|
||||
use RedUNIT\Sqlite as Sqlite;
|
||||
use RedBeanPHP\Facade as R;
|
||||
|
||||
/**
|
||||
* Setget
|
||||
*
|
||||
* This class has been designed to test set/get operations
|
||||
* for a specific Query Writer / Adapter. Since RedBeanPHP
|
||||
* creates columns based on values it's essential that you
|
||||
* get back the 'same' value as you put in - or - if that's
|
||||
* not the case, that there are at least very clear rules
|
||||
* about what to expect. Examples of possible issues tested in
|
||||
* this class include:
|
||||
*
|
||||
* - Test whether booleans are returned correctly (they will become integers)
|
||||
* - Test whether large numbers are preserved
|
||||
* - Test whether floating point numbers are preserved
|
||||
* - Test whether date/time values are preserved
|
||||
* and so on...
|
||||
*
|
||||
* @file RedUNIT/Sqlite/Setget.php
|
||||
* @desc Tests whether values are stored correctly.
|
||||
* @author Gabor de Mooij and the RedBeanPHP Community
|
||||
* @license New BSD/GPLv2
|
||||
*
|
||||
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
|
||||
* This source file is subject to the New BSD/GPLv2 License that is bundled
|
||||
* with this source code in the file license.txt.
|
||||
*/
|
||||
class Setget extends Sqlite
|
||||
{
|
||||
/**
|
||||
* Test whether we can store DateTime objects and get them back
|
||||
* as 'date-time' strings representing the same date and time.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDateObject()
|
||||
{
|
||||
$dt = new \DateTime();
|
||||
$dt->setTimeZone( new \DateTimeZone( 'Europe/Amsterdam' ) );
|
||||
$dt->setDate( 1981, 5, 1 );
|
||||
$dt->setTime( 3, 13, 13 );
|
||||
asrt( setget( $dt ), '1981-05-01 03:13:13' );
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->dt = $dt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test numbers.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNumbers()
|
||||
{
|
||||
asrt( setget( "-1" ), "-1" );
|
||||
asrt( setget( -1 ), "-1" );
|
||||
asrt( setget( "-0.25" ), "-0.25" );
|
||||
asrt( setget( -0.25 ), "-0.25" );
|
||||
asrt( setget( "1.0" ), "1" );
|
||||
asrt( setget( 1.0 ), "1" );
|
||||
asrt( setget( "0.12345678" ), "0.12345678" );
|
||||
asrt( setget( 0.12345678 ), "0.12345678" );
|
||||
asrt( setget( "-0.12345678" ), "-0.12345678" );
|
||||
asrt( setget( -0.12345678 ), "-0.12345678" );
|
||||
asrt( setget( "2147483647" ), "2147483647" );
|
||||
asrt( setget( 2147483647 ), "2147483647" );
|
||||
asrt( setget( -2147483647 ), "-2147483647" );
|
||||
asrt( setget( "-2147483647" ), "-2147483647" );
|
||||
asrt( setget( "2147483648" ), "2147483648" );
|
||||
asrt( setget( "-2147483648" ), "-2147483648" );
|
||||
asrt( setget( "199936710040730" ), "199936710040730" );
|
||||
asrt( setget( "-199936710040730" ), "-199936710040730" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test dates.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDates()
|
||||
{
|
||||
asrt( setget( "2010-10-11" ), "2010-10-11" );
|
||||
asrt( setget( "2010-10-11 12:10" ), "2010-10-11 12:10" );
|
||||
asrt( setget( "2010-10-11 12:10:11" ), "2010-10-11 12:10:11" );
|
||||
asrt( setget( "x2010-10-11 12:10:11" ), "x2010-10-11 12:10:11" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test strings.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStrings()
|
||||
{
|
||||
asrt( setget( "a" ), "a" );
|
||||
asrt( setget( "." ), "." );
|
||||
asrt( setget( "\"" ), "\"" );
|
||||
asrt( setget( "just some text" ), "just some text" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test booleans.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBool()
|
||||
{
|
||||
asrt( setget( TRUE ), "1" );
|
||||
asrt( setget( FALSE ), "0" );
|
||||
asrt( setget( "TRUE" ), "TRUE" );
|
||||
asrt( setget( "FALSE" ), "FALSE" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test NULL.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNull()
|
||||
{
|
||||
asrt( setget( "NULL" ), "NULL" );
|
||||
asrt( setget( "NULL" ), "NULL" );
|
||||
asrt( setget( NULL ), NULL );
|
||||
asrt( ( setget( 0 ) == 0 ), TRUE );
|
||||
asrt( ( setget( 1 ) == 1 ), TRUE );
|
||||
asrt( ( setget( TRUE ) == TRUE ), TRUE );
|
||||
asrt( ( setget( FALSE ) == FALSE ), TRUE );
|
||||
}
|
||||
}
|
406
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Writer.php
vendored
Normal file
406
vendor/gabordemooij/redbean/testing/RedUNIT/Sqlite/Writer.php
vendored
Normal file
@@ -0,0 +1,406 @@
|
||||
<?php
|
||||
|
||||
namespace RedUNIT\Sqlite;
|
||||
|
||||
use RedUNIT\Sqlite as Sqlite;
|
||||
use RedBeanPHP\Facade as R;
|
||||
use RedBeanPHP\AssociationManager as AssociationManager;
|
||||
use RedBeanPHP\QueryWriter\SQLiteT as SQLiteT;
|
||||
use RedBeanPHP\RedException\SQL as SQL;
|
||||
use RedBeanPHP\RedException as RedException;
|
||||
use RedBeanPHP\QueryWriter\AQueryWriter;
|
||||
/**
|
||||
* Writer
|
||||
*
|
||||
* Tests for SQLite Query Writer.
|
||||
* This test class contains Query Writer specific tests.
|
||||
* Use this class to add tests to test Query Writer specific
|
||||
* behaviours, quirks and issues.
|
||||
*
|
||||
* @file RedUNIT/Sqlite/Writer.php
|
||||
* @desc Tests writer specific functions.
|
||||
* @author Gabor de Mooij and the RedBeanPHP Community
|
||||
* @license New BSD/GPLv2
|
||||
*
|
||||
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
|
||||
* This source file is subject to the New BSD/GPLv2 License that is bundled
|
||||
* with this source code in the file license.txt.
|
||||
*/
|
||||
class Writer extends Sqlite
|
||||
{
|
||||
/**
|
||||
* Test whether SQLite QueryWriter safely filters FOR-UPDATE snippets.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWriterShouldFilterSelectForUpdate()
|
||||
{
|
||||
R::nuke();
|
||||
$id = R::store( R::dispense('bean') );
|
||||
R::debug( TRUE, 1 );
|
||||
$bean = R::findForUpdate('bean', 'id > 0');
|
||||
$logs = R::getDatabaseAdapter()->getDatabase()->getLogger()->grep( AQueryWriter::C_SELECT_SNIPPET_FOR_UPDATE );
|
||||
asrt( count($logs), 0 ); //if no cache clear, then would have been 2
|
||||
$bean = R::loadForUpdate('bean', $id);
|
||||
$logs = R::getDatabaseAdapter()->getDatabase()->getLogger()->grep( AQueryWriter::C_SELECT_SNIPPET_FOR_UPDATE );
|
||||
asrt( count($logs), 0 ); //if no cache clear, then would have been 2
|
||||
R::getWriter()->setUseCache( FALSE );
|
||||
R::debug( FALSE );
|
||||
R::nuke();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether optimizations do not have effect on Writer query outcomes.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWriterSpeedUp()
|
||||
{
|
||||
R::nuke();
|
||||
$id = R::store( R::dispense( 'book' ) );
|
||||
$writer = R::getWriter();
|
||||
$count1 = $writer->queryRecordCount( 'book', array( 'id' => $id ), ' id = :id ', array( ':id' => $id ) );
|
||||
$count2 = $writer->queryRecordCount( 'book', array( ), ' id = :id ', array( ':id' => $id ) );
|
||||
$count3 = $writer->queryRecordCount( 'book', NULL, ' id = :id ', array( ':id' => $id ) );
|
||||
$count4 = $writer->queryRecordCount( 'book', array( 'id' => $id ) );
|
||||
asrt( $count1, $count2 );
|
||||
asrt( $count2, $count3 );
|
||||
asrt( $count3, $count4 );
|
||||
R::nuke();
|
||||
$books = R::dispenseAll( 'book*4' );
|
||||
$ids = R::storeAll( $books[0] );
|
||||
$writer->deleteRecord( 'book', array( 'id' => $ids[0] ) );
|
||||
$writer->deleteRecord( 'book', array( 'id' => $ids[1] ), ' id = :id ', array( ':id' => $ids[1] ) );
|
||||
$writer->deleteRecord( 'book', NULL, ' id = :id ', array( ':id' => $ids[2] ) );
|
||||
$writer->deleteRecord( 'book', array(), ' id = :id ', array( ':id' => $ids[3] ) );
|
||||
asrt( R::count( 'book' ), 0 );
|
||||
R::nuke();
|
||||
$id = R::store( R::dispense( 'book' ) );
|
||||
$record = $writer->queryRecord( 'book', array( 'id' => $id ) );
|
||||
asrt( is_array( $record ), TRUE );
|
||||
asrt( is_array( $record[0] ), TRUE );
|
||||
asrt( isset( $record[0]['id'] ), TRUE );
|
||||
asrt( (int) $record[0]['id'], $id );
|
||||
$record = $writer->queryRecord( 'book', array( 'id' => $id ), ' id = :id ', array( ':id' => $id ) );
|
||||
asrt( is_array( $record ), TRUE );
|
||||
asrt( is_array( $record[0] ), TRUE );
|
||||
asrt( isset( $record[0]['id'] ), TRUE );
|
||||
asrt( (int) $record[0]['id'], $id );
|
||||
$record = $writer->queryRecord( 'book', NULL, ' id = :id ', array( ':id' => $id ) );
|
||||
asrt( is_array( $record ), TRUE );
|
||||
asrt( is_array( $record[0] ), TRUE );
|
||||
asrt( isset( $record[0]['id'] ), TRUE );
|
||||
asrt( (int) $record[0]['id'], $id );
|
||||
$record = $writer->queryRecord( 'book', array(), ' id = :id ', array( ':id' => $id ) );
|
||||
asrt( is_array( $record ), TRUE );
|
||||
asrt( is_array( $record[0] ), TRUE );
|
||||
asrt( isset( $record[0]['id'] ), TRUE );
|
||||
asrt( (int) $record[0]['id'], $id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wheter we can write a deletion query
|
||||
* for SQLite using NO conditions but only an
|
||||
* additional SQL snippet.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteDeleteQuery()
|
||||
{
|
||||
$queryWriter = R::getWriter();
|
||||
asrt( ( $queryWriter instanceof SQLiteT ), TRUE );
|
||||
R::nuke();
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->name = 'a';
|
||||
$id = R::store( $bean );
|
||||
asrt( R::count( 'bean' ), 1 );
|
||||
$queryWriter->deleteRecord( 'bean', array(), $addSql = ' id = :id ', $bindings = array( ':id' => $id ) );
|
||||
asrt( R::count( 'bean' ), 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wheter we can write a counting query
|
||||
* for SQLite using conditions and an additional SQL snippet.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteCountQuery()
|
||||
{
|
||||
$queryWriter = R::getWriter();
|
||||
asrt( ( $queryWriter instanceof SQLiteT ), TRUE );
|
||||
R::nuke();
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->name = 'a';
|
||||
R::store( $bean );
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->name = 'b';
|
||||
R::store( $bean );
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->name = 'b';
|
||||
R::store( $bean );
|
||||
$count = $queryWriter->queryRecordCount( 'bean', array( 'name' => 'b' ), $addSql = ' id > :id ', $bindings = array( ':id' => 0 ) );
|
||||
asrt( $count, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether we can write a SQLite join and
|
||||
* whether the correct exception is thrown in case
|
||||
* of an invalid join.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWriteJoinSnippets()
|
||||
{
|
||||
$queryWriter = R::getWriter();
|
||||
asrt( ( $queryWriter instanceof SQLiteT ), TRUE );
|
||||
$snippet = $queryWriter->writeJoin( 'book', 'page' ); //default must be LEFT
|
||||
asrt( is_string( $snippet ), TRUE );
|
||||
asrt( ( strlen( $snippet ) > 0 ), TRUE );
|
||||
asrt( ' LEFT JOIN `page` ON `page`.id = `book`.page_id ', $snippet );
|
||||
$snippet = $queryWriter->writeJoin( 'book', 'page', 'LEFT' );
|
||||
asrt( is_string( $snippet ), TRUE );
|
||||
asrt( ( strlen( $snippet ) > 0 ), TRUE );
|
||||
asrt( ' LEFT JOIN `page` ON `page`.id = `book`.page_id ', $snippet );
|
||||
$snippet = $queryWriter->writeJoin( 'book', 'page', 'RIGHT' );
|
||||
asrt( is_string( $snippet ), TRUE );
|
||||
asrt( ( strlen( $snippet ) > 0 ), TRUE );
|
||||
asrt( ' RIGHT JOIN `page` ON `page`.id = `book`.page_id ', $snippet );
|
||||
$snippet = $queryWriter->writeJoin( 'book', 'page', 'INNER' );
|
||||
asrt( ' INNER JOIN `page` ON `page`.id = `book`.page_id ', $snippet );
|
||||
$exception = NULL;
|
||||
try {
|
||||
$snippet = $queryWriter->writeJoin( 'book', 'page', 'MIDDLE' );
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
$exception = $e;
|
||||
}
|
||||
asrt( ( $exception instanceof RedException ), TRUE );
|
||||
$errorMessage = $exception->getMessage();
|
||||
asrt( is_string( $errorMessage ), TRUE );
|
||||
asrt( ( strlen( $errorMessage ) > 0 ), TRUE );
|
||||
asrt( $errorMessage, 'Invalid JOIN.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test scanning and coding.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testScanningAndCoding()
|
||||
{
|
||||
$toolbox = R::getToolBox();
|
||||
$adapter = $toolbox->getDatabaseAdapter();
|
||||
$writer = $toolbox->getWriter();
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$pdo = $adapter->getDatabase();
|
||||
$a = new AssociationManager( $toolbox );
|
||||
asrt( in_array( "testtable", $writer->getTables() ), FALSE );
|
||||
$writer->createTable( "testtable" );
|
||||
asrt( in_array( "testtable", $writer->getTables() ), TRUE );
|
||||
asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 1 );
|
||||
asrt( in_array( "id", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
|
||||
asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), FALSE );
|
||||
$writer->addColumn( "testtable", "c1", 1 );
|
||||
asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 2 );
|
||||
asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
|
||||
foreach ( $writer->sqltype_typeno as $key => $type ) {
|
||||
asrt( $writer->code( $key ), $type );
|
||||
}
|
||||
asrt( $writer->code( "unknown" ), 99 );
|
||||
asrt( $writer->scanType( FALSE ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( NULL ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( 2 ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( 255 ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( 256 ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( -1 ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( 1.5 ), SQLiteT::C_DATATYPE_NUMERIC );
|
||||
asrt( $writer->scanType( 2147483648 - 1 ), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( 2147483648 ), SQLiteT::C_DATATYPE_TEXT );
|
||||
asrt( $writer->scanType( -2147483648 + 1), SQLiteT::C_DATATYPE_INTEGER );
|
||||
asrt( $writer->scanType( -2147483648 ), SQLiteT::C_DATATYPE_TEXT );
|
||||
asrt( $writer->scanType( INF ), SQLiteT::C_DATATYPE_TEXT );
|
||||
asrt( $writer->scanType( "abc" ), SQLiteT::C_DATATYPE_TEXT );
|
||||
asrt( $writer->scanType( '2010-10-10' ), SQLiteT::C_DATATYPE_NUMERIC );
|
||||
asrt( $writer->scanType( '2010-10-10 10:00:00' ), SQLiteT::C_DATATYPE_NUMERIC );
|
||||
asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), SQLiteT::C_DATATYPE_TEXT );
|
||||
$writer->widenColumn( "testtable", "c1", 2 );
|
||||
$cols = $writer->getColumns( "testtable" );
|
||||
asrt( $writer->code( $cols["c1"] ), 2 );
|
||||
$id = $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "lorem ipsum" ) ) );
|
||||
$row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
|
||||
asrt( $row[0]["c1"], "lorem ipsum" );
|
||||
$writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "ipsum lorem" ) ), $id );
|
||||
$row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
|
||||
asrt( $row[0]["c1"], "ipsum lorem" );
|
||||
$writer->deleteRecord( "testtable", array( "id" => array( $id ) ) );
|
||||
$row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
|
||||
asrt( empty( $row ), TRUE );
|
||||
}
|
||||
|
||||
/**
|
||||
* (FALSE should be stored as 0 not as '')
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testZeroIssue()
|
||||
{
|
||||
testpack( "Zero issue" );
|
||||
$toolbox = R::getToolBox();
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$bean = $redbean->dispense( "zero" );
|
||||
$bean->zero = FALSE;
|
||||
$bean->title = "bla";
|
||||
$redbean->store( $bean );
|
||||
asrt( count( $redbean->find( "zero", array(), " zero = 0 " ) ), 1 );
|
||||
testpack( "Test ANSI92 issue in clearrelations" );
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$a = new AssociationManager( $toolbox );
|
||||
$book = $redbean->dispense( "book" );
|
||||
$author1 = $redbean->dispense( "author" );
|
||||
$author2 = $redbean->dispense( "author" );
|
||||
$book->title = "My First Post";
|
||||
$author1->name = "Derek";
|
||||
$author2->name = "Whoever";
|
||||
set1toNAssoc( $a, $book, $author1 );
|
||||
set1toNAssoc( $a, $book, $author2 );
|
||||
pass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Various.
|
||||
* Tests whether writer correctly handles keyword 'group' and SQL state 23000 issue.
|
||||
* These tests remain here to make sure issues 9 and 10 never happen again.
|
||||
* However this bug will probably never re-appear due to changed architecture.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIssue9and10()
|
||||
{
|
||||
$toolbox = R::getToolBox();
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$adapter = $toolbox->getDatabaseAdapter();
|
||||
$a = new AssociationManager( $toolbox );
|
||||
$book = $redbean->dispense( "book" );
|
||||
$author1 = $redbean->dispense( "author" );
|
||||
$author2 = $redbean->dispense( "author" );
|
||||
$book->title = "My First Post";
|
||||
$author1->name = "Derek";
|
||||
$author2->name = "Whoever";
|
||||
$a->associate( $book, $author1 );
|
||||
$a->associate( $book, $author2 );
|
||||
pass();
|
||||
testpack( "Test Association Issue Group keyword (Issues 9 and 10)" );
|
||||
$group = $redbean->dispense( "group" );
|
||||
$group->name = "mygroup";
|
||||
$redbean->store( $group );
|
||||
try {
|
||||
$a->associate( $group, $book );
|
||||
pass();
|
||||
} catch ( SQL $e ) {
|
||||
fail();
|
||||
}
|
||||
// Test issue SQL error 23000
|
||||
try {
|
||||
$a->associate( $group, $book );
|
||||
pass();
|
||||
} catch ( SQL $e ) {
|
||||
print_r( $e );
|
||||
fail();
|
||||
}
|
||||
asrt( (int) $adapter->getCell( "select count(*) from book_group" ), 1 ); //just 1 rec!
|
||||
}
|
||||
|
||||
/**
|
||||
* Test various.
|
||||
* Test various somewhat uncommon trash/unassociate scenarios.
|
||||
* (i.e. unassociate unrelated beans, trash non-persistant beans etc).
|
||||
* Should be handled gracefully - no output checking.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testVaria2()
|
||||
{
|
||||
$toolbox = R::getToolBox();
|
||||
$redbean = $toolbox->getRedBean();
|
||||
$a = new AssociationManager( $toolbox );
|
||||
$book = $redbean->dispense( "book" );
|
||||
$author1 = $redbean->dispense( "author" );
|
||||
$author2 = $redbean->dispense( "author" );
|
||||
$book->title = "My First Post";
|
||||
$author1->name = "Derek";
|
||||
$author2->name = "Whoever";
|
||||
$a->unassociate( $book, $author1 );
|
||||
$a->unassociate( $book, $author2 );
|
||||
pass();
|
||||
$redbean->trash( $redbean->dispense( "bla" ) );
|
||||
pass();
|
||||
$bean = $redbean->dispense( "bla" );
|
||||
$bean->name = 1;
|
||||
$bean->id = 2;
|
||||
$redbean->trash( $bean );
|
||||
pass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test special data types.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSpecialDataTypes()
|
||||
{
|
||||
testpack( 'Special data types' );
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->date = 'someday';
|
||||
R::store( $bean );
|
||||
$cols = R::getColumns( 'bean' );
|
||||
asrt( $cols['date'], 'TEXT' );
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->date = '2011-10-10';
|
||||
R::nuke();
|
||||
$bean = R::dispense( 'bean' );
|
||||
$bean->date = '2011-10-10';
|
||||
R::store( $bean );
|
||||
$cols = R::getColumns( 'bean' );
|
||||
asrt( $cols['date'], 'NUMERIC' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test renewed error handling in SQLite.
|
||||
* In fluid mode ignore table/column not exists (HY000 + code 1).
|
||||
* In frozen mode ignore nothing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testErrorHandling()
|
||||
{
|
||||
R::nuke();
|
||||
R::store( R::dispense( 'book' ) );
|
||||
R::freeze( FALSE );
|
||||
R::find( 'book2', ' id > 0' );
|
||||
pass();
|
||||
R::find( 'book', ' id2 > ?' );
|
||||
pass();
|
||||
$exception = NULL;
|
||||
try {
|
||||
R::find( 'book', ' id = ?', array( 0, 1 ) );
|
||||
} catch( \Exception $e ) {
|
||||
$exception = $e;
|
||||
}
|
||||
asrt( ( $exception instanceof SQL ), TRUE );
|
||||
R::freeze( TRUE );
|
||||
$exception = NULL;
|
||||
try {
|
||||
R::find( 'book2', ' id > 0' );
|
||||
} catch( \Exception $e ) {
|
||||
$exception = $e;
|
||||
}
|
||||
asrt( ( $exception instanceof SQL ), TRUE );
|
||||
$exception = NULL;
|
||||
try {
|
||||
R::find( 'book', ' id2 > 0' );
|
||||
} catch( \Exception $e ) {
|
||||
$exception = $e;
|
||||
}
|
||||
asrt( ( $exception instanceof SQL ), TRUE );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user