Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Blackhole / Toolbox.php
1 <?php
2
3 namespace RedUNIT\Blackhole;
4
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;
17
18 /**
19  * Toolbox
20  *
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.
26  *
27  * @file    RedUNIT/Blackhole/Toolbox.php
28  * @desc    Toolbox tests.
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 Toolbox extends Blackhole {
37
38         /**
39          * Tests the hasDatabase() method.
40          *
41          * @return void
42          */
43         public function testDatabaseCheck()
44         {
45                 R::addDatabase( 'key1', 'mysql:dsn1', 'user1', 'password1', TRUE );
46                 asrt( R::hasDatabase( 'key1' ), TRUE );
47                 asrt( R::hasDatabase( 'key2' ), FALSE );
48         }
49
50         /**
51          * Github issue #458, selectDatabase causes PHP notice undefined index
52          * if database key not found.
53          *
54          * @return void
55          */
56         public function testInvalidDB()
57         {
58                 try {
59                         R::selectDatabase( 'idontexist' );
60                         fail();
61                 } catch ( RedException $exception ) {
62                         pass();
63                 }
64         }
65
66         /**
67          * Test whether we can obtain a toolbox properly.
68          *
69          * @return void
70          */
71         public function testCanWeObtainToolbox()
72         {
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 );
93         }
94
95         /**
96          * Does the toolbox contain the necessary tools ?
97          *
98          * @return void
99          */
100         public function testDoesToolboxContainTheTools()
101         {
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 );
106         }
107
108         /**
109          * Tests whether freeze() switches the repository object
110          * as it is supposed to do.
111          *
112          * @return void
113          */
114         public function testRepoSwitching()
115         {
116                 asrt( class_exists( 'RedBeanPHP\Repository' ), TRUE );
117                 asrt( class_exists( 'RedBeanPHP\Repository\Fluid' ), TRUE );
118                 asrt( class_exists( 'RedBeanPHP\Repository\Frozen' ), TRUE );
119                 R::freeze( FALSE );
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 );
125                 R::freeze( TRUE );
126                 $fluid = $repo;
127                 $repo = $redbean->getCurrentRepository();
128                 asrt( is_object( $repo ), TRUE );
129                 asrt( ( $repo instanceof Repository ), TRUE );
130                 asrt( ( $repo instanceof FrozenRepo ), TRUE );
131                 $frozen = $repo;
132                 R::freeze( FALSE );
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 );
139                 R::freeze( TRUE );
140                 $fluid = $repo;
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 );
146                 R::freeze( FALSE );
147         }
148 }