3 namespace RedUNIT\Blackhole;
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\RedException as RedException;
13 * RedBeanPHP offers some methods to import arrays into
14 * beans. For instance using the dispense() method. This
15 * test suite checks whether RedBeanPHP can correctly convert
16 * array structures to beans and also checks the expected effects
17 * on the taint flags. This test suite further tests the 'simple'
18 * single bean import() function, the inject() function (bean-to-bean) and
19 * array access (because this is somehow related).
21 * @file RedUNIT/Blackhole/Import.php
22 * @desc Tests basic bean importing features.
23 * @author Gabor de Mooij and the RedBeanPHP Community
24 * @license New BSD/GPLv2
26 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
27 * This source file is subject to the New BSD/GPLv2 License that is bundled
28 * with this source code in the file license.txt.
31 class Import extends Blackhole
34 * Test multi array dispense import.
38 public function testMultiRecurImport()
40 $books = R::dispense( array(
41 array( '_type' => 'book', 'title' => 'book one' ),
42 array( '_type' => 'book', 'title' => 'book two' ),
44 asrt( is_array( $books ), TRUE );
45 asrt( count( $books ), 2 );
46 $book = reset( $books );
47 asrt( ( $book instanceof OODBBean ), TRUE );
48 asrt( $book->title, 'book one' );
49 $book = next( $books );
50 asrt( ( $book instanceof OODBBean ), TRUE );
51 asrt( $book->title, 'book two' );
55 * Test recursive imports (formely known as R::graph).
59 public function testRecursiveImport()
64 'title'=>'The magic book',
65 'ownPageList' => array(
68 'content' => 'magic potions',
72 'content' => 'magic spells',
77 $id = R::store( $book );
78 $book = R::load( 'book', $id );
79 asrt( $book->title, 'The magic book' );
80 $pages = $book->with(' ORDER BY content ASC ')->ownPageList;
81 asrt( count($pages), 2 );
82 $page1 = array_shift( $pages );
83 asrt( $page1->content, 'magic potions' );
84 $page2 = array_shift( $pages );
85 asrt( $page2->content, 'magic spells' );
90 'title'=>'The magic book',
97 'name' => 'Dr. Creepy'
99 'ownPageList' => array(
102 'content' => 'magic potions',
103 'ownRecipe' => array(
104 'a' => array('_type'=>'recipe', 'name'=>'Invisibility Salad'),
105 'b' => array('_type'=>'recipe', 'name'=>'Soup of Madness'),
106 'c' => array('_type'=>'recipe', 'name'=>'Love cake'),
111 'content' => 'magic spells',
114 'sharedCategory' => array(
116 '_type' => 'category',
117 'label' => 'wizardry'
122 $id = R::store( $book );
123 $book = R::load( 'book', $id );
124 asrt( $book->title, 'The magic book' );
125 $pages = $book->with(' ORDER BY content ASC ')->ownPageList;
126 asrt( count($pages), 2 );
127 $page1 = array_shift( $pages );
128 asrt( $page1->content, 'magic potions' );
129 $page2 = array_shift( $pages );
130 asrt( $page2->content, 'magic spells' );
131 $recipes = $page1->with(' ORDER BY name ASC ')->ownRecipeList;
132 asrt( count( $recipes ), 3 );
133 $recipe1 = array_shift( $recipes );
134 asrt( $recipe1->name, 'Invisibility Salad' );
135 $recipe2 = array_shift( $recipes );
136 asrt( $recipe2->name, 'Love cake' );
137 $recipe3 = array_shift( $recipes );
138 asrt( $recipe3->name, 'Soup of Madness' );
139 $categories = $book->sharedCategoryList;
140 asrt( count($categories), 1 );
141 $category = reset( $categories );
142 asrt( $category->label, 'wizardry' );
143 asrt( $book->author->name, 'Dr. Evil' );
144 asrt( $book->fetchAs('author')->coAuthor->name, 'Dr. Creepy' );
146 $list = R::dispense( array() );
148 asrt( is_array( $list ), TRUE );
149 asrt( count( $list ), 0 );
150 } catch ( RedException $ex ) {
154 R::dispense( array( array() ) );
156 } catch ( RedException $ex ) {
160 R::dispense( array( 'a' ) );
162 } catch ( RedException $ex ) {
166 R::dispense( array( 'property' => 'value' ) );
168 } catch ( RedException $ex ) {
174 * Test import from and tainted.
178 public function testImportFromAndTainted()
180 testpack( 'Test importFrom() and Tainted' );
181 $bean = R::dispense( 'bean' );
184 asrt( $bean->getMeta( 'tainted' ), TRUE );
186 asrt( $bean->getMeta( 'tainted' ), FALSE );
187 $copy = R::dispense( 'bean' );
189 $copy = R::load( 'bean', $copy->id );
190 asrt( $copy->getMeta( 'tainted' ), FALSE );
191 $copy->import( array( 'name' => 'xyz' ) );
192 asrt( $copy->getMeta( 'tainted' ), TRUE );
193 $copy->setMeta( 'tainted', FALSE );
194 asrt( $copy->getMeta( 'tainted' ), FALSE );
195 $copy->importFrom( $bean );
196 asrt( $copy->getMeta( 'tainted' ), TRUE );
197 testpack( 'Test basic import() feature.' );
198 $bean = new OODBBean;
199 $bean->import( array( "a" => 1, "b" => 2 ) );
202 $bean->import( array( "a" => 3, "b" => 4 ), "a,b" );
205 $bean->import( array( "a" => 5, "b" => 6 ), " a , b " );
208 $bean->import( array( "a" => 1, "b" => 2 ) );
209 testpack( 'Test inject() feature.' );
210 $coffee = R::dispense( 'coffee' );
212 $coffee->liquid = 'black';
213 $cup = R::dispense( 'cup' );
214 $cup->color = 'green';
215 // Pour coffee in cup
216 $cup->inject( $coffee );
217 // Do we still have our own property?
218 asrt( $cup->color, 'green' );
219 // Did we pour the liquid in the cup?
220 asrt( $cup->liquid, 'black' );
221 // Id should not be transferred
226 * Test import using array access functions
230 public function testArrayAccess()
232 $book = R::dispense( 'book' );
233 $book->isAwesome = TRUE;
234 asrt( isset( $book->isAwesome ), TRUE );
235 $book = R::dispense( 'book' );
236 $book['isAwesome'] = TRUE;
237 asrt( isset( $book->isAwesome ), TRUE );
238 $book = R::dispense( 'book' );
239 $book['xownPageList'] = R::dispense( 'page', 2 );
240 asrt( isset( $book->ownPage ), TRUE );
241 asrt( isset( $book->xownPage ), TRUE );
242 asrt( isset( $book->ownPageList ), TRUE );
243 asrt( isset( $book->xownPageList ), TRUE );
244 $book = R::dispense( 'book' );
245 $book['ownPageList'] = R::dispense( 'page', 2 );
246 asrt( isset( $book->ownPage ), TRUE );
247 asrt( isset( $book->xownPage ), TRUE );
248 asrt( isset( $book->ownPageList ), TRUE );
249 asrt( isset( $book->xownPageList ), TRUE );
250 $book = R::dispense( 'book' );
251 $book['xownPage'] = R::dispense( 'page', 2 );
252 asrt( isset( $book->ownPage ), TRUE );
253 asrt( isset( $book->xownPage ), TRUE );
254 asrt( isset( $book->ownPageList ), TRUE );
255 asrt( isset( $book->xownPageList ), TRUE );
256 $book = R::dispense( 'book' );
257 $book['ownPage'] = R::dispense( 'page', 2 );
258 asrt( isset( $book->ownPage ), TRUE );
259 asrt( isset( $book->xownPage ), TRUE );
260 asrt( isset( $book->ownPageList ), TRUE );
261 asrt( isset( $book->xownPageList ), TRUE );
262 $book = R::dispense( 'book' );
263 $book['sharedTag'] = R::dispense( 'tag', 2 );
264 asrt( isset( $book->sharedTag ), TRUE );
265 asrt( isset( $book->sharedTagList ), TRUE );
266 $book = R::dispense( 'book' );
267 $book['sharedTagList'] = R::dispense( 'tag', 2 );
268 asrt( isset( $book->sharedTag ), TRUE );
269 asrt( isset( $book->sharedTagList ), TRUE );