3 namespace RedUNIT\Base;
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;
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
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
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.
26 class Batch extends Base
31 * This method runs the actual test pack.
35 public function testBatch()
38 $toolbox = R::getToolBox();
39 $adapter = $toolbox->getDatabaseAdapter();
40 $writer = $toolbox->getWriter();
41 $redbean = $toolbox->getRedBean();
42 $pdo = $adapter->getDatabase();
44 $page = $redbean->dispense( "page" );
46 $page->name = "page no. 1";
49 $id1 = $redbean->store( $page );
51 $page = $redbean->dispense( "page" );
53 $page->name = "page no. 2";
55 $id2 = $redbean->store( $page );
57 $batch = $redbean->batch( "page", array( $id1, $id2 ) );
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 );
65 $book = $redbean->dispense( "book" );
67 $book->name = "book 1";
69 $redbean->store( $book );
71 $book = $redbean->dispense( "book" );
73 $book->name = "book 2";
75 $redbean->store( $book );
77 $book = $redbean->dispense( "book" );
79 $book->name = "book 3";
81 $redbean->store( $book );
83 $books = $redbean->batch( "book", $adapter->getCol( "SELECT id FROM book" ) );
85 asrt( count( $books ), 3 );
87 $a = $redbean->batch( 'book', 9919 );
89 asrt( is_array( $a ), TRUE );
90 asrt( count( $a ), 0 );
91 $a = $redbean->batch( 'triangle', 1 );
93 asrt( is_array( $a ), TRUE );
94 asrt( count( $a ), 0 );
98 $a = $redbean->batch( 'book', 9919 );
100 asrt( is_array( $a ), TRUE );
101 asrt( count( $a ), 0 );
103 $a = $redbean->batch( 'triangle', 1 );
109 asrt( R::wipe( 'spaghettimonster' ), FALSE );
113 * Test missing bean scenarios.
117 public function testMissingBeans()
119 testpack( 'deal with missing beans' );
121 $id = R::store( R::dispense( 'beer' ) );
122 $bottles = R::batch( 'beer', array( $id, $id + 1, $id + 2 ) );
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 );
131 * Test batch alias loadAll.
135 public function testBatchAliasLoadAll()
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 );