3 namespace RedUNIT\Blackhole;
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;
13 * Tests debugging functions and checks whether the output
14 * of the debugger displays the correct information.
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
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.
25 class Debug extends Blackhole
28 * Given a query, a set of bindings and an expected outcome,
29 * this method tests the result of the debugger.
31 * @param string $query
32 * @param mixed $bindings
33 * @param string $expected
37 private function testDebug($query, $bindings = NULL, $expected)
39 $debugger = new Debugger;
40 $debugger->setMode( 1 );
41 $debugger->setParamStringLength( 20 );
42 if (!is_null($bindings)) {
43 $debugger->log($query, $bindings);
45 $debugger->log($query);
47 $logs = $debugger->getLogs();
49 $log = str_replace( "\e[32m", '', $log );
50 $log = str_replace( "\e[39m", '', $log );
51 asrt($log, $expected);
56 * Tests the bean dump function used to inspect
57 * the contents of a bean.
61 public function testDump()
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 );
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
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 );
90 * Tests debugging with parameters.
94 public function testDebugger2()
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');
139 * Test facade fancyDebug function
141 public function testDebug2InFacade()
143 R::fancyDebug( TRUE );