More tidying.
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Chill.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7
8 /**
9  * Chill
10  *
11  * Tests 'chill' mode. In this mode some bean types are frozen,
12  * their schemas cannot be modified while others are fluid and
13  * can still be adjusted.
14  *
15  * @file    RedUNIT/Base/Chill.php
16  * @desc    Tests chill list functionality, i.e. freezing a subset of all types.
17  * @author  Gabor de Mooij and the RedBeanPHP Community
18  * @license New BSD/GPLv2
19  *
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.
23  */
24 class Chill extends Base
25 {
26         /**
27          * Test Chill mode.
28          *
29          * @return void
30          */
31         public function testChill()
32         {
33                 $bean = R::dispense( 'bean' );
34
35                 $bean->col1 = '1';
36                 $bean->col2 = '2';
37
38                 R::store( $bean );
39
40                 asrt( count( R::getWriter()->getColumns( 'bean' ) ), 3 );
41
42                 $bean->col3 = '3';
43
44                 R::store( $bean );
45
46                 asrt( count( R::getWriter()->getColumns( 'bean' ) ), 4 );
47
48                 R::freeze( array( 'umbrella' ) );
49
50                 $bean->col4 = '4';
51
52                 R::store( $bean );
53
54                 asrt( count( R::getWriter()->getColumns( 'bean' ) ), 5 );
55
56                 R::freeze( array( 'bean' ) );
57
58                 $bean->col5 = '5';
59
60                 try {
61                         R::store( $bean );
62                         fail();
63                 } catch (\Exception $e ) {
64                         pass();
65                 }
66
67                 asrt( count( R::getWriter()->getColumns( 'bean' ) ), 5 );
68
69                 R::freeze( array() );
70
71                 $bean->col5 = '5';
72
73                 R::store( $bean );
74
75                 asrt( count( R::getWriter()->getColumns( 'bean' ) ), 6 );
76         }
77
78         /**
79          * Test whether we cannot add unique constraints on chilled tables,
80          * otherwise you cannot avoid this from happening when adding beans to the
81          * shared list :) -- this is almost a theoretical issue however we want it
82          * to work according to specifications!
83          *
84          * @return void
85          */
86         public function testDontAddUniqueConstraintForChilledBeanTypes()
87         {
88                 R::nuke();
89                 $person = R::dispense( 'person' );
90                 $role = R::dispense( 'role' );
91                 $person->sharedRole[] = $role;
92                 R::store( $person );
93                 $person->sharedRole[] = R::dispense( 'role' );
94                 R::store( $person );
95                 $bean = R::getRedBean()->dispense('person_role');
96                 $bean->personId = $person->id;
97                 $bean->roleId = $role->id;
98                 try {
99                         R::store( $bean );
100                         fail();
101                 } catch(\Exception $e) {
102                         pass();
103                 }
104                 asrt(R::count('person_role'), 2);
105
106                 R::nuke();
107                 $link = R::getRedBean()->dispense('person_role');
108                 $person = R::dispense( 'person' );
109                 $role = R::dispense( 'role' );
110                 $link->person = $person;
111                 $link->role = $role;
112                 R::store( $link );
113                 R::freeze(array('person_role'));
114                 $person->sharedRole[] = R::dispense( 'role' );
115                 R::store( $person );
116                 $bean = R::getRedBean()->dispense('person_role');
117                 $bean->personId = $person->id;
118                 $bean->roleId = $role->id;
119                 try {
120                         R::store( $bean );
121                         pass();
122                 } catch(\Exception $e) {
123                         fail();
124                 }
125                 asrt(R::count('person_role'), 3);
126                 R::freeze( array() ); //set freeze to FALSE and clear CHILL LIST!
127         }
128
129         /**
130          * Test whether we can set and reset the chill list and check the contents
131          * of the chill list.
132          *
133          * @return void
134          */
135         public function testChillTest()
136         {
137                 R::freeze( array( 'beer' ) );
138                 $oodb = R::getRedBean();
139                 asrt( $oodb->isChilled( 'beer' ), TRUE );
140                 asrt( $oodb->isChilled( 'wine' ), FALSE );
141                 R::freeze( FALSE );
142                 $oodb = R::getRedBean();
143                 asrt( $oodb->isChilled( 'beer' ), TRUE );
144                 asrt( $oodb->isChilled( 'wine' ), FALSE );
145                 R::freeze( TRUE );
146                 $oodb = R::getRedBean();
147                 asrt( $oodb->isChilled( 'beer' ), TRUE );
148                 asrt( $oodb->isChilled( 'wine' ), FALSE );
149                 R::freeze( array() );
150                 $oodb = R::getRedBean();
151                 asrt( $oodb->isChilled( 'beer' ), FALSE );
152                 asrt( $oodb->isChilled( 'wine' ), FALSE );
153         }
154 }