3 namespace RedUNIT\Mysql;
5 use RedUNIT\Mysql as Mysql;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper as SimpleFacadeBeanHelper;
8 use RedBeanPHP\OODB as OODB;
9 use RedBeanPHP\OODBBean as OODBBean;
10 use RedBeanPHP\ToolBox as ToolBox;
15 * Tests whether we can use UUIDs with MySQL/MariaDB, to this
16 * end we use a reference implementation of a UUID MySQL Writer:
17 * UUIDWriterMySQL, however this class is not part of the code base,
18 * it should be considered a reference or example implementation.
19 * These tests focus on whether UUIDs in general do not cause any
22 * @file RedUNIT/Mysql/Uuid.php
23 * @desc Tests read support for UUID tables.
24 * @author Gabor de Mooij and the RedBeanPHP Community
25 * @license New BSD/GPLv2
27 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
28 * This source file is subject to the New BSD/GPLv2 License that is bundled
29 * with this source code in the file license.txt.
31 class Uuid extends Mysql
38 public function testUUIDReadSupport()
43 $createPageTableSQL = '
49 magazine_id CHAR( 40 ),
53 ENGINE = InnoDB DEFAULT
55 COLLATE=utf8mb4_unicode_ci ';
57 $createBookTableSQL = '
65 ENGINE = InnoDB DEFAULT
67 COLLATE=utf8mb4_unicode_ci ';
69 $createPagePageTableSQL = '
78 ENGINE = InnoDB DEFAULT
80 COLLATE=utf8mb4_unicode_ci ';
82 R::exec( $createBookTableSQL );
83 R::exec( $createPageTableSQL );
84 R::exec( $createPagePageTableSQL );
88 $book1ID = '6ccd780c-baba-1026-9564-0040f4311e21';
89 $book2ID = '6ccd780c-baba-1026-9564-0040f4311e22';
90 $page1ID = '6ccd780c-baba-1026-9564-0040f4311e23';
91 $page2ID = '6ccd780c-baba-1026-9564-0040f4311e24';
92 $page3ID = '6ccd780c-baba-1026-9564-0040f4311e25';
93 $pagePage1ID = '6ccd780c-baba-1026-9564-0040f4311e26';
96 INSERT INTO book (id, title) VALUES( '$book1ID', 'book 1' );
100 INSERT INTO book (id, title) VALUES( '$book2ID', 'book 2' );
104 INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
108 INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
112 INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
115 $insertPagePage1SQL = "
116 INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
119 R::exec( $insertBook1SQL );
120 R::exec( $insertBook2SQL );
121 R::exec( $insertPage1SQL );
122 R::exec( $insertPage2SQL );
123 R::exec( $insertPage3SQL );
124 R::exec( $insertPagePage1SQL );
126 //basic tour of basic functions....
128 $book1 = R::load( 'book', $book1ID );
130 asrt( $book1->id, $book1ID );
131 asrt( $book1->title, 'book 1' );
133 $book2 = R::load( 'book', $book2ID );
135 asrt( $book2->id, $book2ID );
136 asrt( $book2->title, 'book 2' );
138 asrt( count( $book1->ownPage ), 2 );
139 asrt( count( $book1->fresh()->with( 'LIMIT 1' )->ownPage ), 1 );
140 asrt( count( $book1->fresh()->withCondition( ' title = ? ', array('page 2 of book 1'))->ownPage ), 1 );
142 asrt( count($book2->ownPage), 1 );
143 asrt( $book2->fresh()->countOwn( 'page' ), 1 );
145 $page1 = R::load( 'page', $page1ID );
146 asrt( count( $page1->sharedPage ), 0 );
147 asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
149 $page2 = R::load( 'page', $page2ID );
150 asrt( count($page2->sharedPage), 1 );
151 asrt( $page2->fresh()->countShared( 'page' ), 1 );
153 $page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
154 asrt( $page3->id, $page3ID );
155 asrt( $page3->book->id, $book2ID );
159 * Test Full fluid UUID support.
162 public function testFullSupport()
165 //Rewire objects to support UUIDs.
166 $oldToolBox = R::getToolBox();
167 $oldAdapter = $oldToolBox->getDatabaseAdapter();
168 $uuidWriter = new \UUIDWriterMySQL( $oldAdapter );
169 $newRedBean = new OODB( $uuidWriter );
170 $newToolBox = new ToolBox( $newRedBean, $oldAdapter, $uuidWriter );
171 R::configureFacadeWithToolbox( $newToolBox );
173 list( $mansion, $rooms, $ghosts, $key ) = R::dispenseAll( 'mansion,room*3,ghost*4,key' );
174 $mansion->name = 'Haunted Mansion';
175 $mansion->xownRoomList = $rooms;
176 $rooms[0]->name = 'Green Room';
177 $rooms[1]->name = 'Red Room';
178 $rooms[2]->name = 'Blue Room';
179 $ghosts[0]->name = 'zero';
180 $ghosts[1]->name = 'one';
181 $ghosts[2]->name = 'two';
182 $ghosts[3]->name = 'three';
183 $rooms[0]->sharedGhostList = array( $ghosts[0], $ghosts[1] );
184 $rooms[1]->sharedGhostList = array( $ghosts[0], $ghosts[2] );
185 $rooms[2]->sharedGhostList = array( $ghosts[1], $ghosts[3], $ghosts[2] );
186 $rooms[2]->xownKey = array( $key );
187 //Can we store a bean hierachy with UUIDs?
188 $id = R::store( $mansion );
189 asrt( is_string( $id ), TRUE );
190 asrt( strlen( $id ), 36 );
191 $haunted = R::load( 'mansion', $id );
192 asrt( $haunted->name, 'Haunted Mansion' );
193 asrt( is_string( $haunted->id ), TRUE );
194 asrt( strlen( $haunted->id ), 36 );
195 asrt( is_array( $haunted->xownRoomList ), TRUE );
196 asrt( count( $haunted->ownRoom ), 3 );
197 $rooms = $haunted->xownRoomList;
199 //Do some counting...
201 foreach( $rooms as $room ) {
202 if ( $room->name === 'Green Room' ) {
207 asrt( !is_null( $greenRoom ), TRUE );
208 asrt( is_array( $greenRoom->with(' ORDER BY id ')->sharedGhostList ), TRUE );
209 asrt( count( $greenRoom->sharedGhostList ), 2 );
211 foreach( $greenRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
213 $names = implode(',', $names);
214 asrt($names, 'one,zero');
215 $rooms = $haunted->xownRoomList;
217 foreach( $rooms as $room ) {
218 if ( $room->name === 'Blue Room' ) {
223 asrt( !is_null( $blueRoom ), TRUE );
224 asrt( is_array( $blueRoom->sharedGhostList ), TRUE );
225 asrt( count( $blueRoom->sharedGhostList ), 3 );
227 foreach( $blueRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
229 $names = implode(',', $names);
230 asrt($names, 'one,three,two');
231 $rooms = $haunted->xownRoomList;
233 foreach( $rooms as $room ) {
234 if ( $room->name === 'Red Room' ) {
235 $redRoom = $room; break;
239 foreach( $redRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
241 $names = implode(',', $names);
242 asrt($names, 'two,zero');
243 asrt( !is_null( $redRoom ), TRUE );
244 asrt( is_array( $redRoom->sharedGhostList ), TRUE );
245 asrt( count( $redRoom->sharedGhostList ), 2 );
247 //Can we repaint a room?
248 $redRoom->name = 'Yellow Room';
249 $id = R::store($redRoom);
250 $yellowRoom = R::load( 'room', $id );
251 asrt( $yellowRoom->name, 'Yellow Room');
252 asrt( !is_null( $yellowRoom ), TRUE );
253 asrt( is_array( $yellowRoom->sharedGhostList ), TRUE );
254 asrt( count( $yellowRoom->sharedGhostList ), 2 );
256 //Can we throw one ghost out?
257 array_pop( $yellowRoom->sharedGhost );
258 R::store( $yellowRoom );
259 $yellowRoom = $yellowRoom->fresh();
260 asrt( $yellowRoom->name, 'Yellow Room');
261 asrt( !is_null( $yellowRoom ), TRUE );
262 asrt( is_array( $yellowRoom->sharedGhostList ), TRUE );
263 asrt( count( $yellowRoom->sharedGhostList ), 1 );
265 //can we remove one of the rooms?
266 asrt( R::count('key'), 1);
267 $list = $mansion->withCondition(' `name` = ? ', array('Blue Room'))->xownRoomList;
268 $room = reset($list);
269 unset($mansion->xownRoomList[$room->id]);
271 asrt(R::count('room'), 2);
273 //and what about its dependent beans?
274 asrt(R::count('key'), 0);
275 asrt(R::count('ghost_room'), 3);
277 //and can we find ghosts?
278 $ghosts = R::find('ghost');
279 asrt(count($ghosts), 4);
280 $ghosts = R::findAll('ghost', 'ORDER BY id');
281 asrt(count($ghosts), 4);
282 $ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
283 asrt(count($ghosts), 2);
284 $ghostZero = R::findOne('ghost', ' `name` = ? ', array( 'zero' ) );
285 asrt( ($ghostZero instanceof OODBBean), TRUE );
287 //can we create link properties on existing tables?
288 $blackRoom = R::dispense( 'room' );
289 $blackRoom->name = 'Black Room';
290 $ghostZero->link('ghost_room', array('mood'=>'grumpy'))->room = $blackRoom;
291 R::store($ghostZero);
292 $ghostZero = $ghostZero->fresh();
293 $list = $ghostZero->sharedRoomList;
294 asrt(count($list), 3);
295 $ghostZero = $ghostZero->fresh();
296 $list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
297 asrt(count($list), 1);
299 //can we load a batch?
300 $ids = R::getCol('SELECT id FROM ghost');
301 $ghosts = R::batch('ghost', $ids);
302 asrt(count($ghosts), 4);
304 //can we do an aggregation?
305 $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
306 asrt(count($ghosts), 2);
308 //can we duplicate the mansion?
309 asrt(R::count('mansion'), 1);
310 asrt(R::count('room'), 3);
311 asrt(R::count('ghost'), 4);
312 $copy = R::dup($mansion);
314 asrt(R::count('mansion'), 2);
315 asrt(R::count('room'), 5); //black room does not belong to mansion 1
316 asrt(R::count('ghost'), 4);
318 //can we do some counting using the list?
319 asrt( $copy->countOwn('room'), 2);
320 $rooms = $copy->withCondition(' `name` = ? ', array('Green Room'))->xownRoomList;
321 $room = reset($rooms);
322 asrt($room->countShared('ghost'), 2);
324 //Finally restore old toolbox
325 R::configureFacadeWithToolbox( $oldToolBox );