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