3 namespace RedUNIT\Blackhole;
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
12 * Tests whether we can export beans to arrays and whether we can
13 * use the exportAll function to convert entire hierarchies into
14 * nested array structures.
16 * @file RedUNIT/Blackhole/Export.php
17 * @desc Tests basic bean exporting features.
18 * @author Gabor de Mooij and the RedBeanPHP Community
19 * @license New BSD/GPLv2
21 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
22 * This source file is subject to the New BSD/GPLv2 License that is bundled
23 * with this source code in the file license.txt.
25 class Export extends Blackhole
32 public function testExportAll()
34 testpack( 'Test exportAll' );
35 $redbean = R::getRedBean();
37 $bean->import( array( 'a' => 1, 'b' => 2 ) );
38 $bean->setMeta( 'justametaproperty', 'hellothere' );
39 $arr = $bean->export();
40 asrt( is_array( $arr ), TRUE );
41 asrt( isset( $arr["a"] ), TRUE );
42 asrt( isset( $arr["b"] ), TRUE );
45 asrt( isset( $arr["__info"] ), FALSE );
46 $arr = $bean->export( TRUE );
47 asrt( isset( $arr["__info"] ), TRUE );
50 $exportBean = $redbean->dispense( 'abean' );
51 $exportBean->setMeta( 'metaitem.bla', 1 );
52 $exportedBean = $exportBean->export( TRUE );
53 asrt( $exportedBean["__info"]["metaitem.bla"], 1 );
54 asrt( $exportedBean["__info"]["type"], "abean" );
55 // Can we determine whether a bean is empty?
56 testpack( 'test $bean->isEmpty() function' );
57 $bean = R::dispense( 'bean' );
58 asrt( $bean->isEmpty(), TRUE );
59 asrt( ( count( $bean ) > 0 ), TRUE );
61 asrt( $bean->isEmpty(), FALSE );
62 asrt( ( count( $bean ) > 0 ), TRUE );
64 asrt( $bean->isEmpty(), TRUE );
65 asrt( ( count( $bean ) > 0 ), TRUE );
66 $bean->property = FALSE;
67 asrt( $bean->isEmpty(), TRUE );
68 asrt( ( count( $bean ) > 0 ), TRUE );
69 $bean->property = NULL;
70 asrt( $bean->isEmpty(), TRUE );
71 asrt( ( count( $bean ) > 0 ), TRUE );
72 unset( $bean->property );
73 asrt( $bean->isEmpty(), TRUE );
74 asrt( ( count( $bean ) > 0 ), TRUE );
76 $bandmember = R::dispense( 'bandmember' );
77 $bandmember->name = 'Duke';
78 $instrument = R::dispense( 'instrument' );
79 $instrument->name = 'Piano';
80 $bandmember->ownInstrument[] = $instrument;
81 $a = R::exportAll( $bandmember );
83 asrt( isset( $a[0] ), TRUE );
84 asrt( (int) $a[0]['id'], 0 );
85 asrt( $a[0]['name'], 'Duke' );
86 asrt( $a[0]['ownInstrument'][0]['name'], 'Piano' );
88 $v = R::dispense( 'village' );
89 $b = R::dispense( 'building' );
92 $v->ownBuilding[] = $b;
94 $a = R::exportAll( $v );
95 asrt( $a[0]['name'], 'a' );
96 asrt( $a[0]['ownBuilding'][0]['name'], 'b' );
97 $v = R::load( 'village', $id );
98 $b2 = R::dispense( 'building' );
100 $v->ownBuilding[] = $b2;
101 $a = R::exportAll( $v );
102 asrt( $a[0]['name'], 'a' );
103 asrt( $a[0]['ownBuilding'][0]['name'], 'b' );
104 asrt( count( $a[0]['ownBuilding'] ), 2 );
105 list( $r1, $r2 ) = R::dispense( 'army', 2 );
108 $v->sharedArmy[] = $r2;
109 $a = R::exportAll( $v );
110 asrt( count( $a[0]['sharedArmy'] ), 1 );
112 $v = R::load( 'village', $id );
113 $a = R::exportAll( $v );
114 asrt( count( $a[0]['sharedArmy'] ), 1 );
115 asrt( $a[0]['name'], 'a' );
116 asrt( $a[0]['ownBuilding'][0]['name'], 'b' );
117 asrt( count( $a[0]['ownBuilding'] ), 2 );
118 $v->sharedArmy[] = $r1;
119 $a = R::exportAll( $v );
120 asrt( count( $a[0]['sharedArmy'] ), 2 );
121 $v = R::load( 'village', $id );
122 $a = R::exportAll( $v );
123 asrt( count( $a[0]['sharedArmy'] ), 1 );
124 $v->sharedArmy[] = $r1;
126 $v = R::load( 'village', $id );
127 $a = R::exportAll( $v );
128 asrt( count( $a[0]['sharedArmy'] ), 2 );