@@ -506,4 +506,53 @@ public function testIsEnabledForceEnableCallable()
506506 ]);
507507 $ this ->assertTrue ($ bar ->isEnabled (), 'debug is off, panel is forced on ' );
508508 }
509+
510+ /**
511+ * Test that saveData handles serialization errors gracefully
512+ *
513+ * @return void
514+ */
515+ public function testSaveDataSerializationError ()
516+ {
517+ $ request = new Request ([
518+ 'url ' => '/articles ' ,
519+ 'environment ' => ['REQUEST_METHOD ' => 'GET ' ],
520+ ]);
521+ $ response = new Response ([
522+ 'statusCode ' => 200 ,
523+ 'type ' => 'text/html ' ,
524+ 'body ' => '<html><title>test</title><body><p>some text</p></body> ' ,
525+ ]);
526+
527+ $ bar = new ToolbarService ($ this ->events , []);
528+ $ bar ->loadPanels ();
529+
530+ // Create a panel with unserializable data
531+ $ panel = $ bar ->registry ()->load ('DebugKit.TestApp\Panel\SimplePanel ' );
532+ // Mock the data() method to return something problematic
533+ $ panel ->_data = ['closure ' => fn () => 'test ' ];
534+
535+ $ row = $ bar ->saveData ($ request , $ response );
536+ $ this ->assertNotEmpty ($ row , 'Should save data even with serialization errors ' );
537+
538+ $ requests = $ this ->getTableLocator ()->get ('DebugKit.Requests ' );
539+ $ result = $ requests ->find ()
540+ ->orderBy (['Requests.requested_at ' => 'DESC ' ])
541+ ->contain ('Panels ' )
542+ ->first ();
543+
544+ // Find the SimplePanel in the results
545+ $ simplePanel = null ;
546+ foreach ($ result ->panels as $ p ) {
547+ if ($ p ->panel === 'SimplePanel ' ) {
548+ $ simplePanel = $ p ;
549+ break ;
550+ }
551+ }
552+
553+ $ this ->assertNotNull ($ simplePanel , 'SimplePanel should be present ' );
554+ $ content = unserialize ($ simplePanel ->content );
555+ $ this ->assertArrayHasKey ('error ' , $ content , 'Should have error key ' );
556+ $ this ->assertStringContainsString ('SimplePanel ' , $ content ['error ' ], 'Error should mention panel name ' );
557+ }
509558}
0 commit comments