Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Xnull.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8
9 /**
10  * Null
11  *
12  * Tests NULL handling.
13  *
14  * @file    RedUNIT/Base/Xnull.php
15  * @desc    Tests handling of NULL values.
16  * @author  Gabor de Mooij and the RedBeanPHP Community
17  * @license New BSD/GPLv2
18  *
19  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
20  * This source file is subject to the New BSD/GPLv2 License that is bundled
21  * with this source code in the file license.txt.
22  */
23 class Xnull extends Base
24 {
25         /**
26          * Test Null bindings.
27          */
28         public function testBindings()
29         {
30                 R::nuke();
31                 $book = R::dispense( 'book' );
32                 $book->content = NULL;
33                 //can we store a NULL?
34                 asrt( is_null( $book->content ), TRUE );
35                 R::store( $book );
36                 //did we really store the NULL value ?
37                 $book = R::findOne( 'book', ' content IS NULL ' );
38                 asrt( ( $book instanceof OODBBean ), TRUE );
39                 //still NULL, not empty STRING ?
40                 asrt( is_null( $book->content ), TRUE );
41                 $book->pages = 100;
42                 R::store( $book );
43                 //did we save it once again as NULL?
44                 $book = R::findOne( 'book', ' content IS NULL ' );
45                 asrt( ( $book instanceof OODBBean ), TRUE );
46                 asrt( is_null( $book->content ), TRUE );
47                 asrt( gettype( $book->pages ), 'string' );
48                 $otherBook = R::dispense( 'book' );
49                 $otherBook->pages = 99;
50                 //also if the column is VARCHAR-like?
51                 $otherBook->content = 'blah blah';
52                 R::store( $otherBook );
53                 $book = R::findOne( 'book', ' content IS NULL ' );
54                 asrt( ( $book instanceof OODBBean ), TRUE );
55                 asrt( is_null( $book->content ), TRUE );
56                 asrt( intval( $book->pages ), 100 );
57                 //can we query not NULL as well?
58                 $book = R::findOne( 'book', ' content IS NOT NULL ' );
59                 asrt( ( $book instanceof OODBBean ), TRUE );
60                 asrt( is_null( $book->content ), FALSE );
61                 asrt( intval( $book->pages ), 99 );
62                 asrt( $book->content, 'blah blah' );
63                 //Can we bind NULL directly?
64                 $book->isGood = FALSE;
65                 //Is NULL the default? And... no confusion with boolean FALSE?
66                 R::store( $book );
67                 $book = R::findOne( 'book', ' is_good IS NULL' );
68                 asrt( ( $book instanceof OODBBean ), TRUE );
69                 asrt( is_null( $book->content ), TRUE );
70                 asrt( intval( $book->pages ), 100 );
71                 $book = R::findOne( 'book', ' is_good = ?', array( 0 ) );
72                 asrt( ( $book instanceof OODBBean ), TRUE );
73                 asrt( is_null( $book->content ), FALSE );
74                 asrt( intval( $book->pages ), 99 );
75         }
76
77         /**
78          * Tests whether we can NULLify a parent bean
79          * page->book if the parent (book) is already
80          * NULL. (isset vs array_key_exists bug).
81          *
82          * @return void
83          */
84         public function testUnsetParent()
85         {
86                 R::nuke();
87                 $book = R::dispense( 'book' );
88                 $book->title = 'My Book';
89                 $page = R::dispense( 'page' );
90                 $page->text = 'Lorem Ipsum';
91                 $book->ownPage[] = $page;
92                 R::store( $book );
93                 $page = $page->fresh();
94                 R::freeze( TRUE );
95                 asrt( (int) $page->book->id, (int) $book->id );
96                 unset( $page->book );
97                 R::store( $page );
98                 $page = $page->fresh();
99                 asrt( (int) $page->book->id, (int) $book->id );
100                 $page->book = NULL;
101                 R::store( $page );
102                 $page = $page->fresh();
103                 asrt( $page->book, NULL );
104                 asrt( $page->book_id, NULL );
105                 asrt( $page->bookID, NULL );
106                 asrt( $page->bookId, NULL );
107                 $page = R::dispense( 'page' );
108                 $page->text = 'Another Page';
109                 $page->book = NULL;
110                 try {
111                         R::store( $page );
112                         fail();
113                 } catch( \Exception $exception ) {
114                         pass();
115                 }
116                 unset($page->book);
117                 R::store($page);
118                 $page = $page->fresh();
119                 $page->book = NULL; //this must set field id to NULL not ADD column!
120                 try {
121                         R::store($page);
122                         pass();
123                 } catch( \Exception $exception ) {
124                         fail();
125                 }
126                 $page = $page->fresh();
127                 $page->book = NULL;
128                 R::store( $page );
129                 $page = $page->fresh();
130                 asrt( is_null( $page->book_id ), TRUE );
131                 $page->book = $book;
132                 R::store( $page );
133                 $page = $page->fresh();
134                 asrt( (int) $page->book->id, (int) $book->id );
135                 $page->book = NULL;
136                 R::store( $page );
137                 asrt( is_null( $page->book_id ), TRUE );
138                 asrt( is_null( $page->book ), TRUE );
139                 R::freeze( FALSE );
140         }
141
142         /**
143          * Test nullifying aliased parent.
144          *
145          * @return void
146          */
147         public function testUnsetAliasedParent()
148         {
149                 R::nuke();
150                 $book = R::dispense( 'book' );
151                 $author = R::dispense( 'author' );
152                 $book->coauthor = $author;
153                 R::store( $book );
154                 $book = $book->fresh();
155                 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
156                 unset( $book->coauthor );
157                 R::store( $book );
158                 $book = $book->fresh();
159                 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
160                 $book->coauthor = NULL;
161                 R::store( $book );
162                 $book = $book->fresh();
163                 asrt( is_null( $book->fetchAs('author')->coauthor ), TRUE );
164                 R::trash( $book );
165                 R::trash( $author );
166                 R::freeze( TRUE );
167                 $book = R::dispense( 'book' );
168                 $author = R::dispense( 'author' );
169                 $book->coauthor = $author;
170                 R::store( $book );
171                 $book = $book->fresh();
172                 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
173                 unset( $book->coauthor );
174                 R::store( $book );
175                 $book = $book->fresh();
176                 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
177                 $book->coauthor = NULL;
178                 R::store( $book );
179                 $book = $book->fresh();
180                 asrt( is_null( $book->fetchAs('author')->coauthor ), TRUE );
181                 R::trash( $book );
182                 R::trash( $author );
183                 R::freeze( FALSE );
184         }
185
186         /**
187          * Test NULL handling, setting a property to NULL must
188          * cause a change.
189          *
190          * @return void
191          */
192         public function testBasicNullHandling()
193         {
194                 // NULL can change bean
195                 $bean      = R::dispense( 'bean' );
196                 $bean->bla = 'a';
197                 R::store( $bean );
198                 $bean = $bean->fresh();
199                 asrt( $bean->hasChanged( 'bla' ), FALSE );
200                 $bean->bla = NULL;
201                 asrt( $bean->hasChanged( 'bla' ), TRUE );
202                 // NULL test
203                 $page = R::dispense( 'page' );
204                 $book = R::dispense( 'book' );
205                 $page->title = 'a NULL page';
206                 $page->book  = $book;
207                 $book->title = 'Why NUll is painful..';
208                 R::store( $page );
209                 $bookid = $page->book->id;
210                 unset( $page->book );
211                 $id = R::store( $page );
212                 $page = R::load( 'page', $id );
213                 $page->title = 'another title';
214                 R::store( $page );
215                 pass();
216                 $page = R::load( 'page', $id );
217                 $page->title   = 'another title';
218                 $page->book_id = NULL;
219                 R::store( $page );
220                 pass();
221         }
222
223         /**
224          * Here we test whether the column type is set correctly.
225          * Normally if you store NULL, the smallest type (bool/set) will
226          * be selected. However in case of a foreign key type INT should
227          * be selected because fks columns require matching types.
228          *
229          * @return void
230          */
231         public function ColumnType()
232         {
233
234                 $book = R::dispense( 'book' );
235                 $page = R::dispense( 'page' );
236                 $book->ownPage[] = $page;
237                 R::store( $book );
238                 pass();
239                 asrt( $page->getMeta( 'cast.book_id' ), 'id' );
240         }
241
242         /**
243          * Test meta column type.
244          *
245          * @return void
246          */
247         public function TypeColumn()
248         {
249                 $book = R::dispense( 'book' );
250                 $page = R::dispense( 'page' );
251                 $page->book = $book;
252                 R::store( $page );
253                 pass();
254                 asrt( $page->getMeta( 'cast.book_id' ), 'id' );
255         }
256 }