3 namespace RedUNIT\Base;
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
11 * Beans can also be treated like arrays, this test verifies
12 * whether the bean array interface works correctly in various
13 * scenarios (combination with lists and so on).
15 * @file RedUNIT/Base/Arrays.php
16 * @desc Tests the array interface of beans
17 * @author Gabor de Mooij and the RedBeanPHP Community
18 * @license New BSD/GPLv2
20 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
21 * This source file is subject to the New BSD/GPLv2 License that is bundled
22 * with this source code in the file license.txt.
24 class Arrays extends Base
27 * Tests basic array access.
31 public function testArrayAccess()
33 $bean = R::dispense('bean');
35 $bean->taste = 'salty';
36 $properties = array();
37 foreach($bean as $key => $value) {
38 $properties[ $key ] = $value;
40 asrt( count( $bean ), 3 );
41 asrt( count( $properties ), 3 );
42 asrt( isset( $properties['id'] ), TRUE );
43 asrt( isset( $properties['name'] ), TRUE );
44 asrt( isset( $properties['taste'] ), TRUE );
45 $bean = R::dispense('bean');
46 $bean['name'] = 'bean';
47 $bean['taste'] = 'salty';
48 $properties = array();
49 foreach($bean as $key => $value) {
50 $properties[ $key ] = $value;
52 asrt( count( $bean ), 3 );
53 asrt( count( $properties ), 3 );
54 asrt( isset( $properties['id'] ), TRUE );
55 asrt( isset( $properties['name'] ), TRUE );
56 asrt( isset( $properties['taste'] ), TRUE );
60 * Tests array access with lists.
61 * Tests whether list properties behave as arrays correctly.
65 public function testArrayAccessAndLists()
67 $book = R::dispense('book');
68 $book['title'] = 'My Book';
69 //Can we add a bean in list with array access?
70 $book['ownPage'][] = R::dispense('page');
71 $book['ownPage'][] = R::dispense('page');
72 asrt( count( $book ), 3 );
73 $properties = array();
74 foreach($book as $key => $value) {
75 $properties[ $key ] = $value;
77 asrt( count( $properties ), 3 );
78 //Dont reveal aliased x-own and -List in foreach-loop
79 asrt( isset( $properties['id'] ), TRUE );
80 asrt( isset( $properties['title'] ), TRUE );
81 asrt( isset( $properties['ownPage'] ), TRUE );
82 asrt( isset( $properties['ownPageList'] ), FALSE );
83 asrt( isset( $properties['xownPage'] ), FALSE );
84 asrt( isset( $properties['xownPageList'] ), FALSE );
85 //But keep them countable
86 asrt( count( $book['ownPage'] ), 2 );
87 asrt( count( $book['ownPageList'] ), 2 );
88 asrt( count( $book['xownPage'] ), 2 );
89 asrt( count( $book['xownPageList'] ), 2 );
90 //And reveal state of items with isset()
91 asrt( isset( $book['id'] ), TRUE );
92 asrt( isset( $book['title'] ), TRUE );
93 asrt( isset( $book['ownPage'] ), TRUE );
94 asrt( isset( $book['ownPageList'] ), TRUE );
95 asrt( isset( $book['xownPage'] ), TRUE );
96 asrt( isset( $book['xownPageList'] ), TRUE );
97 //Can we add using the List alias?
98 $book['ownPageList'][] = R::dispense('page');
99 asrt( count( $book['ownPage'] ), 3 );
100 asrt( count( $book['ownPageList'] ), 3 );
101 asrt( count( $book['xownPage'] ), 3 );
102 asrt( count( $book['xownPageList'] ), 3 );
103 //Can we add using the x-mode alias?
104 $book['ownPageList'][] = R::dispense('page');
105 asrt( count( $book['ownPage'] ), 4 );
106 asrt( count( $book['ownPageList'] ), 4 );
107 asrt( count( $book['xownPage'] ), 4 );
108 asrt( count( $book['xownPageList'] ), 4 );
109 //Can we unset using array access?
110 unset( $book['ownPage'] );
111 asrt( isset( $book['ownPage'] ), FALSE );
112 asrt( isset( $book['ownPageList'] ), FALSE );
113 asrt( isset( $book['xownPage'] ), FALSE );
114 asrt( isset( $book['xownPageList'] ), FALSE );
115 $book['ownPage'] = array( R::dispense('page') );
116 unset( $book['xownPage'] );
117 asrt( isset( $book['ownPage'] ), FALSE );
118 asrt( isset( $book['ownPageList'] ), FALSE );
119 asrt( isset( $book['xownPage'] ), FALSE );
120 asrt( isset( $book['xownPageList'] ), FALSE );
121 $book['ownPage'] = array( R::dispense('page') );
122 unset( $book['ownPageList'] );
123 asrt( isset( $book['ownPage'] ), FALSE );
124 asrt( isset( $book['ownPageList'] ), FALSE );
125 asrt( isset( $book['xownPage'] ), FALSE );
126 asrt( isset( $book['xownPageList'] ), FALSE );
127 $book['ownPage'] = array( R::dispense('page') );
128 unset( $book['xownPageList'] );
129 asrt( isset( $book['ownPage'] ), FALSE );
130 asrt( isset( $book['ownPageList'] ), FALSE );
131 asrt( isset( $book['xownPage'] ), FALSE );
132 asrt( isset( $book['xownPageList'] ), FALSE );
133 //does it work with shared lists as well?
134 $book['sharedCategory'] = array( R::dispense('category') );
135 asrt( count( $book ), 3 );
136 $properties = array();
137 foreach($book as $key => $value) {
138 $properties[ $key ] = $value;
140 asrt( isset( $properties['sharedCategory'] ), TRUE );
141 asrt( isset( $properties['sharedCategoryList'] ), FALSE );
142 asrt( isset( $book['sharedCategory'] ), TRUE );
143 asrt( isset( $book['sharedCategoryList'] ), TRUE );
144 asrt( count( $book['sharedCategory'] ), 1 );
145 asrt( count( $book['sharedCategoryList'] ), 1 );
146 $book['sharedCategory'][] = R::dispense( 'category' );
147 asrt( count( $book['sharedCategory'] ), 2 );
148 asrt( count( $book['sharedCategoryList'] ), 2 );
149 $book['sharedCategoryList'][] = R::dispense( 'category' );
150 asrt( count( $book['sharedCategory'] ), 3 );
151 asrt( count( $book['sharedCategoryList'] ), 3 );
155 * Tests array access with parent beans.
159 public function testArrayAccessWithBeans()
161 $book = R::dispense( 'bean' );
162 $book['author'] = R::dispense( 'author' );
163 asrt( isset( $book['author'] ), TRUE );
164 asrt( count( $book ), 2 );
165 $book['author']['name'] = 'me';
166 asrt( $book['author']['name'], 'me' );
167 $book['author']['address'] = R::dispense( 'address' );
168 $book['author']['ownTagList'][] = R::dispense( 'tag' );
169 asrt( isset( $book['author']['address'] ), TRUE );
170 asrt( isset( $book['author']['ownTag'] ), TRUE );
171 asrt( count( $book['author']['ownTag'] ), 1 );
172 asrt( isset( $book['author']['xownTag'] ), TRUE );
173 asrt( count( $book['author']['xownTag'] ), 1 );
174 asrt( isset( $book['author']['ownTagList'] ), TRUE );
175 asrt( count( $book['author']['ownTagList'] ), 1 );
176 asrt( isset( $book['author']['xownTagList'] ), TRUE );
177 asrt( count( $book['author']['xownTagList'] ), 1 );
178 unset( $book['author'] );
179 asrt( isset( $book['author'] ), FALSE );
180 asrt( count( $book ), 1 );
184 * Tests array access with CRUD operations.
188 public function testArrayAccessWithCRUD()
191 $book = R::dispense( 'book' );
192 $book['ownPageList'] = R::dispense( 'page', 3 );
194 $book = $book->fresh();
195 //note that isset first returns false, so you can check if a list is loaded
196 asrt( isset( $book['ownPage'] ), FALSE );
197 asrt( isset( $book['ownPageList'] ), FALSE );
198 asrt( isset( $book['xownPage'] ), FALSE );
199 asrt( isset( $book['xownPageList'] ), FALSE );
200 //count triggers load...
201 asrt( count( $book['ownPage'] ), 3 );
202 asrt( isset( $book['ownPage'] ), TRUE );
203 asrt( isset( $book['ownPageList'] ), TRUE );
204 asrt( isset( $book['xownPage'] ), TRUE );
205 asrt( isset( $book['xownPageList'] ), TRUE );
206 $book = $book->fresh();
207 asrt( count( $book['xownPage'] ), 3 );
208 $book = $book->fresh();
209 asrt( count( $book['ownPageList'] ), 3 );
210 $book = $book->fresh();
211 asrt( count( $book['xownPageList'] ), 3 );
212 $book['ownPage'][] = R::dispense( 'page' );
214 $book = $book->fresh();
215 asrt( count( $book['ownPage'] ), 4 );
216 $book = $book->fresh();
217 asrt( count( $book['xownPage'] ), 4 );
218 $book = $book->fresh();
219 asrt( count( $book['ownPageList'] ), 4 );
220 $book = $book->fresh();
221 asrt( count( $book['xownPageList'] ), 4 );
222 //does dependency still work?
223 $book['xownPageList'] = array();
225 $book = $book->fresh();
226 asrt( count( $book['ownPage'] ), 0 );
227 $book = $book->fresh();
228 asrt( count( $book['xownPage'] ), 0 );
229 $book = $book->fresh();
230 asrt( count( $book['ownPageList'] ), 0 );
231 $book = $book->fresh();
232 asrt( count( $book['xownPageList'] ), 0 );
233 //does shared list work as well?
234 $book['sharedTag'] = R::dispense( 'tag', 2 );
236 $book = $book->fresh();
237 //note that isset first returns false, so you can check if a list is loaded
238 asrt( isset( $book['sharedTagList'] ), FALSE );
239 asrt( isset( $book['sharedTag'] ), FALSE );
240 //count triggers load...
241 asrt( count( $book['sharedTagList'] ), 2 );
242 asrt( count( $book['sharedTag'] ), 2 );
243 asrt( isset( $book['sharedTagList'] ), TRUE );
244 asrt( isset( $book['sharedTag'] ), TRUE );
245 $book['sharedTag'][] = R::dispense( 'tag' );
247 $book = $book->fresh();
248 asrt( count( $book['sharedTagList'] ), 3 );
249 asrt( count( $book['sharedTag'] ), 3 );
250 $book['sharedTagList'][] = R::dispense( 'tag' );
252 $book = $book->fresh();
253 asrt( count( $book['sharedTagList'] ), 4 );
254 asrt( count( $book['sharedTag'] ), 4 );
255 //does it also work with cross-shared
256 $book['sharedBookList'][] = R::dispense( 'book' );
258 $book = $book->fresh();
259 asrt( isset( $book['sharedBookList'] ), FALSE );
260 asrt( count( $book['sharedBookList'] ), 1 );
261 $first = reset( $book['sharedBookList'] );
263 asrt( count( $book['sharedBookList'][$id]['sharedBookList'] ), 1 );
264 $properties = array();
265 foreach($book as $key => $value) {
266 $properties[ $key ] = $value;
268 asrt( count( $properties ), 2 );
269 $keys = array_keys( $properties );
271 asrt( implode( ',', $keys ), 'id,sharedBook' );