Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Postgres / Uuid.php
1 <?php
2
3 namespace RedUNIT\Postgres;
4
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;
11
12 /**
13  * UUID
14  *
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
20  * unexpected issues.
21  *
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
26  *
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.
30  */
31 class Uuid extends Postgres
32 {
33         /**
34          * Test Read-support.
35          *
36          * @return void
37          */
38         public function testUUIDReadSupport()
39         {
40
41                 R::nuke();
42
43                 $createPageTableSQL = '
44                         CREATE TABLE
45                         page
46                         (
47                                 id UUID PRIMARY KEY,
48                                 book_id UUID,
49                                 magazine_id UUID,
50                                 title VARCHAR(255)
51                         )';
52
53                 $createBookTableSQL = '
54                         CREATE TABLE
55                         book
56                         (
57                                 id UUID PRIMARY KEY,
58                                 title VARCHAR(255)
59                         )';
60
61                 $createPagePageTableSQL = '
62                         CREATE TABLE
63                         page_page
64                         (
65                                 id UUID PRIMARY KEY,
66                                 page_id UUID,
67                                 page2_id UUID
68                         )';
69
70                 R::exec( $createBookTableSQL );
71                 R::exec( $createPageTableSQL );
72                 R::exec( $createPagePageTableSQL );
73
74                 //insert some records
75
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';
82
83                 $insertBook1SQL = "
84                         INSERT INTO book (id, title) VALUES( '$book1ID', 'book 1' );
85                 ";
86
87                 $insertBook2SQL = "
88                         INSERT INTO book (id, title) VALUES( '$book2ID', 'book 2' );
89                 ";
90
91                 $insertPage1SQL = "
92                         INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
93                 ";
94
95                 $insertPage2SQL = "
96                         INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
97                 ";
98
99                 $insertPage3SQL = "
100                         INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
101                 ";
102
103                 $insertPagePage1SQL = "
104                         INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
105                 ";
106
107                 R::exec( $insertBook1SQL );
108                 R::exec( $insertBook2SQL );
109                 R::exec( $insertPage1SQL );
110                 R::exec( $insertPage2SQL );
111                 R::exec( $insertPage3SQL );
112                 R::exec( $insertPagePage1SQL );
113
114                 //basic tour of basic functions....
115
116                 $book1 = R::load( 'book', $book1ID );
117
118                 asrt( $book1->id, $book1ID );
119                 asrt( $book1->title, 'book 1' );
120
121                 $book2 = R::load( 'book', $book2ID );
122
123                 asrt( $book2->id, $book2ID );
124                 asrt( $book2->title, 'book 2' );
125
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 );
129
130                 asrt( count($book2->ownPage), 1 );
131                 asrt( $book2->fresh()->countOwn( 'page' ), 1 );
132
133                 $page1 = R::load( 'page', $page1ID );
134                 asrt( count( $page1->sharedPage ), 0 );
135                 asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
136
137                 $page2 = R::load( 'page', $page2ID );
138                 asrt( count($page2->sharedPage), 1 );
139                 asrt( $page2->fresh()->countShared( 'page' ), 1 );
140
141                 $page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
142                 asrt( $page3->id, $page3ID );
143                 asrt( $page3->book->id, $book2ID );
144         }
145
146         /**
147          * Test Full fluid UUID support.
148          *
149          */
150         public function testFullSupport()
151         {
152
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 );
160
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?
176
177                 R::debug(1);
178                 $id = R::store( $mansion );
179                 //exit;
180
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;
190
191                 //Do some counting...
192                 $greenRoom = NULL;
193                 foreach( $rooms as $room ) {
194                         if ( $room->name === 'Green Room' ) {
195                                 $greenRoom = $room;
196                                 break;
197                         }
198                 }
199                 asrt( !is_null( $greenRoom ), TRUE );
200                 asrt( is_array( $greenRoom->with(' ORDER BY id ')->sharedGhostList ), TRUE );
201                 asrt( count( $greenRoom->sharedGhostList ), 2 );
202                 $names = array();
203                 foreach( $greenRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
204                 sort($names);
205                 $names = implode(',', $names);
206                 asrt($names, 'one,zero');
207                 $rooms = $haunted->xownRoomList;
208                 $blueRoom = NULL;
209                 foreach( $rooms as $room ) {
210                         if ( $room->name === 'Blue Room' ) {
211                                 $blueRoom = $room;
212                                 break;
213                         }
214                 }
215                 asrt( !is_null( $blueRoom ), TRUE );
216                 asrt( is_array( $blueRoom->sharedGhostList ), TRUE );
217                 asrt( count( $blueRoom->sharedGhostList ), 3 );
218                 $names = array();
219                 foreach( $blueRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
220                 sort($names);
221                 $names = implode(',', $names);
222                 asrt($names, 'one,three,two');
223                 $rooms = $haunted->xownRoomList;
224                 $redRoom = NULL;
225                 foreach( $rooms as $room ) {
226                         if ( $room->name === 'Red Room' ) {
227                                 $redRoom = $room; break;
228                         }
229                 }
230                 $names = array();
231                 foreach( $redRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
232                 sort($names);
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 );
238
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 );
247
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 );
256
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]);
262                 R::store($mansion);
263                 asrt(R::count('room'), 2);
264
265                 //and what about its dependent beans?
266                 asrt(R::count('key'), 0);
267                 asrt(R::count('ghost_room'), 3);
268
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 );
278
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);
290
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);
295
296                 //can we do an aggregation?
297                 $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
298                 asrt(count($ghosts), 2);
299
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);
305                 R::store($copy);
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);
309
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);
315
316                 //Finally restore old toolbox
317                 R::configureFacadeWithToolbox( $oldToolBox );
318         }
319 }