-
Notifications
You must be signed in to change notification settings - Fork 279
/
Finding.php
873 lines (804 loc) · 28.7 KB
/
Finding.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
<?php
namespace RedUNIT\Base;
use RedUNIT\Base as Base;
use RedBeanPHP\Facade as R;
use RedBeanPHP\AssociationManager as AssociationManager;
use RedBeanPHP\OODB as OODB;
use RedBeanPHP\OODBBean as OODBBean;
use RedBeanPHP\RedException as RedException;
use RedBeanPHP\RedException\SQL as SQL;
/**
* Finding
*
* Tests whether we can find beans by passing SQL queries to the
* Facade or the Finder Service Class.
*
* @file RedUNIT/Base/Finding.php
* @desc Tests finding beans.
* @author Gabor de Mooij and the RedBeanPHP Community
* @license New BSD/GPLv2
*
* (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
* This source file is subject to the New BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class Finding extends Base {
/**
* Helper for testing findLike.
*
* @param array $flowers beans
* @param boolean $noSort sorting?
*
* @return string
*/
private function getColors( $flowers, $noSort = FALSE )
{
$colors = array();
foreach( $flowers as $flower ) $colors[] = $flower->color;
if ( !$noSort) sort( $colors );
return implode( ',', $colors );
}
/**
* Inserts data for findMulti-tests.
*
* @return void
*/
private function insertBookData()
{
list( $books, $pages, $texts, $categories ) = R::dispenseAll( 'book*5,page*25,text*60,category*3' );
$texts[0]->content = 'C is a beautiful language.';
$texts[1]->content = 'But also a bit dangerous.';
$texts[2]->content = 'You need to know what you are doing.';
$texts[3]->content = 'Javascript is very flexible.';
$texts[4]->content = 'It can be anything you want...';
$texts[5]->content = 'But it can lead to chaos.';
$texts[6]->content = 'CSS was meant for documents';
$texts[7]->content = 'Now we use it for applications...';
$texts[8]->content = 'PHP is an easy language to learn,';
$texts[9]->content = 'Maybe a bit too easy...';
$texts[10]->content = 'SQL is much more powerful than you think.';
$pages[0]->ownTextList = array( $texts[0], $texts[1] );
$pages[1]->ownTextList = array( $texts[2] );
$pages[2]->ownTextList = array( $texts[3] );
$pages[3]->ownTextList = array( $texts[4] );
$pages[4]->ownTextList = array( $texts[5] );
$pages[5]->ownTextList = array( $texts[6], $texts[7] );
$pages[6]->ownTextList = array( $texts[8] );
$pages[7]->ownTextList = array( $texts[9] );
$pages[8]->ownTextList = array( $texts[10] );
$books[0]->ownPageList = array( $pages[0], $pages[1] );
$books[1]->ownPageList = array( $pages[2], $pages[3], $pages[4] );
$books[2]->ownPageList = array( $pages[5] );
$books[3]->ownPageList = array( $pages[6], $pages[7] );
$books[4]->ownPageList = array( $pages[8] );
$books[0]->title = 'Diehard C';
$books[1]->title = 'Adventures in JavaScript';
$books[2]->title = 'CSS ala Picasso';
$books[3]->title = 'PHP Tips and Tricks';
$books[4]->title = 'Secrets of SQL';
$categories[0]->name = 'Programming';
$categories[1]->name = 'Design';
$categories[2]->name = 'Web Development';
$books[0]->sharedCategoryList = array( $categories[0] );
$books[1]->sharedCategoryList = array( $categories[0], $categories[2] );
$books[2]->sharedCategoryList = array( $categories[0], $categories[2], $categories[1] );
$books[3]->sharedCategoryList = array( $categories[0], $categories[2] );
$books[4]->sharedCategoryList = array( $categories[0], $categories[2] );
R::storeAll( $books );
}
/**
* A custom record-to-bean mapping function for findMulti test.
*
* @param string $parentName name of the parent bean
* @param string $childName name of the child bean
*
* @return array
*/
private function map($parentName,$childName) {
return array(
'a' => $parentName,
'b' => $childName,
'matcher' => function( $parent, $child ) use ( $parentName ) {
$property = "{$parentName}ID";
return ( $child->$property == $parent->id );
},
'do' => function( $parent, $child ) use ( $childName ) {
$list = 'own'.ucfirst( $childName ).'List';
$parent->noLoad()->{$list}[] = $child;
}
);
}
/**
* You can build your own mapping functions to remap records to bean.
* Just like the preloader once did. However now you can define the
* mapping yourself using closures. This test verifies that such a
* function would actually work.
*
* This method also tests whether empty records (resulting from LEFT JOINS for
* instance) do not produce unnecessary, empty beans.
*
* @return void
*/
public function testFindMultiExtFunc()
{
R::nuke();
$shop = R::dispense( 'shop' );
$shop2 = R::dispense( 'shop' );
$products = R::dispense( 'product', 3 );
$price = R::dispense( 'price' );
$price->tag = 5;
$products[0]->name = 'vase';
$products[1]->name = 'candle';
$products[2]->name = 'plate';
$products[1]->ownPriceList[] = $price;
$shop->ownProduct[] = $products[0];
$shop->ownProduct[] = $products[1];
$shop2->ownProduct[] = $products[2];
R::storeAll( array( $shop, $shop2 ) );
$collection = R::findMulti( 'shop,product,price', '
SELECT shop.*, product.*, price.* FROM shop
LEFT JOIN product ON product.shop_id = shop.id
LEFT JOIN price ON price.product_id = product.id
', array(), array(
'0' => $this->map( 'shop', 'product' ),
'1' => $this->map( 'product', 'price' ),
));
asrt( is_array( $collection ), TRUE );
asrt( count( $collection ), 3 );
asrt( count( $collection['shop'] ), 2 );
asrt( count( $collection['product'] ), 3 );
asrt( count( $collection['price'] ), 1 );
$shop = reset( $collection['shop'] );
asrt( count( $shop->ownProductList ), 2 );
$shop2 = next( $collection['shop'] );
asrt( count( $shop2->ownProductList ), 1 );
$candle = NULL;
foreach( $shop->ownProduct as $product ) {
if ( $product->name == 'candle' ) {
$candle = $product;
}
}
asrt( is_null( $candle ), FALSE );
asrt( count( $candle->ownPrice ), 1 );
asrt( $candle->name, 'candle' );
$price = reset( $candle->ownPrice );
asrt( (int) $price->tag, 5 );
}
/**
* Test findMuli with self-made arrays.
*
* @return void
*/
public function testFindMultiDirectArray()
{
R::nuke();
$collection = R::findMulti( 'shop,product', array(
array( 'shop__id' => 1, 'product__id' => 1, 'product__name' => 'vase', 'product__shop_id' => 1 ),
array( 'shop__id' => 1, 'product__id' => 2, 'product__name' => 'candle', 'product__shop_id' => 1 ),
array( 'shop__id' => 1, 'product__id' => 3, 'product__name' => 'plate', 'product__shop_id' => 1 ),
) );
asrt( is_array( $collection ), TRUE );
asrt( isset( $collection['shop'] ), TRUE );
asrt( isset( $collection['product'] ), TRUE );
asrt( (int) $collection['shop'][1]->id, 1 );
asrt( (int) $collection['product'][1]->id, 1 );
asrt( (int) $collection['product'][2]->id, 2 );
asrt( (int) $collection['product'][3]->id, 3 );
asrt( (int) $collection['product'][1]->shopID, 1 );
asrt( (int) $collection['product'][2]->shopID, 1 );
asrt( (int) $collection['product'][3]->shopID, 1 );
asrt( $collection['product'][1]->name, 'vase' );
asrt( $collection['product'][2]->name, 'candle' );
asrt( $collection['product'][3]->name, 'plate' );
R::nuke();
$shop = R::dispense('shop');
$shop2 = R::dispense('shop');
$products = R::dispense('product', 3);
$price = R::dispense('price');
$price->tag = 5;
$products[0]->name = 'vase';
$products[1]->name = 'candle';
$products[2]->name = 'plate';
$products[1]->ownPriceList[] = $price;
$shop->ownProduct = $products;
R::store($shop);
$collection = R::findMulti('shop,product,price', '
SELECT shop.*, product.*, price.* FROM shop
LEFT JOIN product ON product.shop_id = shop.id
LEFT JOIN price ON price.product_id = product.id
', array(), array(
'0' => $this->map('shop', 'product'),
'1' => $this->map('product', 'price'),
));
$collection = R::findMulti( 'shop,product', array(
array( 'shop__id' => 1, 'product__id' => 1, 'product__name' => 'vase', 'product__shop_id' => 1 ),
array( 'shop__id' => 1, 'product__id' => 2, 'product__name' => 'candle', 'product__shop_id' => 1 ),
array( 'shop__id' => 1, 'product__id' => 3, 'product__name' => 'plate', 'product__shop_id' => 1 ),
array( 'shop__id' => 1, 'product__id' => 2, 'product__name' => 'candle', 'product__shop_id' => 1)
), array(), array(
array(
'a' => 'shop',
'b' => 'product',
'matcher' => function( $a, $b ) { return ( $b->shopID == $a->id ); },
'do' => function( $a, $b ) { return $a->noLoad()->ownProductList[] = $b; }
)
) );
asrt( is_array( $collection ), TRUE );
asrt( isset( $collection['shop'] ), TRUE );
asrt( isset( $collection['product'] ), TRUE );
asrt( (int) $collection['shop'][1]->id, 1 );
asrt( (int) $collection['product'][1]->id, 1 );
asrt( (int) $collection['product'][2]->id, 2 );
asrt( (int) $collection['product'][3]->id, 3 );
asrt( (int) $collection['product'][1]->shopID, 1 );
asrt( (int) $collection['product'][2]->shopID, 1 );
asrt( (int) $collection['product'][3]->shopID, 1 );
asrt( $collection['product'][1]->name, 'vase' );
asrt( $collection['product'][2]->name, 'candle' );
asrt( $collection['product'][3]->name, 'plate' );
asrt( isset( $collection['shop'][1]->ownProductList ), TRUE );
asrt( is_array( $collection['shop'][1]->ownProductList ), TRUE );
asrt( count( $collection['shop'][1]->ownProductList ), 3 );
asrt( $collection['shop'][1]->ownProductList[0]->name, 'vase' );
asrt( $collection['shop'][1]->ownProductList[1]->name, 'candle' );
asrt( $collection['shop'][1]->ownProductList[2]->name, 'plate' );
}
/**
* Test findMulti() with manual crafted fields.
*
* @return void
*/
public function testFindMultiDIY()
{
R::nuke();
$movie = R::dispense( 'movie' );
$review = R::dispense( 'review' );
$movie->ownReviewList[] = $review;
$review->stars = 5;
$movie->title = 'Gambit';
R::store( $movie );
$stuff = R::findMulti( 'movie,review', 'SELECT
movie.id AS movie__id,
movie.title AS movie__title,
review.id AS review__id,
review.stars AS review__stars,
review.movie_id AS review__movie_id
FROM movie
LEFT JOIN review ON review.movie_id = movie.id
' );
asrt( count( $stuff ), 2 );
asrt( isset( $stuff['movie'] ), TRUE );
asrt( isset( $stuff['review'] ), TRUE );
asrt( is_array( $stuff['movie'] ), TRUE );
asrt( is_array( $stuff['review'] ), TRUE );
asrt( count( $stuff['movie'] ), 1 );
asrt( count( $stuff['review'] ), 1 );
$movie = reset( $stuff['movie'] );
asrt( $movie->title, 'Gambit' );
$review = reset( $stuff['review'] );
asrt( (int) $review->stars, 5 );
R::nuke();
$movie = R::dispense( 'movie' );
$review = R::dispense( 'review' );
$movie->ownReviewList[] = $review;
$review->stars = 5;
$movie->title = 'Gambit';
R::store( $movie );
$stuff = R::findMulti( array( 'movie', 'review' ), 'SELECT
movie.id AS movie__id,
movie.title AS movie__title,
review.id AS review__id,
review.stars AS review__stars,
review.movie_id AS review__movie_id
FROM movie
LEFT JOIN review ON review.movie_id = movie.id
' );
asrt( count( $stuff ), 2 );
asrt( isset( $stuff['movie'] ), TRUE );
asrt( isset( $stuff['review'] ), TRUE );
asrt( is_array( $stuff['movie'] ), TRUE );
asrt( is_array( $stuff['review'] ), TRUE );
asrt( count( $stuff['movie'] ), 1 );
asrt( count( $stuff['review'] ), 1 );
$movie = reset( $stuff['movie'] );
asrt( $movie->title, 'Gambit' );
$review = reset( $stuff['review'] );
asrt( (int) $review->stars, 5 );
}
/**
* Test findMulti(). Basic version.
*
* @return void
*/
public function testFindMulti()
{
$book = R::dispense( 'book' );
$book->title = 'My Book';
$book->ownPageList = R::dispense('page', 3);
$no = 1;
foreach( $book->ownPageList as $page ) {
$page->num = $no++;
}
R::store( $book );
$collection = R::findMulti('book,page', '
SELECT book.*, page.* FROM book
LEFT JOIN page ON page.book_id = book.id
');
asrt( count( $collection ), 2 );
asrt( isset( $collection['book'] ), TRUE );
asrt( isset( $collection['page'] ), TRUE );
asrt( count( $collection['book'] ), 1 );
asrt( count( $collection['page'] ), 3 );
foreach( $collection['book'] as $bean ) asrt( ( $bean instanceof OODBBean ), TRUE );
foreach( $collection['page'] as $bean ) asrt( ( $bean instanceof OODBBean ), TRUE );
$book = reset( $collection['book'] );
asrt( $book->title, 'My Book' );
$no = 1;
foreach( $collection['page'] as $page ) asrt( (int) $page->num, $no++ );
R::nuke();
$book->noLoad()->ownPageList = $collection['page'];
asrt( count( $book->ownPageList ), 3 );
}
/**
* Tests the complex use case for findMulti().
*
* @return void
*/
public function testMultiAdvanced()
{
$this->insertBookData();
$collection = R::findMulti( 'book,page,text,category', '
SELECT book.*, page.*, text.*, category.*
FROM book
LEFT JOIN page ON page.book_id = book.id
LEFT JOIN text ON text.page_id = page.id
LEFT JOIN book_category ON book_category.book_id = book.id
LEFT JOIN category ON book_category.category_id = category.id
' );
asrt( count( $collection ), 4 );
asrt( isset( $collection['book'] ), TRUE );
asrt( isset( $collection['page'] ), TRUE );
asrt( isset( $collection['text'] ), TRUE );
asrt( isset( $collection['category'] ), TRUE );
asrt( count( $collection['book'] ), 5 );
asrt( count( $collection['page'] ), 9 );
asrt( count( $collection['text'] ), 11 );
asrt( count( $collection['category'] ), 3 );
foreach( $collection['book'] as $bean ) asrt( ( $bean instanceof OODBBean ), TRUE );
foreach( $collection['page'] as $bean ) asrt( ( $bean instanceof OODBBean ), TRUE );
foreach( $collection['text'] as $bean ) asrt( ( $bean instanceof OODBBean ), TRUE );
foreach( $collection['category'] as $bean ) asrt( ( $bean instanceof OODBBean ), TRUE );
foreach( $collection['book'] as $book ) $titles[] = $book->title;
asrt( in_array( 'Diehard C', $titles ), TRUE );
asrt( in_array( 'Adventures in JavaScript', $titles ), TRUE );
asrt( in_array( 'CSS ala Picasso', $titles ), TRUE );
asrt( in_array( 'PHP Tips and Tricks', $titles ), TRUE );
asrt( in_array( 'Secrets of SQL', $titles ), TRUE );
$collection = R::findMulti( 'book,page,text,category,book_category', '
SELECT book.*, page.*, text.*, category.*, book_category.*
FROM book
LEFT JOIN page ON page.book_id = book.id
LEFT JOIN text ON text.page_id = page.id
LEFT JOIN book_category ON book_category.book_id = book.id
LEFT JOIN category ON book_category.category_id = category.id
WHERE category_id > ?
ORDER BY book.title ASC
', array( 0 ), array(
array(
'b'=>'page',
'a'=>'text',
'do' => function( $a, $b ) {
$b->noLoad()->ownTextList[] = $a;
$b->clearHistory();
},
'matcher' => function( $a, $b ){ return ($a->page_id == $b->id); }
),
array(
'b'=>'book',
'a'=>'page',
'do' => function( $a, $b ) {
$b->noLoad()->ownPageList[] = $a;
$b->clearHistory();
},
'matcher' => function( $a, $b ){ return ($a->book_id == $b->id); }
),
array(
'b' => 'category',
'a' => 'book',
'do' => function($a, $b) {
$a->noLoad()->sharedCategoryList[] = $b;
$a->clearHistory();
},
'matcher' => function( $a, $b, $beans ) {
foreach( $beans['book_category'] as $bean ) {
if ($bean->book_id == $a->id && $bean->category_id == $b->id) return TRUE;
}
return FALSE;
}
),
)
);
$books = $collection['book'];
$book = reset( $books );
asrt( $book->title, 'Adventures in JavaScript' );
R::nuke();
asrt( count( $book->ownPageList ), 3 );
$page = reset( $book->ownPageList );
asrt( count( $page->ownTextList ), 1 );
asrt( count( $book->sharedCategoryList ), 2);
$categories = array();
foreach( $book->sharedCategoryList as $category ) {
$categories[] = $category->name;
}
sort( $categories );
asrt( implode( ',', $categories ), 'Programming,Web Development' );
$book = next( $books );
asrt( $book->title, 'CSS ala Picasso' );
asrt( count( $book->ownPage ), 1 );
$page = reset( $book->ownPage );
asrt( count( $page->ownTextList ), 2 );
$texts = array();
foreach( $page->ownTextList as $text ) $texts[] = $text->content;
asrt( in_array( 'Now we use it for applications...', $texts ), TRUE );
$categories = array();
foreach( $book->sharedCategoryList as $category ) {
$categories[] = $category->name;
}
sort( $categories );
asrt( implode( ',', $categories ), 'Design,Programming,Web Development' );
$book = next( $books );
asrt( $book->title, 'Diehard C' );
asrt( count( $book->ownPageList ), 2 );
$page = reset( $book->ownPageList );
asrt( count( $page->ownTextList ), 2 );
$page = next( $book->ownPageList );
asrt( count( $page->ownTextList ), 1 );
$categories = array();
foreach( $book->sharedCategoryList as $category ) {
$categories[] = $category->name;
}
sort( $categories );
asrt( implode( ',', $categories ), 'Programming' );
//should have no effect, nothing should have changed
R::storeAll($books);
asrt( R::count('book'), 0 );
asrt( R::count('page'), 0 );
asrt( R::count('text'), 0 );
}
/**
* Test forming IN-clause using genSlots and flat.
*
* @return void
*/
public function testINClause()
{
list( $flowers, $shop ) = R::dispenseAll( 'flower*4,shop' );
$flowers[0]->color = 'red';
$flowers[1]->color = 'yellow';
$flowers[2]->color = 'blue';
$flowers[3]->color = 'purple';
$flowers[0]->price = 10;
$flowers[1]->price = 15;
$flowers[2]->price = 20;
$flowers[3]->price = 25;
$shop->xownFlowerList = $flowers;
R::store( $shop );
$colors = array( 'red', 'yellow' );
$result = $this->getColors( R::find( 'flower', ' color IN ('.R::genSlots( $colors ).' ) AND price < ?' , R::flat( array( $colors, 100 ) ) ) );
asrt( $result, 'red,yellow' );
$colors = array( 'red', 'yellow' );
$result = $this->getColors( R::find( 'flower', ' color IN ('.R::genSlots( $colors ).' ) AND price < ?' , R::flat( array( $colors, 10 ) ) ) );
asrt( $result, '' );
$colors = array( 'red', 'yellow' );
$result = $this->getColors( R::find( 'flower', ' color IN ('.R::genSlots( $colors ).' ) AND price < ?' , R::flat( array( $colors, 15 ) ) ) );
asrt( $result, 'red' );
asrt( json_encode( R::flat( array( 'a', 'b', 'c' ) ) ), '["a","b","c"]' );
asrt( json_encode( R::flat( array( 'a', array( 'b' ), 'c' ) ) ), '["a","b","c"]' );
asrt( json_encode( R::flat( array( 'a', array( 'b', array( 'c' ) ) ) ) ), '["a","b","c"]' );
asrt( json_encode( R::flat( array( array( 'a', array( 'b', array( array( 'c' ) ) ) ) ) ) ), '["a","b","c"]' );
asrt( json_encode( R::flat( array( 'a', 'b', 'c', array() ) ) ), '["a","b","c"]' );
asrt( genslots( array( 1, 2 ) ), '?,?' );
asrt( json_encode( array_flatten( array( array( 'a', array( 'b', array( array( 'c' ) ) ) ) ) ) ), '["a","b","c"]' );
asrt( genslots( array( 1, 2 ), 'IN (%s) AND' ), 'IN (?,?) AND' );
asrt( genslots( array(), ' IN (%s) AND ' ), '' );
$colors = array( 'blue', 'purple', 'red' );
$flowers = R::find( 'flower', genslots( $colors, ' color IN (%s) AND ' ).' price > ? ', array_flatten( array( $colors, 11 ) ) );
asrt( $this->getColors( $flowers ), 'blue,purple' );
$flowers = R::find( 'flower', genslots( array(), ' color IN (%s) AND ' ).' price > ? ', array_flatten( array( array(), 11 ) ) );
asrt( $this->getColors( $flowers ), 'blue,purple,yellow' );
$flowers = R::find( 'flower', ' id > 0 AND '.genslots( $colors, ' color IN (%s) AND ' ).' price > ? ', array_flatten( array( $colors, 11 ) ) );
asrt( $this->getColors( $flowers ), 'blue,purple' );
$flowers = R::find( 'flower', ' id > 0 AND '.genslots( array(), ' color IN (%s) AND ' ).' price > ? ', array_flatten( array( array(), 11 ) ) );
asrt( $this->getColors( $flowers ), 'blue,purple,yellow' );
}
/**
* Test findLike.
*
* @return void
*/
public function testFindLike2()
{
list( $flowers, $shop ) = R::dispenseAll( 'flower*4,shop' );
$flowers[0]->color = 'red';
$flowers[1]->color = 'yellow';
$flowers[2]->color = 'blue';
$flowers[3]->color = 'purple';
$flowers[0]->price = 10;
$flowers[1]->price = 15;
$flowers[2]->price = 20;
$flowers[3]->price = 25;
$shop->xownFlowerList = $flowers;
R::store( $shop );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array( 'red', 'yellow' ) ), ' price < 20' ) ), 'red,yellow' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array() ), '' ) ), 'blue,purple,red,yellow' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array() ) ) ), 'blue,purple,red,yellow' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array('blue') ), ' OR price = 25' ) ), 'blue,purple' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array() ), ' price < 25' ) ), 'blue,red,yellow' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array() ), ' price < 20' ) ), 'red,yellow' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array() ), ' ORDER BY color DESC' ), TRUE ), 'yellow,red,purple,blue' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array() ), ' ORDER BY color LIMIT 1' ) ), 'blue' );
asrt( $this->getColors( R::findLike( 'flower', array( 'color' => array( 'yellow', 'blue' ) ), ' ORDER BY color ASC LIMIT 1' ) ), 'blue' );
}
/**
* Tests the findOrCreate method.
*
* @return void
*/
public function testFindOrCreate()
{
R::nuke();
$book = R::findOrCreate( 'book', array( 'title' => 'my book', 'price' => 50 ) );
asrt( ( $book instanceof OODBBean ), TRUE );
$id = $book->id;
$book = R::findOrCreate( 'book', array( 'title' => 'my book', 'price' => 50 ) );
asrt( $book->id, $id );
asrt( $book->title, 'my book' );
asrt( (int) $book->price, 50 );
}
/**
* Tests the findLike method.
*
* @return void
*/
public function testFindLike()
{
R::nuke();
$book = R::dispense( array(
'_type' => 'book',
'title' => 'my book',
'price' => 80
) );
R::store( $book );
$book = R::dispense( array(
'_type' => 'book',
'title' => 'other book',
'price' => 80
) );
R::store( $book );
$books = R::findLike( 'book', array( 'price' => 80 ) );
asrt( count( $books ), 2 );
foreach( $books as $book ) {
asrt( $book->getMeta( 'type' ), 'book' );
}
$books = R::findLike( 'book' );
asrt( count( $books ), 2 );
$books = R::findLike( 'book', array( 'title' => 'my book' ) );
asrt( count( $books ), 1 );
$books = R::findLike( 'book', array( 'title' => array( 'my book', 'other book' ) ) );
asrt( count( $books ), 2 );
$books = R::findLike( 'book', array( 'title' => 'strange book') );
asrt( is_array( $books ), TRUE );
asrt( count( $books ), 0 );
$books = R::findLike( 'magazine' );
asrt( is_array( $books ), TRUE );
asrt( count( $books ), 0 );
}
/**
* Test whether findOne gets a LIMIT 1
* clause.
*
* @return void
*/
public function testFindOneLimitOne()
{
R::nuke();
list( $book1, $book2 ) = R::dispense( 'book', 2 );
$book1->title = 'a';
$book2->title = 'b';
R::storeAll( array( $book1, $book2 ) );
$logger = R::debug( 1, 1 );
$logger->clear();
$found = R::findOne( 'book' );
asrt( count( $logger->grep('LIMIT 1') ), 1 );
asrt( ( $found instanceof \RedBeanPHP\OODBBean ), TRUE );
$logger->clear();
$found = R::findOne( 'book', ' title = ? ', array( 'a' ) );
asrt( count( $logger->grep('LIMIT 1') ), 1 );
asrt( ( $found instanceof \RedBeanPHP\OODBBean ), TRUE );
$logger->clear();
$found = R::findOne( 'book', ' title = ? LIMIT 1', array( 'b' ) );
asrt( count( $logger->grep('LIMIT 1') ), 1 );
asrt( ( $found instanceof \RedBeanPHP\OODBBean ), TRUE );
$found = R::findOne( 'book', ' title = ? LIMIT 2', array( 'b' ) );
asrt( count( $logger->grep('LIMIT 2') ), 1 );
asrt( ( $found instanceof \RedBeanPHP\OODBBean ), TRUE );
}
/**
* Begin testing.
* This method runs the actual test pack.
*
* @return void
*/
public function testFinding() {
$toolbox = R::getToolBox();
$adapter = $toolbox->getDatabaseAdapter();
$writer = $toolbox->getWriter();
$redbean = $toolbox->getRedBean();
$pdo = $adapter->getDatabase();
$a = new AssociationManager( $toolbox );
$page = $redbean->dispense( "page" );
$page->name = "John's page";
$idpage = $redbean->store( $page );
$page2 = $redbean->dispense( "page" );
$page2->name = "John's second page";
$idpage2 = $redbean->store( $page2 );
$a->associate( $page, $page2 );
$pageOne = $redbean->dispense( "page" );
$pageOne->name = "one";
$pageMore = $redbean->dispense( "page" );
$pageMore->name = "more";
$pageEvenMore = $redbean->dispense( "page" );
$pageEvenMore->name = "evenmore";
$pageOther = $redbean->dispense( "page" );
$pageOther->name = "othermore";
set1toNAssoc( $a, $pageOther, $pageMore );
set1toNAssoc( $a, $pageOne, $pageMore );
set1toNAssoc( $a, $pageOne, $pageEvenMore );
asrt( count( $redbean->find( "page", array(), " name LIKE '%more%' ", array() ) ), 3 );
asrt( count( $redbean->find( "page", array(), " name LIKE :str ", array( ":str" => '%more%' ) ) ), 3 );
asrt( count( $redbean->find( "page", array(), array( " name LIKE :str ", array( ":str" => '%more%' ) ) ) ), 3 );
asrt( count( $redbean->find( "page", array(), " name LIKE :str ", array( ":str" => '%mxore%' ) ) ), 0 );
asrt( count( $redbean->find( "page", array( "id" => array( 2, 3 ) ) ) ), 2 );
$bean = $redbean->dispense( "wine" );
$bean->name = "bla";
for ( $i = 0; $i < 10; $i++ ) {
$redbean->store( $bean );
}
$redbean->find( "wine", array( "id" => 5 ) ); // Finder:where call OODB::convertToBeans
$bean2 = $redbean->load( "anotherbean", 5 );
asrt( $bean2->id, 0 );
$keys = $adapter->getCol( "SELECT id FROM page WHERE " . $writer->esc( 'name' ) . " LIKE '%John%'" );
asrt( count( $keys ), 2 );
$pages = $redbean->batch( "page", $keys );
asrt( count( $pages ), 2 );
$p = R::findLast( 'page' );
pass();
$row = R::getRow( 'select * from page ' );
asrt( is_array( $row ), TRUE );
asrt( isset( $row['name'] ), TRUE );
// Test findAll -- should not throw an exception
asrt( count( R::findAll( 'page' ) ) > 0, TRUE );
asrt( count( R::findAll( 'page', ' ORDER BY id ' ) ) > 0, TRUE );
$beans = R::findOrDispense( "page" );
asrt( count( $beans ), 6 );
asrt( is_null( R::findLast( 'nothing' ) ), TRUE );
try {
R::find( 'bean', ' id > 0 ', 'invalid bindings argument' );
fail();
} catch ( RedException $exception ) {
pass();
}
}
/**
* Test tree traversal with searchIn().
*
* @return void
*/
public function testTreeTraversal()
{
testpack( 'Test Tree Traversal' );
R::nuke();
$page = R::dispense( 'page', 10 );
//Setup the test data for this series of tests
$i = 0;
foreach( $page as $pageItem ) {
$pageItem->name = 'page' . $i;
$pageItem->number = $i;
$i++;
R::store( $pageItem );
}
$page[0]->ownPage = array( $page[1], $page[2] );
$page[1]->ownPage = array( $page[3], $page[4] );
$page[3]->ownPage = array( $page[5] );
$page[5]->ownPage = array( $page[7] );
$page[9]->document = $page[8];
$page[9]->book = R::dispense('book');
R::store( $page[9] );
$id = R::store( $page[0] );
$book = $page[9]->book;
}
/**
* Test find and export.
*
* @return void
*/
public function testFindAndExport()
{
R::nuke();
$pages = R::dispense( 'page', 3 );
$i = 1;
foreach( $pages as $page ) {
$page->pageNumber = $i++;
}
R::storeAll( $pages );
$pages = R::findAndExport( 'page' );
asrt( is_array( $pages ), TRUE );
asrt( isset( $pages[0] ), TRUE );
asrt( is_array( $pages[0] ), TRUE );
asrt( count( $pages ), 3 );
}
/**
* Test error handling of SQL states.
*
* @return void
*/
public function testFindError()
{
R::freeze( FALSE );
$page = R::dispense( 'page' );
$page->title = 'abc';
R::store( $page );
//Column does not exist, in fluid mode no error!
try {
R::find( 'page', ' xtitle = ? ', array( 'x' ) );
pass();
} catch ( SQL $e ) {
fail();
}
//Table does not exist, in fluid mode no error!
try {
R::find( 'pagex', ' title = ? ', array( 'x' ) );
pass();
} catch ( SQL $e ) {
fail();
}
//Syntax error, error in fluid mode if possible to infer from SQLSTATE (MySQL/Postgres)
try {
R::find( 'page', ' invalid SQL ' );
//In SQLite only get HY000 - not very descriptive so suppress more errors in fluid mode then.
if (
$this->currentlyActiveDriverID === 'sqlite'
|| $this->currentlyActiveDriverID === 'CUBRID' ) {
pass();
} else {
fail();
}
} catch ( SQL $e ) {
pass();
}
//Frozen, always error...
R::freeze( TRUE );
//Column does not exist, in frozen mode error!
try {
R::find( 'page', ' xtitle = ? ', array( 'x' ) );
fail();
} catch ( SQL $e ) {
pass();
}
//Table does not exist, in frozen mode error!
try {
R::find( 'pagex', ' title = ? ', array( 'x' ) );
fail();
} catch ( SQL $e ) {
pass();
}
//Syntax error, in frozen mode error!
try {
R::find( 'page', ' invalid SQL ' );
fail();
} catch ( SQL $e ) {
pass();
}
R::freeze( FALSE );
}
}