3 namespace RedUNIT\Postgres;
5 use RedUNIT\Postgres as Postgres;
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 PostgreSQL, to this
16 * end we use a reference implementation of a UUID MySQL Writer:
17 * UUIDWriterPostgres, 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/Postgres/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 Postgres
38 public function testUUIDReadSupport()
43 $createPageTableSQL = '
53 $createBookTableSQL = '
61 $createPagePageTableSQL = '
70 R::exec( $createBookTableSQL );
71 R::exec( $createPageTableSQL );
72 R::exec( $createPagePageTableSQL );
76 $book1ID = '6ccd780c-baba-1026-9564-0040f4311e21';
77 $book2ID = '6ccd780c-baba-1026-9564-0040f4311e22';
78 $page1ID = '6ccd780c-baba-1026-9564-0040f4311e23';
79 $page2ID = '6ccd780c-baba-1026-9564-0040f4311e24';
80 $page3ID = '6ccd780c-baba-1026-9564-0040f4311e25';
81 $pagePage1ID = '6ccd780c-baba-1026-9564-0040f4311e26';
84 INSERT INTO book (id, title) VALUES( '$book1ID', 'book 1' );
88 INSERT INTO book (id, title) VALUES( '$book2ID', 'book 2' );
92 INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
96 INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
100 INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
103 $insertPagePage1SQL = "
104 INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
107 R::exec( $insertBook1SQL );
108 R::exec( $insertBook2SQL );
109 R::exec( $insertPage1SQL );
110 R::exec( $insertPage2SQL );
111 R::exec( $insertPage3SQL );
112 R::exec( $insertPagePage1SQL );
114 //basic tour of basic functions....
116 $book1 = R::load( 'book', $book1ID );
118 asrt( $book1->id, $book1ID );
119 asrt( $book1->title, 'book 1' );
121 $book2 = R::load( 'book', $book2ID );
123 asrt( $book2->id, $book2ID );
124 asrt( $book2->title, 'book 2' );
126 asrt( count( $book1->ownPage ), 2 );
127 asrt( count( $book1->fresh()->with( 'LIMIT 1' )->ownPage ), 1 );
128 asrt( count( $book1->fresh()->withCondition( ' title = ? ', array('page 2 of book 1'))->ownPage ), 1 );
130 asrt( count($book2->ownPage), 1 );
131 asrt( $book2->fresh()->countOwn( 'page' ), 1 );
133 $page1 = R::load( 'page', $page1ID );
134 asrt( count( $page1->sharedPage ), 0 );
135 asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
137 $page2 = R::load( 'page', $page2ID );
138 asrt( count($page2->sharedPage), 1 );
139 asrt( $page2->fresh()->countShared( 'page' ), 1 );
141 $page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
142 asrt( $page3->id, $page3ID );
143 asrt( $page3->book->id, $book2ID );
147 * Test Full fluid UUID support.
150 public function testFullSupport()
153 //Rewire objects to support UUIDs.
154 $oldToolBox = R::getToolBox();
155 $oldAdapter = $oldToolBox->getDatabaseAdapter();
156 $uuidWriter = new \UUIDWriterPostgres( $oldAdapter );
157 $newRedBean = new OODB( $uuidWriter );
158 $newToolBox = new ToolBox( $newRedBean, $oldAdapter, $uuidWriter );
159 R::configureFacadeWithToolbox( $newToolBox );
161 list( $mansion, $rooms, $ghosts, $key ) = R::dispenseAll( 'mansion,room*3,ghost*4,key' );
162 $mansion->name = 'Haunted Mansion';
163 $mansion->xownRoomList = $rooms;
164 $rooms[0]->name = 'Green Room';
165 $rooms[1]->name = 'Red Room';
166 $rooms[2]->name = 'Blue Room';
167 $ghosts[0]->name = 'zero';
168 $ghosts[1]->name = 'one';
169 $ghosts[2]->name = 'two';
170 $ghosts[3]->name = 'three';
171 $rooms[0]->noLoad()->sharedGhostList = array( $ghosts[0], $ghosts[1] );
172 $rooms[1]->noLoad()->sharedGhostList = array( $ghosts[0], $ghosts[2] );
173 $rooms[2]->noLoad()->sharedGhostList = array( $ghosts[1], $ghosts[3], $ghosts[2] );
174 $rooms[2]->xownKey = array( $key );
175 //Can we store a bean hierachy with UUIDs?
178 $id = R::store( $mansion );
181 asrt( is_string( $id ), TRUE );
182 asrt( strlen( $id ), 36 );
183 $haunted = R::load( 'mansion', $id );
184 asrt( $haunted->name, 'Haunted Mansion' );
185 asrt( is_string( $haunted->id ), TRUE );
186 asrt( strlen( $haunted->id ), 36 );
187 asrt( is_array( $haunted->xownRoomList ), TRUE );
188 asrt( count( $haunted->ownRoom ), 3 );
189 $rooms = $haunted->xownRoomList;
191 //Do some counting...
193 foreach( $rooms as $room ) {
194 if ( $room->name === 'Green Room' ) {
199 asrt( !is_null( $greenRoom ), TRUE );
200 asrt( is_array( $greenRoom->with(' ORDER BY id ')->sharedGhostList ), TRUE );
201 asrt( count( $greenRoom->sharedGhostList ), 2 );
203 foreach( $greenRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
205 $names = implode(',', $names);
206 asrt($names, 'one,zero');
207 $rooms = $haunted->xownRoomList;
209 foreach( $rooms as $room ) {
210 if ( $room->name === 'Blue Room' ) {
215 asrt( !is_null( $blueRoom ), TRUE );
216 asrt( is_array( $blueRoom->sharedGhostList ), TRUE );
217 asrt( count( $blueRoom->sharedGhostList ), 3 );
219 foreach( $blueRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
221 $names = implode(',', $names);
222 asrt($names, 'one,three,two');
223 $rooms = $haunted->xownRoomList;
225 foreach( $rooms as $room ) {
226 if ( $room->name === 'Red Room' ) {
227 $redRoom = $room; break;
231 foreach( $redRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
233 $names = implode(',', $names);
234 asrt($names, 'two,zero');
235 asrt( !is_null( $redRoom ), TRUE );
236 asrt( is_array( $redRoom->sharedGhostList ), TRUE );
237 asrt( count( $redRoom->sharedGhostList ), 2 );
239 //Can we repaint a room?
240 $redRoom->name = 'Yellow Room';
241 $id = R::store($redRoom);
242 $yellowRoom = R::load( 'room', $id );
243 asrt( $yellowRoom->name, 'Yellow Room');
244 asrt( !is_null( $yellowRoom ), TRUE );
245 asrt( is_array( $yellowRoom->sharedGhostList ), TRUE );
246 asrt( count( $yellowRoom->sharedGhostList ), 2 );
248 //Can we throw one ghost out?
249 array_pop( $yellowRoom->sharedGhost );
250 R::store( $yellowRoom );
251 $yellowRoom = $yellowRoom->fresh();
252 asrt( $yellowRoom->name, 'Yellow Room');
253 asrt( !is_null( $yellowRoom ), TRUE );
254 asrt( is_array( $yellowRoom->sharedGhostList ), TRUE );
255 asrt( count( $yellowRoom->sharedGhostList ), 1 );
257 //can we remove one of the rooms?
258 asrt( R::count('key'), 1);
259 $list = $mansion->withCondition(' "name" = ? ', array('Blue Room'))->xownRoomList;
260 $room = reset($list);
261 unset($mansion->xownRoomList[$room->id]);
263 asrt(R::count('room'), 2);
265 //and what about its dependent beans?
266 asrt(R::count('key'), 0);
267 asrt(R::count('ghost_room'), 3);
269 //and can we find ghosts?
270 $ghosts = R::find('ghost');
271 asrt(count($ghosts), 4);
272 $ghosts = R::findAll('ghost', 'ORDER BY id');
273 asrt(count($ghosts), 4);
274 $ghosts = R::findAll('ghost', 'ORDER BY id LIMIT 2');
275 asrt(count($ghosts), 2);
276 $ghostZero = R::findOne('ghost', ' "name" = ? ', array( 'zero' ) );
277 asrt( ($ghostZero instanceof OODBBean), TRUE );
279 //can we create link properties on existing tables?
280 $blackRoom = R::dispense( 'room' );
281 $blackRoom->name = 'Black Room';
282 $ghostZero->link('ghost_room', array('mood'=>'grumpy'))->room = $blackRoom;
283 R::store($ghostZero);
284 $ghostZero = $ghostZero->fresh();
285 $list = $ghostZero->sharedRoomList;
286 asrt(count($list), 3);
287 $ghostZero = $ghostZero->fresh();
288 $list = $ghostZero->withCondition(' ghost_room.mood = ? ', array('grumpy'))->sharedRoomList;
289 asrt(count($list), 1);
291 //can we load a batch?
292 $ids = R::getCol('SELECT id FROM ghost');
293 $ghosts = R::batch('ghost', $ids);
294 asrt(count($ghosts), 4);
296 //can we do an aggregation?
297 $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
298 asrt(count($ghosts), 2);
300 //can we duplicate the mansion?
301 asrt(R::count('mansion'), 1);
302 asrt(R::count('room'), 3);
303 asrt(R::count('ghost'), 4);
304 $copy = R::dup($mansion);
306 asrt(R::count('mansion'), 2);
307 asrt(R::count('room'), 5); //black room does not belong to mansion 1
308 asrt(R::count('ghost'), 4);
310 //can we do some counting using the list?
311 asrt( $copy->countOwn('room'), 2);
312 $rooms = $copy->withCondition(' "name" = ? ', array('Green Room'))->xownRoomList;
313 $room = reset($rooms);
314 asrt($room->countShared('ghost'), 2);
316 //Finally restore old toolbox
317 R::configureFacadeWithToolbox( $oldToolBox );