| 12345 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Toolbar facade tests.
|
|
|
4 |
*
|
|
|
5 |
* PHP 5
|
|
|
6 |
*
|
|
|
7 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
8 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
9 |
*
|
|
|
10 |
* Licensed under The MIT License
|
|
|
11 |
* Redistributions of files must retain the above copyright notice.
|
|
|
12 |
*
|
|
|
13 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
14 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
15 |
* @since DebugKit 0.1
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('View', 'View');
|
|
|
20 |
App::uses('Controller', 'Controller');
|
|
|
21 |
App::uses('Helper', 'View');
|
|
|
22 |
App::uses('ToolbarHelper', 'DebugKit.View/Helper');
|
|
|
23 |
App::uses('ConnectionManager', 'Manager');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Class MockBackendHelper
|
|
|
27 |
*
|
|
|
28 |
* @since DebugKit 0.1
|
|
|
29 |
*/
|
|
|
30 |
class MockBackendHelper extends Helper {
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Class ToolbarHelperTestCase
|
|
|
35 |
*
|
|
|
36 |
*/
|
|
|
37 |
class ToolbarHelperTestCase extends CakeTestCase {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Fixtures
|
|
|
41 |
*
|
|
|
42 |
* @var array
|
|
|
43 |
*/
|
|
|
44 |
public $fixtures = array('core.post');
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* setUp
|
|
|
48 |
*
|
|
|
49 |
* @return void
|
|
|
50 |
*/
|
|
|
51 |
public function setUp() {
|
|
|
52 |
parent::setUp();
|
|
|
53 |
$db = ConnectionManager::getDatasource('test');
|
|
|
54 |
$db->fullDebug = true;
|
|
|
55 |
|
|
|
56 |
Configure::write('Cache.disable', false);
|
|
|
57 |
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
|
|
|
58 |
Router::parse('/');
|
|
|
59 |
|
|
|
60 |
$this->Controller = new Controller(null);
|
|
|
61 |
$this->View = new View($this->Controller);
|
|
|
62 |
$this->Toolbar = new ToolbarHelper($this->View, array(
|
|
|
63 |
'output' => 'MockBackendHelper',
|
|
|
64 |
'cacheKey' => 'debug_kit_toolbar_test_case',
|
|
|
65 |
'cacheConfig' => 'default'
|
|
|
66 |
));
|
|
|
67 |
$this->Toolbar->MockBackend = $this->getMock('Helper', array('testMethod'), array($this->View));
|
|
|
68 |
|
|
|
69 |
$this->_viewPaths = App::path('views');
|
|
|
70 |
App::build(array(
|
|
|
71 |
'View' => array(
|
|
|
72 |
CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'View' . DS,
|
|
|
73 |
APP . 'Plugin' . DS . 'DebugKit' . DS . 'View' . DS,
|
|
|
74 |
CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'View' . DS
|
|
|
75 |
)), true);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* tearDown
|
|
|
80 |
*
|
|
|
81 |
* @return void
|
|
|
82 |
*/
|
|
|
83 |
public function tearDown() {
|
|
|
84 |
parent::tearDown();
|
|
|
85 |
Cache::delete('debug_kit_toolbar_test_case', 'default');
|
|
|
86 |
unset($this->Toolbar, $this->Controller);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* test cache writing for views.
|
|
|
91 |
*
|
|
|
92 |
* @return void
|
|
|
93 |
*/
|
|
|
94 |
public function testCacheWrite() {
|
|
|
95 |
$result = $this->Toolbar->writeCache('test', array('stuff', 'to', 'cache'));
|
|
|
96 |
$this->assertTrue($result);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Ensure that the cache writing only affects the
|
|
|
101 |
* top most level of the history stack. As this is where the current request is stored.
|
|
|
102 |
*
|
|
|
103 |
* @return void
|
|
|
104 |
*/
|
|
|
105 |
public function testOnlyWritingToFirstElement() {
|
|
|
106 |
$values = array(
|
|
|
107 |
array('test' => array('content' => array('first', 'values'))),
|
|
|
108 |
array('test' => array('content' => array('second', 'values'))),
|
|
|
109 |
);
|
|
|
110 |
Cache::write('debug_kit_toolbar_test_case', $values, 'default');
|
|
|
111 |
$this->Toolbar->writeCache('test', array('new', 'values'));
|
|
|
112 |
|
|
|
113 |
$result = $this->Toolbar->readCache('test');
|
|
|
114 |
$this->assertEquals($result, array('new', 'values'));
|
|
|
115 |
|
|
|
116 |
$result = $this->Toolbar->readCache('test', 1);
|
|
|
117 |
$this->assertEquals($result, array('second', 'values'));
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* test cache reading for views
|
|
|
122 |
*
|
|
|
123 |
* @return void
|
|
|
124 |
*/
|
|
|
125 |
public function testCacheRead() {
|
|
|
126 |
$result = $this->Toolbar->writeCache('test', array('stuff', 'to', 'cache'));
|
|
|
127 |
$this->assertTrue($result, 'Cache write failed %s');
|
|
|
128 |
|
|
|
129 |
$result = $this->Toolbar->readCache('test');
|
|
|
130 |
$this->assertEquals($result, array('stuff', 'to', 'cache'), 'Cache value is wrong %s');
|
|
|
131 |
|
|
|
132 |
$result = $this->Toolbar->writeCache('test', array('new', 'stuff'));
|
|
|
133 |
$this->assertTrue($result, 'Cache write failed %s');
|
|
|
134 |
|
|
|
135 |
$result = $this->Toolbar->readCache('test');
|
|
|
136 |
$this->assertEquals($result, array('new', 'stuff'), 'Cache value is wrong %s');
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Test that reading/writing doesn't work with no cache config.
|
|
|
141 |
*
|
|
|
142 |
* @return void
|
|
|
143 |
*/
|
|
|
144 |
public function testNoCacheConfigPresent() {
|
|
|
145 |
$this->Toolbar = new ToolbarHelper($this->View, array('output' => 'MockBackendHelper'));
|
|
|
146 |
|
|
|
147 |
$result = $this->Toolbar->writeCache('test', array('stuff', 'to', 'cache'));
|
|
|
148 |
$this->assertFalse($result, 'Writing to cache succeeded with no cache config %s');
|
|
|
149 |
|
|
|
150 |
$result = $this->Toolbar->readCache('test');
|
|
|
151 |
$this->assertFalse($result, 'Reading cache succeeded with no cache config %s');
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* ensure that getQueryLogs works and writes to the cache so the history panel will
|
|
|
156 |
* work.
|
|
|
157 |
*
|
|
|
158 |
* @return void
|
|
|
159 |
*/
|
|
|
160 |
public function testGetQueryLogs() {
|
|
|
161 |
$model = new CakeTestModel(array('table' => 'posts', 'alias' => 'Post'));
|
|
|
162 |
$model->find('all');
|
|
|
163 |
$model->find('first');
|
|
|
164 |
|
|
|
165 |
$result = $this->Toolbar->getQueryLogs($model->useDbConfig, array('cache' => false));
|
|
|
166 |
$this->assertTrue(is_array($result));
|
|
|
167 |
$this->assertTrue(count($result) >= 2, 'Should be more than 2 queries in the log %s');
|
|
|
168 |
$this->assertTrue(isset($result['queries'][0]['actions']));
|
|
|
169 |
|
|
|
170 |
$model->find('first');
|
|
|
171 |
Cache::delete('debug_kit_toolbar_test_case', 'default');
|
|
|
172 |
$result = $this->Toolbar->getQueryLogs($model->useDbConfig, array('cache' => true));
|
|
|
173 |
|
|
|
174 |
$cached = $this->Toolbar->readCache('sql_log');
|
|
|
175 |
$this->assertTrue(isset($cached[$model->useDbConfig]));
|
|
|
176 |
$this->assertEquals($cached[$model->useDbConfig]['queries'][0], $result['queries'][0]);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
}
|