Skip to content

Commit 67444ec

Browse files
committed
Create fixture records as needed.
Use a factory trait to create records instead of using static fixtures as they are proving to be unreliable on github actions with SQLite.
1 parent 5e16007 commit 67444ec

9 files changed

Lines changed: 106 additions & 43 deletions

src/Command/BenchmarkCommand.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
7575
/**
7676
* Prints calculated results
7777
*
78-
* @param array $times Array of time values
78+
* @param float[] $times Array of time values
7979
* @return void
8080
*/
8181
protected function _results($times)
@@ -89,21 +89,23 @@ protected function _results($times)
8989
$this->io->out('');
9090

9191
$this->io->out(Text::insert(__d('debug_kit', 'Requests/Second: :rps req/sec'), [
92-
'rps' => round($requests / $duration, 3),
92+
'rps' => round($requests / $duration, 3),
9393
]));
9494

9595
$this->io->out(Text::insert(__d('debug_kit', 'Average request time: :average-time seconds'), [
96-
'average-time' => round($duration / $requests, 3),
96+
'average-time' => round($duration / $requests, 3),
9797
]));
9898

9999
$this->io->out(Text::insert(__d('debug_kit', 'Standard deviation of average request time: :std-dev'), [
100-
'std-dev' => round($this->_deviation($times, true), 3),
100+
'std-dev' => round($this->_deviation($times, true), 3),
101101
]));
102102

103-
$this->io->out(Text::insert(__d('debug_kit', 'Longest/shortest request: :longest sec/:shortest sec'), [
103+
if (!empty($times)) {
104+
$this->io->out(Text::insert(__d('debug_kit', 'Longest/shortest request: :longest sec/:shortest sec'), [
104105
'longest' => round(max($times), 3),
105106
'shortest' => round(min($times), 3),
106-
]));
107+
]));
108+
}
107109

108110
$this->io->out('');
109111
}

tests/Fixture/PanelsFixture.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,7 @@ class PanelsFixture extends TestFixture
6060
*
6161
* @var array
6262
*/
63-
public $records = [
64-
[
65-
'id' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
66-
'request_id' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
67-
'panel' => 'DebugKit.Request',
68-
'title' => 'Request',
69-
'element' => 'DebugKit.request_panel',
70-
'content' => 'a:5:{s:6:"params";a:5:{s:6:"plugin";N;s:10:"controller";s:5:"Tasks";s:6:"action";s:3:"add";s:4:"_ext";N;s:4:"pass";a:0:{}}s:5:"query";a:0:{}s:4:"data";a:0:{}s:6:"cookie";a:2:{s:14:"toolbarDisplay";s:4:"show";s:7:"CAKEPHP";s:26:"9pk8sa2ot6pclki9f4iakio560";}s:3:"get";a:0:{}}',
71-
],
72-
];
63+
public $records = [];
7364

7465
/**
7566
* Constructor

tests/Fixture/RequestsFixture.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ class RequestsFixture extends TestFixture
5252
*
5353
* @var array
5454
*/
55-
public $records = [
56-
[
57-
'id' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
58-
'url' => '/tasks/add',
59-
'content_type' => 'text/html',
60-
'status_code' => 200,
61-
'requested_at' => '2014-08-21 7:41:12',
62-
],
63-
];
55+
public $records = [];
6456

6557
/**
6658
* Constructor

tests/TestCase/Controller/DashboardControllerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
namespace DebugKit\Test\TestCase\Controller;
1717

1818
use Cake\TestSuite\IntegrationTestCase;
19+
use DebugKit\Test\TestCase\FixtureFactoryTrait;
1920
use DebugKit\TestApp\Application;
2021

2122
/**
2223
* Dashboard controller test.
2324
*/
2425
class DashboardControllerTest extends IntegrationTestCase
2526
{
27+
use FixtureFactoryTrait;
28+
2629
public $fixtures = [
2730
'plugin.DebugKit.Requests',
2831
'plugin.DebugKit.Panels',
@@ -37,7 +40,6 @@ public function setUp(): void
3740
{
3841
parent::setUp();
3942
$this->configApplication(Application::class, []);
40-
$this->useHttpServer(true);
4143
}
4244

4345
public function testIndexNoRequests()
@@ -55,9 +57,8 @@ public function testIndexNoRequests()
5557

5658
public function testIndexWithRequests()
5759
{
58-
$requests = $this->getTableLocator()->get('DebugKit.Requests');
59-
$request = $requests->newEntity(['url' => '/example']);
60-
$requests->save($request);
60+
$request = $this->makeRequest();
61+
$this->makePanel($request);
6162

6263
$this->get('/debug-kit/dashboard');
6364

@@ -68,12 +69,13 @@ public function testIndexWithRequests()
6869

6970
public function testReset()
7071
{
71-
$requests = $this->getTableLocator()->get('DebugKit.Requests');
72-
$this->assertGreaterThan(0, $requests->find()->count(), 'precondition failed');
72+
$request = $this->makeRequest();
73+
$this->makePanel($request);
7374

7475
$this->post('/debug-kit/dashboard/reset');
7576

7677
$this->assertRedirect('/debug-kit');
78+
$requests = $this->getTableLocator()->get('DebugKit.Requests');
7779
$this->assertSame(0, $requests->find()->count());
7880
}
7981
}

tests/TestCase/Controller/MailPreviewControllerTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717

1818
use Cake\Routing\Router;
1919
use Cake\TestSuite\IntegrationTestCase;
20+
use DebugKit\Test\TestCase\FixtureFactoryTrait;
2021
use DebugKit\TestApp\Application;
2122

2223
/**
2324
* Mail preview controller test
2425
*/
2526
class MailPreviewControllerTest extends IntegrationTestCase
2627
{
28+
use FixtureFactoryTrait;
29+
2730
/**
2831
* Fixtures.
2932
*
@@ -46,7 +49,7 @@ public function setUp(): void
4649
$routes->connect('/users/:action/*', ['controller' => 'Users']);
4750
});
4851
$this->configApplication(Application::class, []);
49-
$this->useHttpServer(true);
52+
$this->getTableLocator()->clear();
5053
}
5154

5255
/**
@@ -105,8 +108,9 @@ public function testSentInvalidData()
105108
*/
106109
public function testSentValidData()
107110
{
108-
$panels = $this->getTableLocator()->get('Panels');
109-
$panel = $panels->newEntity(['request_id' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa']);
111+
$panels = $this->getTableLocator()->get('DebugKit.Panels');
112+
$request = $this->makeRequest();
113+
$panel = $panels->newEntity(['request_id' => $request->id]);
110114
$data = [
111115
'emails' => [
112116
[
@@ -131,8 +135,9 @@ public function testSentValidData()
131135
*/
132136
public function testSentValidDataRenderPart()
133137
{
134-
$panels = $this->getTableLocator()->get('Panels');
135-
$panel = $panels->newEntity(['request_id' => 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa']);
138+
$panels = $this->getTableLocator()->get('DebugKit.Panels');
139+
$request = $this->makeRequest();
140+
$panel = $panels->newEntity(['request_id' => $request->id]);
136141
$data = [
137142
'emails' => [
138143
[

tests/TestCase/Controller/PanelsControllerTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
namespace DebugKit\Test\TestCase\Controller;
1717

1818
use Cake\TestSuite\IntegrationTestCase;
19+
use DebugKit\Test\TestCase\FixtureFactoryTrait;
1920
use DebugKit\TestApp\Application;
2021

2122
/**
2223
* Panel controller test.
2324
*/
2425
class PanelsControllerTest extends IntegrationTestCase
2526
{
27+
use FixtureFactoryTrait;
28+
2629
/**
2730
* Fixtures.
2831
*
@@ -42,7 +45,6 @@ public function setUp(): void
4245
{
4346
parent::setUp();
4447
$this->configApplication(Application::class, []);
45-
$this->useHttpServer(true);
4648
}
4749

4850
/**
@@ -57,8 +59,9 @@ public function testIndex()
5759
'accept' => 'application/json, text/javascript, */*; q=0.01',
5860
],
5961
]);
60-
61-
$this->get('/debug-kit/panels/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
62+
$request = $this->makeRequest();
63+
$this->makePanel($request);
64+
$this->get("/debug-kit/panels/{$request->id}");
6265

6366
$this->assertResponseOk();
6467
$this->assertContentType('application/json');
@@ -71,7 +74,10 @@ public function testIndex()
7174
*/
7275
public function testView()
7376
{
74-
$this->get('/debug-kit/panels/view/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
77+
$request = $this->makeRequest();
78+
$panel = $this->makePanel($request);
79+
80+
$this->get("/debug-kit/panels/view/{$panel->id}");
7581

7682
$this->assertResponseOk();
7783
$this->assertResponseContains('Request</h2>');

tests/TestCase/Controller/RequestsControllerTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
namespace DebugKit\Test\TestCase\Controller;
1717

1818
use Cake\TestSuite\IntegrationTestCase;
19+
use DebugKit\Test\TestCase\FixtureFactoryTrait;
1920
use DebugKit\TestApp\Application;
2021

2122
/**
2223
* Request controller test.
2324
*/
2425
class RequestsControllerTest extends IntegrationTestCase
2526
{
27+
use FixtureFactoryTrait;
28+
2629
/**
2730
* Fixtures.
2831
*
@@ -42,7 +45,6 @@ public function setUp(): void
4245
{
4346
parent::setUp();
4447
$this->configApplication(Application::class, []);
45-
$this->useHttpServer(true);
4648
}
4749

4850
/**
@@ -52,8 +54,11 @@ public function setUp(): void
5254
*/
5355
public function testView()
5456
{
57+
$request = $this->makeRequest();
58+
$panel = $this->makePanel($request);
59+
5560
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
56-
$this->get('/debug-kit/toolbar/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
61+
$this->get("/debug-kit/toolbar/{$request->id}");
5762

5863
$this->assertResponseOk();
5964
$this->assertResponseContains('Request', 'Has a panel button');

tests/TestCase/Controller/ToolbarControllerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function setUp(): void
4343
{
4444
parent::setUp();
4545
$this->configApplication(Application::class, []);
46-
$this->useHttpServer(true);
4746
}
4847

4948
/**
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12+
* @link http://cakephp.org CakePHP(tm) Project
13+
* @since 4.3.6
14+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
15+
*/
16+
namespace DebugKit\Test\TestCase;
17+
18+
trait FixtureFactoryTrait
19+
{
20+
protected function makeRequest()
21+
{
22+
$requests = $this->getTableLocator()->get('DebugKit.Requests');
23+
$request = $requests->newEntity([
24+
'url' => '/panels',
25+
'requested_at' => time(),
26+
]);
27+
28+
return $requests->saveOrFail($request);
29+
}
30+
31+
protected function makePanel($request, $name = 'DebugKit.Request', $title = 'Request', $element = 'DebugKit.request_panel', $content = null)
32+
{
33+
if ($content === null) {
34+
$content = [
35+
'params' => [
36+
'plugin' => null,
37+
'controller' => 'Tasks',
38+
'action' => 'add',
39+
'_ext' => null,
40+
'pass' => [],
41+
],
42+
'query' => [],
43+
'data' => [],
44+
'get' => [],
45+
'cookie' => [
46+
'toolbarDisplay' => 'show',
47+
],
48+
];
49+
}
50+
$panels = $this->getTableLocator()->get('DebugKit.Panels');
51+
$panel = $panels->newEntity([
52+
'request_id' => $request->id,
53+
'panel' => $name,
54+
'title' => $title,
55+
'element' => $element,
56+
'content' => serialize($content),
57+
]);
58+
59+
return $panels->saveOrFail($panel);
60+
}
61+
}

0 commit comments

Comments
 (0)