Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Facade.php
1 <?php
2
3 namespace RedUNIT\Base;
4
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\AssociationManager as AssociationManager;
8 use RedBeanPHP\OODB as OODB;
9 use RedBeanPHP\ToolBox as ToolBox;
10 use RedBeanPHP\Adapter as Adapter;
11 use RedBeanPHP\QueryWriter as QueryWriter;
12 use RedBeanPHP\OODBBean as OODBBean;
13
14 /**
15  * Facade
16  *
17  * Tests the facade. The facade is a single class (often aliased with R)
18  * that provides access to all (or most) RedBeanPHP functionality without
19  * the need to interact with all the objects involved. The facade has
20  * been designed to be as 'straightfoward' as possible, many methods
21  * of the facade class should be 'almost identical' to simple calls
22  * to objects behind the facade. This test focuses on basic usage
23  * scenarios. Complex scenarios are tested in other suites because
24  * we often test 'the complete route' as the facade code path always
25  * includes the non-facade code path as well (as proven by the coverage numbers).
26  *
27  * @file    RedUNIT/Base/Facade.php
28  * @desc    Tests basic functions through facade.
29  * @author  Gabor de Mooij and the RedBeanPHP Community
30  * @license New BSD/GPLv2
31  *
32  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
33  * This source file is subject to the New BSD/GPLv2 License that is bundled
34  * with this source code in the file license.txt.
35  */
36 class Facade extends Base
37 {
38
39         /**
40          * What drivers should be loaded for this test pack?
41          * This pack contains some SQL incomp. with OCI
42          */
43         public function getTargetDrivers()
44         {
45                 return array( 'mysql', 'pgsql', 'sqlite', 'CUBRID' );
46         }
47
48         /**
49          * Tests quick trash method: R::trash( type, id ).
50          *
51          * @return void
52          */
53         public function testQuickTrash()
54         {
55                 R::nuke();
56                 $bean = R::dispense( 'bean' );
57                 $id = R::store( $bean );
58                 asrt( R::count( 'bean' ), 1 );
59                 R::trash( 'bean', $id );
60                 asrt( R::count( 'bean' ), 0 );
61         }
62
63         /**
64          * Test common Facade usage scenarios.
65          *
66          * @return void
67          */
68         public function testCommonUsageFacade()
69         {
70                 $toolbox = R::getToolBox();
71                 $adapter = $toolbox->getDatabaseAdapter();
72                 $writer  = $toolbox->getWriter();
73                 $redbean = $toolbox->getRedBean();
74                 $pdo     = $adapter->getDatabase();
75                 $a       = new AssociationManager( $toolbox );
76                 asrt( R::getRedBean() instanceof OODB, TRUE );
77                 asrt( R::getToolBox() instanceof ToolBox, TRUE );
78                 asrt( R::getDatabaseAdapter() instanceof Adapter, TRUE );
79                 asrt( R::getWriter() instanceof QueryWriter, TRUE );
80                 $book = R::dispense( "book" );
81                 asrt( $book instanceof OODBBean, TRUE );
82                 $book->title = "a nice book";
83                 $id = R::store( $book );
84                 asrt( ( $id > 0 ), TRUE );
85                 $book = R::load( "book", (int) $id );
86                 asrt( $book->title, "a nice book" );
87                 asrt( R::load( 'book', 999 )->title, NULL );
88                 R::freeze( TRUE );
89                 try {
90                         R::load( 'bookies', 999 );
91                         fail();
92                 } catch (\Exception $e ) {
93                         pass();
94                 }
95                 R::freeze( FALSE );
96                 $author = R::dispense( "author" );
97                 $author->name = "me";
98                 R::store( $author );
99                 $book9   = R::dispense( "book" );
100                 $author9 = R::dispense( "author" );
101                 $author9->name = "mr Nine";
102                 $a9 = R::store( $author9 );
103                 $book9->author_id = $a9;
104                 $bk9 = R::store( $book9 );
105                 $book9  = R::load( "book", $bk9 );
106                 $author = R::load( "author", $book9->author_id );
107                 asrt( $author->name, "mr Nine" );
108                 R::trash( $author );
109                 R::trash( $book9 );
110                 pass();
111                 $book2 = R::dispense( "book" );
112                 $book2->title = "second";
113                 R::store( $book2 );
114                 $book3 = R::dispense( "book" );
115                 $book3->title = "third";
116                 R::store( $book3 );
117                 asrt( count( R::find( "book" ) ), 3 );
118                 asrt( count( R::findAll( "book" ) ), 3 );
119                 asrt( count( R::findAll( "book", " LIMIT 2" ) ), 2 );
120                 asrt( count( R::find( "book", " id=id " ) ), 3 );
121                 asrt( count( R::find( "book", " title LIKE ?", array( "third" ) ) ), 1 );
122                 asrt( count( R::find( "book", " title LIKE ?", array( "%d%" ) ) ), 2 );
123                 // Find without where clause
124                 asrt( count( R::findAll( 'book', ' order by id' ) ), 3 );
125                 R::trash( $book3 );
126                 R::trash( $book2 );
127                 asrt( count( R::getAll( "SELECT * FROM book " ) ), 1 );
128                 asrt( count( R::getCol( "SELECT title FROM book " ) ), 1 );
129                 asrt( (int) R::getCell( "SELECT 123 " ), 123 );
130                 $book = R::dispense( "book" );
131                 $book->title = "not so original title";
132                 $author = R::dispense( "author" );
133                 $author->name = "Bobby";
134                 R::store( $book );
135                 $aid = R::store( $author );
136                 $author = R::findOne( "author", " name = ? ", array( "Bobby" ) );
137         }
138 }