Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Blackhole / Import.php
1 <?php
2
3 namespace RedUNIT\Blackhole;
4
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\RedException as RedException;
9
10 /**
11  * Import
12  *
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).
20  *
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
25  *
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.
29  */
30
31 class Import extends Blackhole
32 {
33         /**
34          * Test multi array dispense import.
35          *
36          * @return void
37          */
38         public function testMultiRecurImport()
39         {
40                 $books = R::dispense( array(
41                         array( '_type' => 'book', 'title' => 'book one' ),
42                         array( '_type' => 'book', 'title' => 'book two' ),
43                 ) );
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' );
52         }
53
54         /**
55          * Test recursive imports (formely known as R::graph).
56          *
57          * @return void
58          */
59         public function testRecursiveImport()
60         {
61                 $book = R::dispense(
62                         array(
63                                 '_type'=>'book',
64                                 'title'=>'The magic book',
65                                 'ownPageList' => array(
66                                          array(
67                                                 '_type' => 'page',
68                                                 'content'  => 'magic potions',
69                                          ),
70                                          array(
71                                                 '_type' => 'page',
72                                                 'content'  => 'magic spells',
73                                          )
74                                 )
75                         )
76                 );
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' );
86                 R::nuke();
87                 $book = R::dispense(
88                         array(
89                                 '_type'=>'book',
90                                 'title'=>'The magic book',
91                                 'author' => array(
92                                          '_type' => 'author',
93                                          'name'  => 'Dr. Evil'
94                                 ),
95                                 'coAuthor' => array(
96                                          '_type' => 'author',
97                                          'name'  => 'Dr. Creepy'
98                                 ),
99                                 'ownPageList' => array(
100                                          array(
101                                                 '_type' => 'page',
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'),
107                                                 )
108                                          ),
109                                          array(
110                                                 '_type' => 'page',
111                                                 'content'  => 'magic spells',
112                                          )
113                                 ),
114                                 'sharedCategory' => array(
115                                          array(
116                                                   '_type' => 'category',
117                                                   'label' => 'wizardry'
118                                          ),
119                                 )
120                         )
121                 );
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' );
145                 try {
146                         $list = R::dispense( array() );
147                         pass();
148                         asrt( is_array( $list ), TRUE );
149                         asrt( count( $list ), 0 );
150                 } catch ( RedException $ex ) {
151                         pass();
152                 }
153                 try {
154                         R::dispense( array( array() ) );
155                         fail();
156                 } catch ( RedException $ex ) {
157                         pass();
158                 }
159                 try {
160                         R::dispense( array( 'a' ) );
161                         fail();
162                 } catch ( RedException $ex ) {
163                         pass();
164                 }
165                 try {
166                         R::dispense( array( 'property' => 'value' ) );
167                         fail();
168                 } catch ( RedException $ex ) {
169                         pass();
170                 }
171         }
172
173         /**
174          * Test import from and tainted.
175          *
176          * @return void
177          */
178         public function testImportFromAndTainted()
179         {
180                 testpack( 'Test importFrom() and Tainted' );
181                 $bean = R::dispense( 'bean' );
182                 R::store( $bean );
183                 $bean->name = 'abc';
184                 asrt( $bean->getMeta( 'tainted' ), TRUE );
185                 R::store( $bean );
186                 asrt( $bean->getMeta( 'tainted' ), FALSE );
187                 $copy = R::dispense( 'bean' );
188                 R::store( $copy );
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 ) );
200                 asrt( $bean->a, 1 );
201                 asrt( $bean->b, 2 );
202                 $bean->import( array( "a" => 3, "b" => 4 ), "a,b" );
203                 asrt( $bean->a, 3 );
204                 asrt( $bean->b, 4 );
205                 $bean->import( array( "a" => 5, "b" => 6 ), " a , b " );
206                 asrt( $bean->a, 5 );
207                 asrt( $bean->b, 6 );
208                 $bean->import( array( "a" => 1, "b" => 2 ) );
209                 testpack( 'Test inject() feature.' );
210                 $coffee = R::dispense( 'coffee' );
211                 $coffee->id     = 2;
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
222                 asrt( $cup->id, 0 );
223         }
224
225         /**
226          * Test import using array access functions
227          *
228          * @return void
229          */
230         public function testArrayAccess()
231         {
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 );
270         }
271 }