Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Mysql / Uuid.php
1 <?php
2
3 namespace RedUNIT\Mysql;
4
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;
11
12 /**
13  * UUID
14  *
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
20  * unexpected issues.
21  *
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
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 Mysql
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 CHAR( 40 ),
48                                 book_id CHAR( 40 ),
49                                 magazine_id CHAR( 40 ),
50                                 title VARCHAR(255),
51                                 PRIMARY KEY ( id )
52                         )
53                         ENGINE = InnoDB DEFAULT
54                         CHARSET=utf8mb4
55                         COLLATE=utf8mb4_unicode_ci ';
56
57                 $createBookTableSQL = '
58                         CREATE TABLE
59                         `book`
60                         (
61                                 id CHAR( 40 ),
62                                 title VARCHAR(255),
63                                 PRIMARY KEY ( id )
64                         )
65                         ENGINE = InnoDB DEFAULT
66                         CHARSET=utf8mb4
67                         COLLATE=utf8mb4_unicode_ci ';
68
69                 $createPagePageTableSQL = '
70                         CREATE TABLE
71                         `page_page`
72                         (
73                                 id CHAR( 40 ),
74                                 page_id CHAR( 40 ),
75                                 page2_id CHAR( 40 ),
76                                 PRIMARY KEY ( id )
77                         )
78                         ENGINE = InnoDB DEFAULT
79                         CHARSET=utf8mb4
80                         COLLATE=utf8mb4_unicode_ci ';
81
82                 R::exec( $createBookTableSQL );
83                 R::exec( $createPageTableSQL );
84                 R::exec( $createPagePageTableSQL );
85
86                 //insert some records
87
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';
94
95                 $insertBook1SQL = "
96                         INSERT INTO book (id, title) VALUES( '$book1ID', 'book 1' );
97                 ";
98
99                 $insertBook2SQL = "
100                         INSERT INTO book (id, title) VALUES( '$book2ID', 'book 2' );
101                 ";
102
103                 $insertPage1SQL = "
104                         INSERT INTO page (id, book_id, title, magazine_id) VALUES( '$page1ID', '$book1ID', 'page 1 of book 1', '$book2ID' );
105                 ";
106
107                 $insertPage2SQL = "
108                         INSERT INTO page (id, book_id, title) VALUES( '$page2ID', '$book1ID', 'page 2 of book 1' );
109                 ";
110
111                 $insertPage3SQL = "
112                         INSERT INTO page (id, book_id, title) VALUES( '$page3ID', '$book2ID', 'page 1 of book 2' );
113                 ";
114
115                 $insertPagePage1SQL = "
116                         INSERT INTO page_page (id, page_id, page2_id) VALUES( '$pagePage1ID', '$page2ID', '$page3ID' );
117                 ";
118
119                 R::exec( $insertBook1SQL );
120                 R::exec( $insertBook2SQL );
121                 R::exec( $insertPage1SQL );
122                 R::exec( $insertPage2SQL );
123                 R::exec( $insertPage3SQL );
124                 R::exec( $insertPagePage1SQL );
125
126                 //basic tour of basic functions....
127
128                 $book1 = R::load( 'book', $book1ID );
129
130                 asrt( $book1->id, $book1ID );
131                 asrt( $book1->title, 'book 1' );
132
133                 $book2 = R::load( 'book', $book2ID );
134
135                 asrt( $book2->id, $book2ID );
136                 asrt( $book2->title, 'book 2' );
137
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 );
141
142                 asrt( count($book2->ownPage), 1 );
143                 asrt( $book2->fresh()->countOwn( 'page' ), 1 );
144
145                 $page1 = R::load( 'page', $page1ID );
146                 asrt( count( $page1->sharedPage ), 0 );
147                 asrt( $page1->fetchAs( 'book' )->magazine->id, $book2ID );
148
149                 $page2 = R::load( 'page', $page2ID );
150                 asrt( count($page2->sharedPage), 1 );
151                 asrt( $page2->fresh()->countShared( 'page' ), 1 );
152
153                 $page3 = R::findOne( 'page', ' title = ? ', array( 'page 1 of book 2' ) );
154                 asrt( $page3->id, $page3ID );
155                 asrt( $page3->book->id, $book2ID );
156         }
157
158         /**
159          * Test Full fluid UUID support.
160          *
161          */
162         public function testFullSupport()
163         {
164                 R::nuke();
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 );
172
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;
198
199                 //Do some counting...
200                 $greenRoom = NULL;
201                 foreach( $rooms as $room ) {
202                         if ( $room->name === 'Green Room' ) {
203                                 $greenRoom = $room;
204                                 break;
205                         }
206                 }
207                 asrt( !is_null( $greenRoom ), TRUE );
208                 asrt( is_array( $greenRoom->with(' ORDER BY id ')->sharedGhostList ), TRUE );
209                 asrt( count( $greenRoom->sharedGhostList ), 2 );
210                 $names = array();
211                 foreach( $greenRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
212                 sort($names);
213                 $names = implode(',', $names);
214                 asrt($names, 'one,zero');
215                 $rooms = $haunted->xownRoomList;
216                 $blueRoom = NULL;
217                 foreach( $rooms as $room ) {
218                         if ( $room->name === 'Blue Room' ) {
219                                 $blueRoom = $room;
220                                 break;
221                         }
222                 }
223                 asrt( !is_null( $blueRoom ), TRUE );
224                 asrt( is_array( $blueRoom->sharedGhostList ), TRUE );
225                 asrt( count( $blueRoom->sharedGhostList ), 3 );
226                 $names = array();
227                 foreach( $blueRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
228                 sort($names);
229                 $names = implode(',', $names);
230                 asrt($names, 'one,three,two');
231                 $rooms = $haunted->xownRoomList;
232                 $redRoom = NULL;
233                 foreach( $rooms as $room ) {
234                         if ( $room->name === 'Red Room' ) {
235                                 $redRoom = $room; break;
236                         }
237                 }
238                 $names = array();
239                 foreach( $redRoom->sharedGhost as $ghost ) $names[] = $ghost->name;
240                 sort($names);
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 );
246
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 );
255
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 );
264
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]);
270                 R::store($mansion);
271                 asrt(R::count('room'), 2);
272
273                 //and what about its dependent beans?
274                 asrt(R::count('key'), 0);
275                 asrt(R::count('ghost_room'), 3);
276
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 );
286
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);
298
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);
303
304                 //can we do an aggregation?
305                 $ghosts = $greenRoom->aggr('ownGhostRoom', 'ghost', 'ghost');
306                 asrt(count($ghosts), 2);
307
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);
313                 R::store($copy);
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);
317
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);
323
324                 //Finally restore old toolbox
325                 R::configureFacadeWithToolbox( $oldToolBox );
326         }
327 }