3 namespace RedUNIT\Base;
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;
13 * Tests whether we can count beans with or without
14 * additional conditions and whether we can count associated
15 * beans (relationCount).
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
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.
26 class Count extends Base
29 * Tests type check and conversion in
34 public function testCountType()
37 $book = R::dispense( 'book' );
38 $book->sharedPage = R::dispense( 'page', 10 );
40 asrt( R::count('bookPage'), 10 );
43 R::count( 'WrongTypeName' );
45 } catch ( RedException $ex ) {
50 R::count( 'wrong_type_name' );
52 } catch ( RedException $ex ) {
58 * Test count and wipe.
62 public function testCountAndWipe()
64 testpack( "Test count and wipe" );
66 $page = R::dispense( "page" );
72 $n1 = R::count( "page" );
74 $page = R::dispense( "page" );
80 $n2 = R::count( "page" );
86 asrt( R::count( "page" ), 0 );
87 asrt( R::getRedBean()->count( "page" ), 0 );
88 asrt( R::getRedBean()->count( "kazoo" ), 0 ); // non existing table
92 asrt( R::getRedBean()->count( "kazoo" ), 0 ); // non existing table
96 $page = R::dispense( 'page' );
102 $page = R::dispense( 'page' );
108 asrt( R::count( 'page', ' name = ? ', array( 'foo' ) ), 1 );
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 );
114 $currentDriver = $this->currentlyActiveDriverID;
116 // Some drivers don't support that many error codes.
117 if ( $currentDriver === 'mysql' || $currentDriver === 'postgres' ) {
119 R::count( 'teaport', ' for tea ' );
127 public function testCountShared() {
130 $book = R::dispense( 'book' );
131 $book->sharedPageList = R::dispense( 'page', 5 );
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);
139 $am->relatedCount( 'not a bean', 'type' );
141 } catch( RedException $e ) {