Skip to content

Commit 3026496

Browse files
committed
refs #ensure-serializable ensure the data produced in variables panel is serializable
1 parent 7672bb9 commit 3026496

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

src/Panel/VariablesPanel.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,13 @@ public function shutdown(Event $event)
126126
// Convert objects into using __debugInfo.
127127
$item = $this->_walkDebugInfo($walker, $item);
128128
} else {
129-
try {
130-
serialize($item);
131-
} catch (\Exception $e) {
132-
$item = __d('debug_kit', 'Unserializable object - {0}. Error: {1} in {2}, line {3}', get_class($item), $e->getMessage(), $e->getFile(), $e->getLine());
133-
}
129+
$item = $this->trySerialize($item);
134130
}
135131
} elseif (is_resource($item)) {
136132
$item = sprintf('[%s] %s', get_resource_type($item), $item);
137133
}
138134

139-
return $item;
135+
return $this->trySerialize($item);
140136
};
141137
// Copy so viewVars is not mutated.
142138
$vars = $controller->viewVars;
@@ -160,6 +156,25 @@ public function shutdown(Event $event)
160156
];
161157
}
162158

159+
/**
160+
* Try to serialize an item, provide an error message if not possible
161+
*
162+
* @param mixed $item Item to check
163+
* @return mixed The $item if it is serializable, error message if not
164+
*/
165+
protected function trySerialize($item)
166+
{
167+
try {
168+
serialize($item);
169+
return $item;
170+
} catch (\Exception $e) {
171+
if (is_object($item)) {
172+
return __d('debug_kit', 'Unserializable object - {0}. Error: {1} in {2}, line {3}', get_class($item), $e->getMessage(), $e->getFile(), $e->getLine());
173+
}
174+
return __d('debug_kit', 'Unserializable Error: {1} in {2}, line {3}', $e->getMessage(), $e->getFile(), $e->getLine());
175+
}
176+
}
177+
163178
/**
164179
* Get summary data for the variables panel.
165180
*

tests/TestCase/Panel/VariablesPanelTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
namespace DebugKit\Test\TestCase\Panel;
1414

1515
use Cake\Event\Event;
16+
use Cake\Form\Form;
1617
use Cake\ORM\TableRegistry;
1718
use Cake\TestSuite\TestCase;
1819
use DebugKit\Panel\VariablesPanel;
20+
use DebugKit\TestApp\Form\TestForm;
1921

2022
/**
2123
* Class VariablesPanelTest
@@ -90,12 +92,20 @@ public function testShutdown()
9092
'unbufferedQuery' => $unbufferedQuery,
9193
'result set' => $result,
9294
'string' => 'yes',
93-
'array' => ['some' => 'key']
95+
'array' => ['some' => 'key'],
96+
'notSerializableForm' => new TestForm(),
9497
];
9598
$event = new Event('Controller.shutdown', $controller);
9699
$this->panel->shutdown($event);
97100
$output = $this->panel->data();
98101

102+
array_walk_recursive($output, function($item) {
103+
try {
104+
serialize($item);
105+
} catch (\Exception $e) {
106+
$this->fail('Panel Output content is not serializable');
107+
}
108+
});
99109
$this->assertRegExp('/^\[stream\] Resource id #\d+$/', $output['content']['resource']);
100110
$this->assertInternalType('array', $output['content']['unserializableDebugInfo']);
101111
$this->assertStringStartsWith(

tests/test_app/Form/TestForm.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace DebugKit\TestApp\Form;
3+
4+
use Cake\Form\Form;
5+
use Cake\Form\Schema;
6+
use Cake\Validation\Validator;
7+
8+
/**
9+
* Class TestForm
10+
* @package DebugKit\TestApp\Form
11+
*/
12+
class TestForm extends Form
13+
{
14+
/**
15+
* Builds the schema for the modelless form
16+
*
17+
* @param \Cake\Form\Schema $schema From schema
18+
* @return \Cake\Form\Schema
19+
*/
20+
protected function _buildSchema(Schema $schema)
21+
{
22+
return $schema->addField('accept', 'boolean');
23+
}
24+
25+
/**
26+
* Form validation builder
27+
*
28+
* @param \Cake\Validation\Validator $validator to use against the form
29+
* @return \Cake\Validation\Validator
30+
*/
31+
protected function _buildValidator(Validator $validator)
32+
{
33+
return $validator
34+
->requirePresence('accept')
35+
->add('accept', 'accept', [
36+
'rule' => function ($value) {
37+
return 'always fail validation';
38+
},
39+
]);
40+
}
41+
42+
/**
43+
* Defines what to execute once the From is being processed
44+
*
45+
* @param array $data Form data.
46+
* @return bool
47+
*/
48+
protected function _execute(array $data)
49+
{
50+
return true;
51+
}
52+
}

0 commit comments

Comments
 (0)