73674b2d8e1595475c8e3baef3ff07f7f4b4eec2
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Blackhole / Export.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
9 /**
10  * Export
11  *
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.
15  *
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
20  *
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.
24  */
25 class Export extends Blackhole
26 {
27         /**
28          * ExportAll.
29          *
30          * @return void
31          */
32         public function testExportAll()
33         {
34                 testpack( 'Test exportAll' );
35                 $redbean = R::getRedBean();
36                 $bean = new OODBBean;
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 );
43                 asrt( $arr["a"], 1 );
44                 asrt( $arr["b"], 2 );
45                 asrt( isset( $arr["__info"] ), FALSE );
46                 $arr = $bean->export( TRUE );
47                 asrt( isset( $arr["__info"] ), TRUE );
48                 asrt( $arr["a"], 1 );
49                 asrt( $arr["b"], 2 );
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 );
60                 $bean->property = 1;
61                 asrt( $bean->isEmpty(), FALSE );
62                 asrt( ( count( $bean ) > 0 ), TRUE );
63                 $bean->property = 0;
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 );
75                 // Export bug I found
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 );
82                 pass();
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' );
87                 R::nuke();
88                 $v = R::dispense( 'village' );
89                 $b = R::dispense( 'building' );
90                 $v->name = 'a';
91                 $b->name = 'b';
92                 $v->ownBuilding[] = $b;
93                 $id = R::store( $v );
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' );
99                 $b2->name = 'c';
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 );
106                 $r1->name = '1';
107                 $r2->name = '2';
108                 $v->sharedArmy[] = $r2;
109                 $a = R::exportAll( $v );
110                 asrt( count( $a[0]['sharedArmy'] ), 1 );
111                 R::store( $v );
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;
125                 R::store( $v );
126                 $v = R::load( 'village', $id );
127                 $a = R::exportAll( $v );
128                 asrt( count( $a[0]['sharedArmy'] ), 2 );
129         }
130 }