3 namespace RedUNIT\Base;
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter;
10 R::ext('xdispense', function($type){
11 return R::getRedBean()->dispense($type);
14 define('BOOK', 'tbl_book');
15 define('AUTHOR', 'tbl_author');
16 define('COAUTHOR', 'coAuthor');
17 define('FRIEND', 'tbl_friend');
18 define('PUBLISHER', 'tbl_publisher');
19 define('BOOKLIST', 'ownTbl_book');
20 define('FRIENDLIST', 'sharedTbl_friend');
25 * Tests whether we can use tables with prefixes.
26 * Some people seem to like that.
28 * @file RedUNIT/Base/Prefixes.php
29 * @desc Tests whether you can use RedBeanPHP with table prefixes.
30 * @author Gabor de Mooij and the RedBeanPHP Community
31 * @license New BSD/GPLv2
33 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
34 * This source file is subject to the New BSD/GPLv2 License that is bundled
35 * with this source code in the file license.txt.
37 class Prefixes extends Base
42 public function testPrerequisites()
45 $bean = R::xdispense( 'type_with_underscore' );
46 asrt( ( $bean instanceof OODBBean ), TRUE );
47 asrt( constant( 'BOOK' ), 'tbl_book' );
48 asrt( constant( 'AUTHOR' ), 'tbl_author' );
49 asrt( constant( 'PUBLISHER' ), 'tbl_publisher' );
50 asrt( constant( 'FRIEND' ), 'tbl_friend' );
51 asrt( constant( 'BOOKLIST' ), 'ownTbl_book' );
52 asrt( constant( 'FRIENDLIST' ), 'sharedTbl_friend' );
53 asrt( constant( 'COAUTHOR' ), 'coAuthor' );
57 * Test basic CRUD operations.
59 public function testBasicOperations()
61 //Can we dispense a naughty bean? (with underscore)
62 $author = R::xdispense( AUTHOR );
63 asrt( ( $author instanceof OODBBean ), TRUE );
64 asrt( $author->getMeta('type'), AUTHOR );
65 $author->name = 'Mr. Quill';
66 $book = R::xdispense( BOOK );
67 asrt( ( $book instanceof OODBBean ), TRUE );
68 asrt( $book->getMeta('type'), BOOK );
69 $book->title = 'Good Stories';
70 $friend = R::xdispense( FRIEND );
71 $friend->name = 'Muse';
72 asrt( ( $friend instanceof OODBBean ), TRUE );
73 asrt( $friend->getMeta('type'), FRIEND );
74 $publisher = R::xdispense( PUBLISHER );
75 $publisher->name = 'Good Books';
76 asrt( ( $publisher instanceof OODBBean ), TRUE );
77 asrt( $publisher->getMeta('type'), PUBLISHER );
78 asrt( is_array( $author->{BOOKLIST} ), TRUE );
79 //add books to the book list using the constant
80 $author->{BOOKLIST}[] = $book;
81 asrt( count( $author->{BOOKLIST} ), 1 );
82 //can we also add friends? (N-M)
83 $author->{FRIENDLIST}[] = $friend;
84 $author->{PUBLISHER} = $publisher;
85 $id = R::store( $author );
86 asrt( ( $id > 0 ), TRUE );
87 $author = $author->fresh();
88 //Can we add another friend after reload?
89 $author->{FRIENDLIST}[] = R::xdispense( FRIEND )->setAttr( 'name', 'buddy' );
91 $author = $author->fresh();
92 //Now check the contents of the bean, its lists (books,friends) and parent (publisher)
93 asrt( $author->name, 'Mr. Quill' );
94 asrt( count( $author->{BOOKLIST} ), 1 );
95 $firstBook = reset( $author->{BOOKLIST} );
96 asrt( $firstBook->title, 'Good Stories' );
97 asrt( count( $author->{FRIENDLIST} ), 2 );
98 $firstFriend = reset( $author->{FRIENDLIST} );
99 $parent = $author->{PUBLISHER};
100 asrt( ( $parent instanceof OODBBean ), TRUE );
101 $tables = R::inspect();
102 //have all tables been prefixed?
103 foreach( $tables as $table ) asrt( strpos( $table, 'tbl_' ), 0 );
104 //Can we make an export?
105 $export = R::exportAll( R::findOne( AUTHOR ), TRUE );
106 $export = reset( $export );
107 asrt( isset( $export[ PUBLISHER ] ), TRUE );
108 asrt( isset( $export[ BOOKLIST ] ), TRUE );
109 asrt( isset( $export[ FRIENDLIST ] ), TRUE );
110 asrt( isset( $export[ 'ownBook' ] ), FALSE );
111 asrt( isset( $export[ 'sharedFriend' ] ), FALSE );
112 asrt( isset( $export[ 'publisher' ] ), FALSE );
114 $copy = R::dup( $author );
115 $copy->name = 'Mr. Clone';
117 $copy = $copy->fresh();
118 asrt( $copy->name, 'Mr. Clone' );
119 asrt( count( $copy->{BOOKLIST} ), 1 );
120 $firstBook = reset( $copy->{BOOKLIST} );
121 asrt( $firstBook->title, 'Good Stories' );
122 asrt( count( $copy->{FRIENDLIST} ), 2 );
123 $firstFriend = reset( $copy->{FRIENDLIST} );
124 $parent = $copy->{PUBLISHER};
125 asrt( ( $parent instanceof OODBBean ), TRUE );
127 asrt( R::count( AUTHOR ), 2 );
128 $copy = $copy->fresh();
129 asrt( $copy->countOwn( BOOK ), 1 );
130 asrt( $copy->countShared( FRIEND ), 2 );
133 asrt( R::count( AUTHOR ), 1 );
136 asrt( R::count( AUTHOR ), 0 );
137 asrt( count( R::inspect() ), 0 );
141 * Test basic operations in frozen mode.
143 public function testBasicOperationsFrozen()
146 $author = R::xdispense( AUTHOR );
147 $author->name = 'Mr. Quill';
148 $book = R::xdispense( BOOK );
149 $book->title = 'Good Stories';
150 $book2 = R::xdispense( BOOK );
151 $book2->title = 'Good Stories 2';
152 $friend = R::xdispense( FRIEND );
153 $friend->name = 'Muse';
154 $publisher = R::xdispense( PUBLISHER );
155 $publisher->name = 'Good Books';
156 $author->{BOOKLIST} = array( $book, $book2 );
157 $author->{FRIENDLIST}[] = $friend;
158 $author->{PUBLISHER} = $publisher;
159 $coAuthor = R::xdispense( AUTHOR );
160 $coAuthor->name = 'Xavier';
161 $book2->{COAUTHOR} = $coAuthor;
164 asrt( $author->name, 'Mr. Quill' );
165 asrt( count( $author->{BOOKLIST} ), 2 );
166 $firstBook = reset( $author->{BOOKLIST} );
167 asrt( $firstBook->title, 'Good Stories' );
168 asrt( count( $author->{FRIENDLIST} ), 1 );
169 $firstFriend = reset( $author->{FRIENDLIST} );
170 $parent = $author->{PUBLISHER};
171 asrt( ( $parent instanceof OODBBean ), TRUE );
172 $tables = R::inspect();
173 //have all tables been prefixed?
174 foreach( $tables as $table ) asrt( strpos( $table, 'tbl_' ), 0 );
175 //Can we make an export?
176 $export = R::exportAll( R::findOne( AUTHOR ), TRUE );
177 $export = reset( $export );
178 asrt( isset( $export[ PUBLISHER ] ), TRUE );
179 asrt( isset( $export[ BOOKLIST ] ), TRUE );
180 asrt( isset( $export[ FRIENDLIST ] ), TRUE );
181 asrt( isset( $export[ 'ownBook' ] ), FALSE );
182 asrt( isset( $export[ 'sharedFriend' ] ), FALSE );
183 asrt( isset( $export[ 'publisher' ] ), FALSE );
188 * Test conditions and aliases.
190 public function testConditionsAndAliases()
193 $author = R::xdispense( AUTHOR );
194 $author->name = 'Mr. Quill';
195 $book = R::xdispense( BOOK );
196 $book->title = 'Good Stories';
197 $book2 = R::xdispense( BOOK );
198 $book2->title = 'Good Stories 2';
199 $friend = R::xdispense( FRIEND );
200 $friend->name = 'Muse';
201 $publisher = R::xdispense( PUBLISHER );
202 $publisher->name = 'Good Books';
203 $author->{BOOKLIST} = array( $book, $book2 );
204 $author->{FRIENDLIST}[] = $friend;
205 $author->{PUBLISHER} = $publisher;
206 $coAuthor = R::xdispense( AUTHOR );
207 $coAuthor->name = 'Xavier';
208 $book2->{COAUTHOR} = $coAuthor;
210 $author = $author->fresh();
211 asrt( R::count( AUTHOR ), 2 );
212 //Can we use with and withCondition?
213 asrt( count( $author->{BOOKLIST} ), 2 );
214 asrt( count( $author->with(' LIMIT 1 ')->{BOOKLIST} ), 1 );
215 asrt( count( $author->withCondition(' title LIKE ? ', array( '%2%' ) )->{BOOKLIST} ), 1 );
216 //Can we use an alias?
217 $book2 = $book2->fresh();
218 asrt( $book2->fetchAs( AUTHOR )->{COAUTHOR}->name, 'Xavier' );
219 $coAuthor = $book2->fetchAs( AUTHOR )->{COAUTHOR}->fresh();
220 asrt( count( $coAuthor->alias( COAUTHOR )->{BOOKLIST} ), 1 );
224 * Test prettier tables using via().
226 public function testViaPrettification()
229 R::renameAssociation( 'tbl_author_tbl_friend', 'tbl_author_friend' );
230 $author = R::xdispense( AUTHOR );
231 $author->name = 'Mr. Quill';
232 $friend = R::xdispense( FRIEND );
233 $friend->name = 'Muse';
234 $author->{FRIENDLIST}[] = $friend;
235 $id = R::store( $author );
236 //print_r(R::inspect()); exit;
237 $author = R::load( AUTHOR, $id );
238 $tables = array_flip( R::inspect() );
239 asrt( isset( $tables[ 'tbl_author_friend' ] ), TRUE );
240 asrt( isset( $tables[ 'tbl_author_tbl_friend' ] ), FALSE );
241 asrt( count( $author->{FRIENDLIST} ), 1 );
242 AQueryWriter::clearRenames();
246 * Test self-referential N-M relations.
248 public function testSelfRefNM()
251 $friend1 = R::xdispense( FRIEND );
252 $friend1->name = 'f1';
253 $friend2 = R::xdispense( FRIEND );
254 $friend2->name = 'f2';
255 $friend3 = R::xdispense( FRIEND );
256 $friend3->name = 'f3';
257 $friend1->{FRIENDLIST} = array( $friend2, $friend3 );
258 $friend3->{FRIENDLIST} = array( $friend1 );
259 R::storeAll( array( $friend1, $friend2, $friend3 ) );
260 $friend1 = $friend1->fresh();
261 $friend2 = $friend2->fresh();
262 $friend3 = $friend3->fresh();
263 asrt( count( $friend1->{FRIENDLIST} ), 2 );
264 asrt( count( $friend2->{FRIENDLIST} ), 1 );
265 asrt( count( $friend3->{FRIENDLIST} ), 1 );
266 $friend = reset( $friend3->{FRIENDLIST} );
267 asrt( $friend->name, 'f1' );