Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Sqlite / Writer.php
1 <?php
2
3 namespace RedUNIT\Sqlite;
4
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;
10
11 /**
12  * Writer
13  *
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.
18  *
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
23  *
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.
27  */
28 class Writer extends Sqlite
29 {
30         /**
31          * Test scanning and coding.
32          *
33          * @return void
34          */
35         public function testScanningAndCoding()
36         {
37                 $toolbox = R::getToolBox();
38                 $adapter = $toolbox->getDatabaseAdapter();
39                 $writer  = $toolbox->getWriter();
40                 $redbean = $toolbox->getRedBean();
41                 $pdo     = $adapter->getDatabase();
42
43                 $a = new AssociationManager( $toolbox );
44
45                 asrt( in_array( "testtable", $writer->getTables() ), FALSE );
46
47                 $writer->createTable( "testtable" );
48
49                 asrt( in_array( "testtable", $writer->getTables() ), TRUE );
50
51                 asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 1 );
52
53                 asrt( in_array( "id", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
54                 asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), FALSE );
55
56                 $writer->addColumn( "testtable", "c1", 1 );
57
58                 asrt( count( array_keys( $writer->getColumns( "testtable" ) ) ), 2 );
59
60                 asrt( in_array( "c1", array_keys( $writer->getColumns( "testtable" ) ) ), TRUE );
61
62                 foreach ( $writer->sqltype_typeno as $key => $type ) {
63                         asrt( $writer->code( $key ), $type );
64                 }
65
66                 asrt( $writer->code( "unknown" ), 99 );
67
68                 asrt( $writer->scanType( FALSE ), SQLiteT::C_DATATYPE_INTEGER );
69                 asrt( $writer->scanType( NULL ), SQLiteT::C_DATATYPE_INTEGER );
70
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 );
76
77                 asrt( $writer->scanType( 2147483648 - 1 ), SQLiteT::C_DATATYPE_INTEGER );
78                 asrt( $writer->scanType( 2147483648 ), SQLiteT::C_DATATYPE_TEXT );
79
80                 asrt( $writer->scanType( -2147483648 + 1), SQLiteT::C_DATATYPE_INTEGER );
81                 asrt( $writer->scanType( -2147483648 ), SQLiteT::C_DATATYPE_TEXT );
82
83                 asrt( $writer->scanType( INF ), SQLiteT::C_DATATYPE_TEXT );
84
85                 asrt( $writer->scanType( "abc" ), SQLiteT::C_DATATYPE_TEXT );
86
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 );
89
90                 asrt( $writer->scanType( str_repeat( "lorem ipsum", 100 ) ), SQLiteT::C_DATATYPE_TEXT );
91
92                 $writer->widenColumn( "testtable", "c1", 2 );
93
94                 $cols = $writer->getColumns( "testtable" );
95
96                 asrt( $writer->code( $cols["c1"] ), 2 );
97
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 ) ) );
101
102                 asrt( $row[0]["c1"], "lorem ipsum" );
103
104                 $writer->updateRecord( "testtable", array( array( "property" => "c1", "value" => "ipsum lorem" ) ), $id );
105
106                 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
107
108                 asrt( $row[0]["c1"], "ipsum lorem" );
109
110                 $writer->deleteRecord( "testtable", array( "id" => array( $id ) ) );
111
112                 $row = $writer->queryRecord( "testtable", array( "id" => array( $id ) ) );
113
114                 asrt( empty( $row ), TRUE );
115         }
116
117         /**
118          * (FALSE should be stored as 0 not as '')
119          *
120          * @return void
121          */
122         public function testZeroIssue()
123         {
124                 testpack( "Zero issue" );
125
126                 $toolbox = R::getToolBox();
127                 $redbean = $toolbox->getRedBean();
128
129                 $bean = $redbean->dispense( "zero" );
130
131                 $bean->zero  = FALSE;
132                 $bean->title = "bla";
133
134                 $redbean->store( $bean );
135
136                 asrt( count( $redbean->find( "zero", array(), " zero = 0 " ) ), 1 );
137
138                 testpack( "Test ANSI92 issue in clearrelations" );
139
140                 $redbean = $toolbox->getRedBean();
141
142                 $a = new AssociationManager( $toolbox );
143
144                 $book    = $redbean->dispense( "book" );
145                 $author1 = $redbean->dispense( "author" );
146                 $author2 = $redbean->dispense( "author" );
147
148                 $book->title = "My First Post";
149
150                 $author1->name = "Derek";
151                 $author2->name = "Whoever";
152
153                 set1toNAssoc( $a, $book, $author1 );
154                 set1toNAssoc( $a, $book, $author2 );
155
156                 pass();
157         }
158
159         /**
160          * Various.
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.
164          *
165          * @return void
166          */
167         public function testIssue9and10()
168         {
169                 $toolbox = R::getToolBox();
170                 $redbean = $toolbox->getRedBean();
171                 $adapter = $toolbox->getDatabaseAdapter();
172
173                 $a = new AssociationManager( $toolbox );
174
175                 $book    = $redbean->dispense( "book" );
176                 $author1 = $redbean->dispense( "author" );
177                 $author2 = $redbean->dispense( "author" );
178
179                 $book->title = "My First Post";
180
181                 $author1->name = "Derek";
182                 $author2->name = "Whoever";
183
184                 $a->associate( $book, $author1 );
185                 $a->associate( $book, $author2 );
186
187                 pass();
188
189                 testpack( "Test Association Issue Group keyword (Issues 9 and 10)" );
190
191                 $group       = $redbean->dispense( "group" );
192                 $group->name = "mygroup";
193
194                 $redbean->store( $group );
195
196                 try {
197                         $a->associate( $group, $book );
198
199                         pass();
200                 } catch ( SQL $e ) {
201                         fail();
202                 }
203
204                 // Test issue SQL error 23000
205                 try {
206                         $a->associate( $group, $book );
207
208                         pass();
209                 } catch ( SQL $e ) {
210                         print_r( $e );
211
212                         fail();
213                 }
214
215                 asrt( (int) $adapter->getCell( "select count(*) from book_group" ), 1 ); //just 1 rec!
216         }
217
218         /**
219          * Test various.
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.
223          *
224          * @return void
225          */
226         public function testVaria2()
227         {
228                 $toolbox = R::getToolBox();
229                 $redbean = $toolbox->getRedBean();
230
231                 $a = new AssociationManager( $toolbox );
232
233                 $book    = $redbean->dispense( "book" );
234                 $author1 = $redbean->dispense( "author" );
235                 $author2 = $redbean->dispense( "author" );
236
237                 $book->title = "My First Post";
238
239                 $author1->name = "Derek";
240                 $author2->name = "Whoever";
241
242                 $a->unassociate( $book, $author1 );
243                 $a->unassociate( $book, $author2 );
244
245                 pass();
246
247                 $redbean->trash( $redbean->dispense( "bla" ) );
248
249                 pass();
250
251                 $bean = $redbean->dispense( "bla" );
252
253                 $bean->name = 1;
254                 $bean->id   = 2;
255
256                 $redbean->trash( $bean );
257
258                 pass();
259         }
260
261         /**
262          * Test special data types.
263          *
264          * @return void
265          */
266         public function testSpecialDataTypes()
267         {
268                 testpack( 'Special data types' );
269
270                 $bean = R::dispense( 'bean' );
271
272                 $bean->date = 'someday';
273
274                 R::store( $bean );
275
276                 $cols = R::getColumns( 'bean' );
277
278                 asrt( $cols['date'], 'TEXT' );
279
280                 $bean = R::dispense( 'bean' );
281
282                 $bean->date = '2011-10-10';
283
284                 R::nuke();
285
286                 $bean = R::dispense( 'bean' );
287
288                 $bean->date = '2011-10-10';
289
290                 R::store( $bean );
291
292                 $cols = R::getColumns( 'bean' );
293
294                 asrt( $cols['date'], 'NUMERIC' );
295         }
296 }