Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Batch.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\RedException\SQL as SQL;
9
10 /**
11  * Batch
12  *
13  * Test whether we can load a group of beans in one go, i.e.
14  * the batch loading functionality of RedBeanPHP, also in combination
15  * with aliasing.
16  *
17  * @file    RedUNIT/Base/Batch.php
18  * @desc    Tests batch loading of beans, i.e. loading large collections of beans in optimized way.
19  * @author  Gabor de Mooij and the RedBeanPHP Community
20  * @license New BSD/GPLv2
21  *
22  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
23  * This source file is subject to the New BSD/GPLv2 License that is bundled
24  * with this source code in the file license.txt.
25  */
26 class Batch extends Base
27 {
28
29         /**
30          * Begin testing.
31          * This method runs the actual test pack.
32          *
33          * @return void
34          */
35         public function testBatch()
36         {
37                 R::freeze( FALSE );
38                 $toolbox = R::getToolBox();
39                 $adapter = $toolbox->getDatabaseAdapter();
40                 $writer  = $toolbox->getWriter();
41                 $redbean = $toolbox->getRedBean();
42                 $pdo     = $adapter->getDatabase();
43
44                 $page = $redbean->dispense( "page" );
45
46                 $page->name   = "page no. 1";
47                 $page->rating = 1;
48
49                 $id1 = $redbean->store( $page );
50
51                 $page = $redbean->dispense( "page" );
52
53                 $page->name = "page no. 2";
54
55                 $id2 = $redbean->store( $page );
56
57                 $batch = $redbean->batch( "page", array( $id1, $id2 ) );
58
59                 asrt( count( $batch ), 2 );
60                 asrt( $batch[$id1]->getMeta( "type" ), "page" );
61                 asrt( $batch[$id2]->getMeta( "type" ), "page" );
62                 asrt( (int) $batch[$id1]->id, $id1 );
63                 asrt( (int) $batch[$id2]->id, $id2 );
64
65                 $book = $redbean->dispense( "book" );
66
67                 $book->name = "book 1";
68
69                 $redbean->store( $book );
70
71                 $book = $redbean->dispense( "book" );
72
73                 $book->name = "book 2";
74
75                 $redbean->store( $book );
76
77                 $book = $redbean->dispense( "book" );
78
79                 $book->name = "book 3";
80
81                 $redbean->store( $book );
82
83                 $books = $redbean->batch( "book", $adapter->getCol( "SELECT id FROM book" ) );
84
85                 asrt( count( $books ), 3 );
86
87                 $a = $redbean->batch( 'book', 9919 );
88
89                 asrt( is_array( $a ), TRUE );
90                 asrt( count( $a ), 0 );
91                 $a = $redbean->batch( 'triangle', 1 );
92
93                 asrt( is_array( $a ), TRUE );
94                 asrt( count( $a ), 0 );
95
96                 R::freeze( TRUE );
97
98                 $a = $redbean->batch( 'book', 9919 );
99
100                 asrt( is_array( $a ), TRUE );
101                 asrt( count( $a ), 0 );
102                 try {
103                         $a = $redbean->batch( 'triangle', 1 );
104                         fail();
105                 } catch(SQL $e) {
106                         pass();
107                 }
108                 R::freeze( FALSE );
109                 asrt( R::wipe( 'spaghettimonster' ), FALSE );
110         }
111
112         /**
113          * Test missing bean scenarios.
114          *
115          * @return void
116          */
117         public function testMissingBeans()
118         {
119                 testpack( 'deal with missing beans' );
120
121                 $id      = R::store( R::dispense( 'beer' ) );
122                 $bottles = R::batch( 'beer', array( $id, $id + 1, $id + 2 ) );
123
124                 asrt( count( $bottles ), 3 );
125                 asrt( (int) $bottles[$id]->id, (int) $id );
126                 asrt( (int) $bottles[$id + 1]->id, 0 );
127                 asrt( (int) $bottles[$id + 2]->id, 0 );
128         }
129
130         /**
131          * Test batch alias loadAll.
132          *
133          * @return void
134          */
135         public function testBatchAliasLoadAll()
136         {
137                 $ids = R::storeAll( R::dispense( 'page', 2 ) );
138                 $pages = R::loadAll( 'page', $ids );
139                 asrt( is_array( $pages ), true );
140                 asrt( count( $pages ), 2 );
141                 asrt( ( $pages[$ids[0]] instanceof OODBBean ), true );
142         }
143 }