Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Count.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\RedException as RedException;
8 use RedBeanPHP\RedException\SQL as SQL;
9
10 /**
11  * Count
12  *
13  * Tests whether we can count beans with or without
14  * additional conditions and whether we can count associated
15  * beans (relationCount).
16  *
17  * @file    RedUNIT/Base/Count.php
18  * @desc    Tests for simple bean counting.
19  * @author  Gabor de Mooij and the RedBeanPHP Community
20  * @license New BSD/GPLv2
21  *
22  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
23  * This source file is subject to the New BSD/GPLv2 License that is bundled
24  * with this source code in the file license.txt.
25  */
26 class Count extends Base
27 {
28         /**
29          * Tests type check and conversion in
30          * OODB for count().
31          *
32          * @return void
33          */
34         public function testCountType()
35         {
36                 R::nuke();
37                 $book = R::dispense( 'book' );
38                 $book->sharedPage = R::dispense( 'page', 10 );
39                 R::store( $book );
40                 asrt( R::count('bookPage'), 10 );
41
42                 try {
43                         R::count( 'WrongTypeName' );
44                         fail();
45                 } catch ( RedException $ex ) {
46                         pass();
47                 }
48
49                 try {
50                         R::count( 'wrong_type_name' );
51                         fail();
52                 } catch ( RedException $ex ) {
53                         pass();
54                 }
55         }
56
57         /**
58          * Test count and wipe.
59          *
60          * @return void
61          */
62         public function testCountAndWipe()
63         {
64                 testpack( "Test count and wipe" );
65
66                 $page = R::dispense( "page" );
67
68                 $page->name = "ABC";
69
70                 R::store( $page );
71
72                 $n1 = R::count( "page" );
73
74                 $page = R::dispense( "page" );
75
76                 $page->name = "DEF";
77
78                 R::store( $page );
79
80                 $n2 = R::count( "page" );
81
82                 asrt( $n1 + 1, $n2 );
83
84                 R::wipe( "page" );
85
86                 asrt( R::count( "page" ), 0 );
87                 asrt( R::getRedBean()->count( "page" ), 0 );
88                 asrt( R::getRedBean()->count( "kazoo" ), 0 ); // non existing table
89
90                 R::freeze( TRUE );
91
92                 asrt( R::getRedBean()->count( "kazoo" ), 0 ); // non existing table
93
94                 R::freeze( FALSE );
95
96                 $page = R::dispense( 'page' );
97
98                 $page->name = 'foo';
99
100                 R::store( $page );
101
102                 $page = R::dispense( 'page' );
103
104                 $page->name = 'bar';
105
106                 R::store( $page );
107
108                 asrt( R::count( 'page', ' name = ? ', array( 'foo' ) ), 1 );
109
110                 // Now count something that does not exist, this should return 0. (just be polite)
111                 asrt( R::count( 'teapot', ' name = ? ', array( 'flying' ) ), 0 );
112                 asrt( R::count( 'teapot' ), 0 );
113
114                 $currentDriver = $this->currentlyActiveDriverID;
115
116                 // Some drivers don't support that many error codes.
117                 if ( $currentDriver === 'mysql' || $currentDriver === 'postgres' ) {
118                         try {
119                                 R::count( 'teaport', ' for tea ' );
120                                 fail();
121                         } catch ( SQL $e ) {
122                                 pass();
123                         }
124                 }
125         }
126
127         public function testCountShared() {
128
129                 R::nuke();
130                 $book = R::dispense( 'book' );
131                 $book->sharedPageList = R::dispense( 'page', 5 );
132                 R::store( $book );
133                 asrt( $book->countShared('page'), 5 );
134                 asrt( $book->countShared('leaflet'), 0 );
135                 asrt( R::dispense( 'book' )->countShared('page'), 0 );
136                 $am = R::getRedBean()->getAssociationManager();
137                 asrt( $am->relatedCount( R::dispense( 'book' ), 'page' ), 0);
138                 try {
139                         $am->relatedCount( 'not a bean', 'type' );
140                         fail();
141                 } catch( RedException $e ) {
142                         pass();
143                 }
144
145         }
146
147 }