Skip to content

Commit b6a75d4

Browse files
authored
Merge pull request #651 from CakeDC/feature/ensure-serializable
Feature/ensure serializable
2 parents 1407274 + 4c108aa commit b6a75d4

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

src/Panel/VariablesPanel.php

Lines changed: 23 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,27 @@ 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+
170+
return $item;
171+
} catch (\Exception $e) {
172+
if (is_object($item)) {
173+
return __d('debug_kit', 'Unserializable object - {0}. Error: {1} in {2}, line {3}', get_class($item), $e->getMessage(), $e->getFile(), $e->getLine());
174+
}
175+
176+
return __d('debug_kit', 'Unserializable Error: {1} in {2}, line {3}', $e->getMessage(), $e->getFile(), $e->getLine());
177+
}
178+
}
179+
163180
/**
164181
* Get summary data for the variables panel.
165182
*

tests/TestCase/Panel/VariablesPanelTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cake\ORM\TableRegistry;
1717
use Cake\TestSuite\TestCase;
1818
use DebugKit\Panel\VariablesPanel;
19+
use DebugKit\TestApp\Form\TestForm;
1920

2021
/**
2122
* Class VariablesPanelTest
@@ -90,12 +91,20 @@ public function testShutdown()
9091
'unbufferedQuery' => $unbufferedQuery,
9192
'result set' => $result,
9293
'string' => 'yes',
93-
'array' => ['some' => 'key']
94+
'array' => ['some' => 'key'],
95+
'notSerializableForm' => new TestForm(),
9496
];
9597
$event = new Event('Controller.shutdown', $controller);
9698
$this->panel->shutdown($event);
9799
$output = $this->panel->data();
98100

101+
array_walk_recursive($output, function ($item) {
102+
try {
103+
serialize($item);
104+
} catch (\Exception $e) {
105+
$this->fail('Panel Output content is not serializable');
106+
}
107+
});
99108
$this->assertRegExp('/^\[stream\] Resource id #\d+$/', $output['content']['resource']);
100109
$this->assertInternalType('array', $output['content']['unserializableDebugInfo']);
101110
$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)