3 namespace RedUNIT\Base;
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;
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).
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
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.
36 class Facade extends Base
40 * What drivers should be loaded for this test pack?
41 * This pack contains some SQL incomp. with OCI
43 public function getTargetDrivers()
45 return array( 'mysql', 'pgsql', 'sqlite', 'CUBRID' );
49 * Tests quick trash method: R::trash( type, id ).
53 public function testQuickTrash()
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 );
64 * Test common Facade usage scenarios.
68 public function testCommonUsageFacade()
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 );
90 R::load( 'bookies', 999 );
92 } catch (\Exception $e ) {
96 $author = R::dispense( "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" );
111 $book2 = R::dispense( "book" );
112 $book2->title = "second";
114 $book3 = R::dispense( "book" );
115 $book3->title = "third";
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 );
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";
135 $aid = R::store( $author );
136 $author = R::findOne( "author", " name = ? ", array( "Bobby" ) );