3 namespace RedUNIT\Sqlite;
5 use RedUNIT\Sqlite as Sqlite;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\AssociationManager as AssociationManager;
8 use RedBeanPHP\QueryWriter\SQLiteT as SQLiteT;
9 use RedBeanPHP\RedException\SQL as SQL;
14 * Tests for SQLite Query Writer.
15 * This test class contains Query Writer specific tests.
16 * Use this class to add tests to test Query Writer specific
17 * behaviours, quirks and issues.
19 * @file RedUNIT/Sqlite/Writer.php
20 * @desc Tests writer specific functions.
21 * @author Gabor de Mooij and the RedBeanPHP Community
22 * @license New BSD/GPLv2
24 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
25 * This source file is subject to the New BSD/GPLv2 License that is bundled
26 * with this source code in the file license.txt.
28 class Writer extends Sqlite
31 * Test scanning and coding.
35 public function testScanningAndCoding()
37 $toolbox = R::getToolBox();
38 $adapter = $toolbox->getDatabaseAdapter();
39 $writer = $toolbox->getWriter();
40 $redbean = $toolbox->getRedBean();
41 $pdo = $adapter->getDatabase();
43 $a = new AssociationManager( $toolbox );
45 asrt( in_array( "testtable", $writer->getTables() ), FALSE );
47 $writer->createTable( "testtable" );
49 asrt( in_array( "testtable", $writer->getTables() ), TRUE );
51 asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 1 );
53 asrt( in_array( "id", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
54 asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), FALSE );
56 $writer->addColumn( "testtable", "c1", 1 );
58 asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 2 );
60 asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
62 foreach ( $writer->sqltype_typeno as $key => $type ) {
63 asrt( $writer->code( $key ), $type );
66 asrt( $writer->code( "unknown" ), 99 );
68 asrt( $writer->scanType( FALSE ), SQLiteT::C_DATATYPE_INTEGER );
69 asrt( $writer->scanType( NULL ), SQLiteT::C_DATATYPE_INTEGER );
71 asrt( $writer->scanType( 2 ), SQLiteT::C_DATATYPE_INTEGER );
72 asrt( $writer->scanType( 255 ), SQLiteT::C_DATATYPE_INTEGER );
73 asrt( $writer->scanType( 256 ), SQLiteT::C_DATATYPE_INTEGER );
74 asrt( $writer->scanType( -1 ), SQLiteT::C_DATATYPE_INTEGER );
75 asrt( $writer->scanType( 1.5 ), SQLiteT::C_DATATYPE_NUMERIC );
77 asrt( $writer->scanType( 2147483648 - 1 ), SQLiteT::C_DATATYPE_INTEGER );
78 asrt( $writer->scanType( 2147483648 ), SQLiteT::C_DATATYPE_TEXT );
80 asrt( $writer->scanType( -2147483648 + 1), SQLiteT::C_DATATYPE_INTEGER );
81 asrt( $writer->scanType( -2147483648 ), SQLiteT::C_DATATYPE_TEXT );
83 asrt( $writer->scanType( INF ), SQLiteT::C_DATATYPE_TEXT );
85 asrt( $writer->scanType( "abc" ), SQLiteT::C_DATATYPE_TEXT );
87 asrt( $writer->scanType( '2010-10-10' ), SQLiteT::C_DATATYPE_NUMERIC );
88 asrt( $writer->scanType( '2010-10-10 10:00:00' ), SQLiteT::C_DATATYPE_NUMERIC );
90 asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), SQLiteT::C_DATATYPE_TEXT );
92 $writer->widenColumn( "testtable", "c1", 2 );
94 $cols = $writer->getColumns( "testtable" );
96 asrt( $writer->code( $cols["c1"] ), 2 );
98 //$id = $writer->insertRecord("testtable", array("c1"), array(array("lorem ipsum")));
99 $id = $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "lorem ipsum" ) ) );
100 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
102 asrt( $row[0]["c1"], "lorem ipsum" );
104 $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "ipsum lorem" ) ), $id );
106 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
108 asrt( $row[0]["c1"], "ipsum lorem" );
110 $writer->deleteRecord( "testtable", array( "id" => array( $id ) ) );
112 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
114 asrt( empty( $row ), TRUE );
118 * (FALSE should be stored as 0 not as '')
122 public function testZeroIssue()
124 testpack( "Zero issue" );
126 $toolbox = R::getToolBox();
127 $redbean = $toolbox->getRedBean();
129 $bean = $redbean->dispense( "zero" );
132 $bean->title = "bla";
134 $redbean->store( $bean );
136 asrt( count( $redbean->find( "zero", array(), " zero = 0 " ) ), 1 );
138 testpack( "Test ANSI92 issue in clearrelations" );
140 $redbean = $toolbox->getRedBean();
142 $a = new AssociationManager( $toolbox );
144 $book = $redbean->dispense( "book" );
145 $author1 = $redbean->dispense( "author" );
146 $author2 = $redbean->dispense( "author" );
148 $book->title = "My First Post";
150 $author1->name = "Derek";
151 $author2->name = "Whoever";
153 set1toNAssoc( $a, $book, $author1 );
154 set1toNAssoc( $a, $book, $author2 );
161 * Tests whether writer correctly handles keyword 'group' and SQL state 23000 issue.
162 * These tests remain here to make sure issues 9 and 10 never happen again.
163 * However this bug will probably never re-appear due to changed architecture.
167 public function testIssue9and10()
169 $toolbox = R::getToolBox();
170 $redbean = $toolbox->getRedBean();
171 $adapter = $toolbox->getDatabaseAdapter();
173 $a = new AssociationManager( $toolbox );
175 $book = $redbean->dispense( "book" );
176 $author1 = $redbean->dispense( "author" );
177 $author2 = $redbean->dispense( "author" );
179 $book->title = "My First Post";
181 $author1->name = "Derek";
182 $author2->name = "Whoever";
184 $a->associate( $book, $author1 );
185 $a->associate( $book, $author2 );
189 testpack( "Test Association Issue Group keyword (Issues 9 and 10)" );
191 $group = $redbean->dispense( "group" );
192 $group->name = "mygroup";
194 $redbean->store( $group );
197 $a->associate( $group, $book );
204 // Test issue SQL error 23000
206 $a->associate( $group, $book );
215 asrt( (int) $adapter->getCell( "select count(*) from book_group" ), 1 ); //just 1 rec!
220 * Test various somewhat uncommon trash/unassociate scenarios.
221 * (i.e. unassociate unrelated beans, trash non-persistant beans etc).
222 * Should be handled gracefully - no output checking.
226 public function testVaria2()
228 $toolbox = R::getToolBox();
229 $redbean = $toolbox->getRedBean();
231 $a = new AssociationManager( $toolbox );
233 $book = $redbean->dispense( "book" );
234 $author1 = $redbean->dispense( "author" );
235 $author2 = $redbean->dispense( "author" );
237 $book->title = "My First Post";
239 $author1->name = "Derek";
240 $author2->name = "Whoever";
242 $a->unassociate( $book, $author1 );
243 $a->unassociate( $book, $author2 );
247 $redbean->trash( $redbean->dispense( "bla" ) );
251 $bean = $redbean->dispense( "bla" );
256 $redbean->trash( $bean );
262 * Test special data types.
266 public function testSpecialDataTypes()
268 testpack( 'Special data types' );
270 $bean = R::dispense( 'bean' );
272 $bean->date = 'someday';
276 $cols = R::getColumns( 'bean' );
278 asrt( $cols['date'], 'TEXT' );
280 $bean = R::dispense( 'bean' );
282 $bean->date = '2011-10-10';
286 $bean = R::dispense( 'bean' );
288 $bean->date = '2011-10-10';
292 $cols = R::getColumns( 'bean' );
294 asrt( $cols['date'], 'NUMERIC' );