3 namespace RedUNIT\Base;
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
12 * Tests NULL handling.
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
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.
23 class Xnull extends Base
28 public function testBindings()
31 $book = R::dispense( 'book' );
32 $book->content = NULL;
33 //can we store a NULL?
34 asrt( is_null( $book->content ), TRUE );
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 );
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?
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 );
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).
84 public function testUnsetParent()
87 $book = R::dispense( 'book' );
88 $book->title = 'My Book';
89 $page = R::dispense( 'page' );
90 $page->text = 'Lorem Ipsum';
91 $book->ownPage[] = $page;
93 $page = $page->fresh();
95 asrt( (int) $page->book->id, (int) $book->id );
98 $page = $page->fresh();
99 asrt( (int) $page->book->id, (int) $book->id );
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';
113 } catch( \Exception $exception ) {
118 $page = $page->fresh();
119 $page->book = NULL; //this must set field id to NULL not ADD column!
123 } catch( \Exception $exception ) {
126 $page = $page->fresh();
129 $page = $page->fresh();
130 asrt( is_null( $page->book_id ), TRUE );
133 $page = $page->fresh();
134 asrt( (int) $page->book->id, (int) $book->id );
137 asrt( is_null( $page->book_id ), TRUE );
138 asrt( is_null( $page->book ), TRUE );
143 * Test nullifying aliased parent.
147 public function testUnsetAliasedParent()
150 $book = R::dispense( 'book' );
151 $author = R::dispense( 'author' );
152 $book->coauthor = $author;
154 $book = $book->fresh();
155 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
156 unset( $book->coauthor );
158 $book = $book->fresh();
159 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
160 $book->coauthor = NULL;
162 $book = $book->fresh();
163 asrt( is_null( $book->fetchAs('author')->coauthor ), TRUE );
167 $book = R::dispense( 'book' );
168 $author = R::dispense( 'author' );
169 $book->coauthor = $author;
171 $book = $book->fresh();
172 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
173 unset( $book->coauthor );
175 $book = $book->fresh();
176 asrt( is_null( $book->fetchAs('author')->coauthor ), FALSE );
177 $book->coauthor = NULL;
179 $book = $book->fresh();
180 asrt( is_null( $book->fetchAs('author')->coauthor ), TRUE );
187 * Test NULL handling, setting a property to NULL must
192 public function testBasicNullHandling()
194 // NULL can change bean
195 $bean = R::dispense( 'bean' );
198 $bean = $bean->fresh();
199 asrt( $bean->hasChanged( 'bla' ), FALSE );
201 asrt( $bean->hasChanged( 'bla' ), TRUE );
203 $page = R::dispense( 'page' );
204 $book = R::dispense( 'book' );
205 $page->title = 'a NULL page';
207 $book->title = 'Why NUll is painful..';
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';
216 $page = R::load( 'page', $id );
217 $page->title = 'another title';
218 $page->book_id = NULL;
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.
231 public function ColumnType()
234 $book = R::dispense( 'book' );
235 $page = R::dispense( 'page' );
236 $book->ownPage[] = $page;
239 asrt( $page->getMeta( 'cast.book_id' ), 'id' );
243 * Test meta column type.
247 public function TypeColumn()
249 $book = R::dispense( 'book' );
250 $page = R::dispense( 'page' );
254 asrt( $page->getMeta( 'cast.book_id' ), 'id' );