3 namespace RedUNIT\Blackhole;
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\ToolBox as TB;
8 use RedBeanPHP\QueryWriter as QueryWriter;
9 use RedBeanPHP\Adapter as Adapter;
10 use RedBeanPHP\OODB as OODB;
11 use RedBeanPHP\BeanHelper as BeanHelper;
12 use RedBeanPHP\BeanHelper\SimpleFacadeBeanHelper as SimpleFacadeBeanHelper;
13 use RedBeanPHP\Repository as Repository;
14 use RedBeanPHP\Repository\Fluid as FluidRepo;
15 use RedBeanPHP\Repository\Frozen as FrozenRepo;
16 use RedBeanPHP\RedException as RedException;
21 * The Toolbox acts as a kind of micro service locator.
22 * The toolbox is passed around by RedBeanPHP core objects
23 * to share services. It contains an adapter, a query writer
24 * and the RedBeanPHP Object Database object (OODB).
25 * This test suite focuses on the toolbox.
27 * @file RedUNIT/Blackhole/Toolbox.php
28 * @desc Toolbox tests.
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 Toolbox extends Blackhole {
39 * Tests the hasDatabase() method.
43 public function testDatabaseCheck()
45 R::addDatabase( 'key1', 'mysql:dsn1', 'user1', 'password1', TRUE );
46 asrt( R::hasDatabase( 'key1' ), TRUE );
47 asrt( R::hasDatabase( 'key2' ), FALSE );
51 * Github issue #458, selectDatabase causes PHP notice undefined index
52 * if database key not found.
56 public function testInvalidDB()
59 R::selectDatabase( 'idontexist' );
61 } catch ( RedException $exception ) {
67 * Test whether we can obtain a toolbox properly.
71 public function testCanWeObtainToolbox()
73 $toolbox = R::getToolBox();
74 asrt( ( $toolbox instanceof TB), TRUE );
75 $extractedToolbox = R::getExtractedToolbox();
76 asrt( is_array( $extractedToolbox ), TRUE );
77 asrt( count( $extractedToolbox ), 4 );
78 asrt( ( $extractedToolbox[0] instanceof OODB ), TRUE );
79 asrt( ( $extractedToolbox[1] instanceof Adapter ), TRUE );
80 asrt( ( $extractedToolbox[2] instanceof QueryWriter ), TRUE );
81 asrt( ( $extractedToolbox[3] instanceof TB ), TRUE );
82 $beanHelper = new SimpleFacadeBeanHelper;
83 $toolbox2 = $beanHelper->getToolbox();
84 asrt( ( $toolbox2 instanceof TB), TRUE );
85 asrt( $toolbox, $toolbox2 );
86 $extractedToolbox = $beanHelper->getExtractedToolbox();
87 asrt( is_array( $extractedToolbox ), TRUE );
88 asrt( count( $extractedToolbox ), 4 );
89 asrt( ( $extractedToolbox[0] instanceof OODB ), TRUE );
90 asrt( ( $extractedToolbox[1] instanceof Adapter ), TRUE );
91 asrt( ( $extractedToolbox[2] instanceof QueryWriter ), TRUE );
92 asrt( ( $extractedToolbox[3] instanceof TB ), TRUE );
96 * Does the toolbox contain the necessary tools ?
100 public function testDoesToolboxContainTheTools()
102 $toolbox = R::getToolBox();
103 asrt( ( $toolbox->getDatabaseAdapter() instanceof Adapter ), TRUE );
104 asrt( ( $toolbox->getRedBean() instanceof OODB ), TRUE );
105 asrt( ( $toolbox->getWriter() instanceof QueryWriter ), TRUE );
109 * Tests whether freeze() switches the repository object
110 * as it is supposed to do.
114 public function testRepoSwitching()
116 asrt( class_exists( 'RedBeanPHP\Repository' ), TRUE );
117 asrt( class_exists( 'RedBeanPHP\Repository\Fluid' ), TRUE );
118 asrt( class_exists( 'RedBeanPHP\Repository\Frozen' ), TRUE );
120 $redbean = R::getRedBean();
121 $repo = $redbean->getCurrentRepository();
122 asrt( is_object( $repo ), TRUE );
123 asrt( ( $repo instanceof Repository ), TRUE );
124 asrt( ( $repo instanceof FluidRepo ), TRUE );
127 $repo = $redbean->getCurrentRepository();
128 asrt( is_object( $repo ), TRUE );
129 asrt( ( $repo instanceof Repository ), TRUE );
130 asrt( ( $repo instanceof FrozenRepo ), TRUE );
133 $redbean = R::getRedBean();
134 $repo = $redbean->getCurrentRepository();
135 asrt( is_object( $repo ), TRUE );
136 asrt( ( $repo instanceof Repository ), TRUE );
137 asrt( ( $repo instanceof FluidRepo ), TRUE );
138 asrt( $repo, $fluid );
141 $repo = $redbean->getCurrentRepository();
142 asrt( is_object( $repo ), TRUE );
143 asrt( ( $repo instanceof Repository ), TRUE );
144 asrt( ( $repo instanceof FrozenRepo ), TRUE );
145 asrt( $repo, $frozen );