File tree 2 files changed +41
-1
lines changed
src/Symfony/Component/HttpFoundation
2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -95,7 +95,20 @@ public function setCallback($callback = null)
95
95
*/
96
96
public function setData ($ data = array ())
97
97
{
98
- $ this ->data = @json_encode ($ data , $ this ->encodingOptions );
98
+ $ errorHandler = null ;
99
+ $ errorHandler = set_error_handler (function () use (&$ errorHandler ) {
100
+ if (JSON_ERROR_NONE !== json_last_error ()) {
101
+ return ;
102
+ }
103
+
104
+ if ($ errorHandler ) {
105
+ call_user_func_array ($ errorHandler , func_get_args ());
106
+ }
107
+ });
108
+
109
+ $ this ->data = json_encode ($ data , $ this ->encodingOptions );
110
+
111
+ restore_error_handler ();
99
112
100
113
if (JSON_ERROR_NONE !== json_last_error ()) {
101
114
throw new \InvalidArgumentException ($ this ->transformJsonError ());
Original file line number Diff line number Diff line change @@ -201,4 +201,31 @@ public function testSetContent()
201
201
{
202
202
JsonResponse::create ("\xB1\x31" );
203
203
}
204
+
205
+ /**
206
+ * @expectedException PHPUnit_Framework_Error_Warning
207
+ * @expectedExceptionMessage This error is expected
208
+ */
209
+ public function testSetContentJsonSerializeError ()
210
+ {
211
+ if (!interface_exists ('JsonSerializable ' )) {
212
+ $ this ->markTestSkipped ('Interface JsonSerializable is available in PHP 5.4+ ' );
213
+ }
214
+
215
+ $ serializable = new JsonSerializableObject ();
216
+
217
+ JsonResponse::create ($ serializable );
218
+ }
219
+ }
220
+
221
+ if (interface_exists ('JsonSerializable ' )) {
222
+ class JsonSerializableObject implements JsonSerializable
223
+ {
224
+ public function jsonSerialize ()
225
+ {
226
+ trigger_error ('This error is expected ' , E_USER_WARNING );
227
+
228
+ return array ();
229
+ }
230
+ }
204
231
}
You can’t perform that action at this time.
0 commit comments