Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Blackhole / Debug.php
1 <?php
2
3 namespace RedUNIT\Blackhole;
4
5 use RedUNIT\Blackhole as Blackhole;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\Logger\RDefault\Debug as Debugger;
9
10 /**
11  * Debug
12  *
13  * Tests debugging functions and checks whether the output
14  * of the debugger displays the correct information.
15  *
16  * @file    RedUNIT/Blackhole/Debug.php
17  * @desc    Tests Debugger II.
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 Debug extends Blackhole
26 {
27         /**
28          * Given a query, a set of bindings and an expected outcome,
29          * this method tests the result of the debugger.
30          *
31          * @param string $query
32          * @param mixed  $bindings
33          * @param string $expected
34          *
35          * @return void
36          */
37         private function testDebug($query, $bindings = NULL, $expected)
38         {
39                 $debugger = new Debugger;
40                 $debugger->setMode( 1 );
41                 $debugger->setParamStringLength( 20 );
42                 if (!is_null($bindings)) {
43                         $debugger->log($query, $bindings);
44                 } else {
45                         $debugger->log($query);
46                 }
47                 $logs = $debugger->getLogs();
48                 $log = reset($logs);
49                 $log = str_replace( "\e[32m", '', $log );
50                 $log = str_replace( "\e[39m", '', $log );
51                 asrt($log, $expected);
52                 $debugger->clear();
53         }
54
55         /**
56          * Tests the bean dump function used to inspect
57          * the contents of a bean.
58          *
59          * @return void
60          */
61         public function testDump()
62         {
63                 $beans = R::dispense( 'bean', 2 );
64                 $beans[0]->name = 'hello';
65                 $beans[1]->name = 'world';
66                 $array = R::dump($beans);
67                 asrt( is_array( $array ), TRUE );
68                 foreach( $array as $item ) {
69                         asrt( is_string( $item ), TRUE );
70                 }
71                 $beans[1]->name = 'world, and a very long string that should be shortened';
72                 $array = R::dump($beans);
73                 asrt( is_array( $array ), TRUE );
74                 asrt( strpos( $array[1], '...' ), 35 );
75                 //just to get 100% test cov, we dont need to test this
76                 dmp( $beans );
77                 pass();
78                 //test wrong input
79                 asrt( is_array( R::dump( NULL ) ), TRUE );
80                 asrt( count( R::dump( NULL ) ), 0 );
81                 asrt( is_array( R::dump( '' ) ), TRUE );
82                 asrt( count( R::dump( '' ) ), 0 );
83                 asrt( is_array( R::dump( 1 ) ), TRUE );
84                 asrt( count( R::dump( 1 ) ), 0 );
85                 asrt( is_array( R::dump( TRUE ) ), TRUE );
86                 asrt( count( R::dump( FALSE ) ), 0 );
87         }
88
89         /**
90          * Tests debugging with parameters.
91          *
92          * @return void
93          */
94         public function testDebugger2()
95         {
96                 testpack( 'Test debugger with params.' );
97                 $this->testDebug('SELECT * FROM table', NULL, 'SELECT * FROM table');
98                 $this->testDebug('SELECT * FROM book WHERE title = ?', array('my book'), 'SELECT * FROM book WHERE title = \'my book\'');
99                 $this->testDebug('title = ? OR title = ?', array('book1', 'book2'), 'title = \'book1\' OR title = \'book2\'');
100                 $this->testDebug('title = ? OR price = ?', array('book1', 20), 'title = \'book1\' OR price = 20');
101                 $this->testDebug('number IN (?,?)', array(8,900), 'number IN (8,900)');
102                 $this->testDebug('?', array(20), '20');
103                 $this->testDebug('?,?', array('test',20), '\'test\',20');
104                 $this->testDebug('?', array( NULL ), 'NULL');
105                 $this->testDebug('title = ?', array( NULL ), 'title = NULL');
106                 $this->testDebug('?,?', array( NULL,NULL ), 'NULL,NULL');
107                 $this->testDebug('title = ?', array('a very long title that should be shortened'), 'title = \'a very long title th... \'');
108                 $this->testDebug('title = ? OR title = ?', array('a very long title that should be shortened', 'another long title that should be shortened'), 'title = \'a very long title th... \' OR title = \'another long title t... \'');
109                 $this->testDebug('title = ? OR ?', array('a very long title that should be shortened', NULL), 'title = \'a very long title th... \' OR NULL');
110                 $this->testDebug('?,?', array('hello'), '\'hello\',:slot1');
111                 $this->testDebug('title = :first OR title = :second', array(':first'=>'book1', ':second'=>'book2'), 'title = \'book1\' OR title = \'book2\'');
112                 $this->testDebug('title = :first OR price = :second', array(':first'=>'book1', ':second'=>20), 'title = \'book1\' OR price = 20');
113                 $this->testDebug('number IN (:one,:two)', array(':one'=>8, ':two'=>900), 'number IN (8,900)');
114                 $this->testDebug('number IN (:one,:two)', array(':one'=>8, ':two'=>900, ':three'=>999), 'number IN (8,900)');
115                 $this->testDebug('number IN (:one,:two)', array(':three'=>999, ':one'=>8, ':two'=>900), 'number IN (8,900)');
116                 $this->testDebug('number IN (:one,:two)', array(':one'=>8, ':three'=>999, ':two'=>900), 'number IN (8,900)');
117                 $this->testDebug(':a', array(':a'=>20), '20');
118                 $this->testDebug(':a,?', array(':a'=>20, 30), '20,30');
119                 $this->testDebug(':a,?', array(30, ':a'=>20), '20,30');
120                 $this->testDebug('?,?', array('test',20), '\'test\',20');
121                 $this->testDebug('?', array( NULL ), 'NULL');
122                 $this->testDebug('title = ?', array( NULL ), 'title = NULL');
123                 $this->testDebug('?,?', array( NULL,NULL ), 'NULL,NULL');
124                 $this->testDebug('title = ?', array('a very long title that should be shortened'), 'title = \'a very long title th... \'');
125                 $this->testDebug('title = ? OR title = ?', array('a very long title that should be shortened', 'another long title that should be shortened'), 'title = \'a very long title th... \' OR title = \'another long title t... \'');
126                 $this->testDebug('title = ? OR ?', array('a very long title that should be shortened', NULL), 'title = \'a very long title th... \' OR NULL');
127                 $this->testDebug('?,?', array('hello'), '\'hello\',:slot1');
128                 $this->testDebug('hello ?', 'world', 'hello ?');
129                 $this->testDebug(':slot0 :slot1 :slot2 :slot3 :slot4 :slot5 :slot6 :slot7 :slot8 :slot9 :slot10', array(
130                 'a','b','c','d','e','f','g','h','i','j','k'
131                 ),"'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k'");
132                 $this->testDebug('? ? ? ? ? ? ? ? ? ? ?', array(
133                 'a','b','c','d','e','f','g','h','i','j','k'
134                 ),"'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k'");
135                 $this->testDebug(':a :aaa :ab', array(':a'=>1,':aaa'=>2,':ab'=>3),'1 2 3');
136         }
137
138         /**
139          * Test facade fancyDebug function
140          */
141         public function testDebug2InFacade()
142         {
143                 R::fancyDebug( TRUE );
144                 pass();
145         }
146 }