Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Blackhole / Plugins.php
1 <?php
2
3 namespace RedUNIT\Blackhole;
4
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\Facade as Facade;
8 use RedBeanPHP\RedException as RedException;
9
10 /**
11  * Plugins
12  *
13  * This test suite tests whether we can define dynamic
14  * plugins using the ext() method on the facade.
15  *
16  * @file    RedUNIT/Blackhole/Plugins.php
17  * @desc    Tests extending R facade dynamically.
18  * @author  Gabor de Mooij and the RedBeanPHP Community
19  * @license New BSD/GPLv2
20  *
21  * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
22  * This source file is subject to the New BSD/GPLv2 License that is bundled
23  * with this source code in the file license.txt.
24  */
25 class Plugins extends Blackhole
26 {
27         /**
28          * Test if we can dynamically extend the R-facade.
29          *
30          * @return void
31          */
32         public function testDynamicPlugins()
33         {
34                 testpack('Test dynamic plugins');
35
36                 //basic behaviour
37                 R::ext( 'makeTea', function() {
38                         return 'sorry cant do that!';
39                 });
40
41                 asrt( R::makeTea(), 'sorry cant do that!' );
42
43                 //with parameters
44                 R::ext( 'multiply', function( $a, $b ) {
45                         return $a * $b;
46                 });
47
48                 asrt( R::multiply( 3, 4 ), 12 );
49
50                 //can we call R inside?
51                 R::ext( 'singVersion', function() {
52                         return R::getVersion() . ' lalala !';
53                 } );
54
55                 asrt( R::singVersion(), ( R::getVersion().' lalala !' ) );
56
57                 //should also work with Facade
58                 asrt( Facade::singVersion(), ( R::getVersion().' lalala !' ) );
59
60                 //test error handling
61                 try {
62                         R::ext( '---', function() {} );
63                         fail();
64                 } catch ( RedException $e ) {
65                         asrt( $e->getMessage(), 'Plugin name may only contain alphanumeric characters.' );
66                 }
67
68                 try {
69                         R::__callStatic( '---', function() {} );
70                         fail();
71                 } catch ( RedException $e ) {
72                         asrt( $e->getMessage(), 'Plugin name may only contain alphanumeric characters.' );
73                 }
74
75                 try {
76                         R::invalidMethod();
77                         fail();
78                 } catch ( RedException $e ) {
79                         asrt( $e->getMessage(), 'Plugin \'invalidMethod\' does not exist, add this plugin using: R::ext(\'invalidMethod\')' );
80                 }
81         }
82 }