Skip to content

Commit 12e2ad4

Browse files
committed
Refactor the configuration and add validation
1 parent cdb6f19 commit 12e2ad4

File tree

6 files changed

+107
-30
lines changed

6 files changed

+107
-30
lines changed

src/Config/CleanUpBefore.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace DEVizzent\CodeceptionMockServerHelper\Config;
4+
5+
use InvalidArgumentException;
6+
use PHP_CodeSniffer\Tests\Core\File\testFECNClass;
7+
8+
class CleanUpBefore
9+
{
10+
public const TEST = 'test';
11+
public const SUITE = 'suite';
12+
public const NEVER = 'never';
13+
private const ALLOWED_VALUES = [self::TEST, self::SUITE, self::NEVER];
14+
private string $value;
15+
16+
public function __construct(string $value)
17+
{
18+
if (!in_array($value, self::ALLOWED_VALUES, true)) {
19+
$message = sprintf(
20+
'"%s" is not allowed value for cleanUpBefore. Only %s are valid',
21+
$value,
22+
implode(', ', self::ALLOWED_VALUES)
23+
);
24+
throw new InvalidArgumentException($message);
25+
}
26+
$this->value = $value;
27+
}
28+
29+
public function isTest(): bool
30+
{
31+
return self::TEST === $this->value;
32+
}
33+
34+
public function isSuite(): bool
35+
{
36+
return self::SUITE === $this->value;
37+
}
38+
}

src/Config/NotMatchedRequest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DEVizzent\CodeceptionMockServerHelper\Config;
4+
5+
use InvalidArgumentException;
6+
7+
class NotMatchedRequest
8+
{
9+
public const ENABLED = 'enabled';
10+
public const DISABLED = 'disabled';
11+
private const ALLOWED_VALUES = [self::ENABLED, self::DISABLED];
12+
private string $value;
13+
14+
public function __construct(string $value)
15+
{
16+
if (!in_array($value, self::ALLOWED_VALUES, true)) {
17+
$message = sprintf(
18+
'"%s" is not allowed value for notMatchedRequest. Only %s are valid',
19+
$value,
20+
implode(', ', self::ALLOWED_VALUES)
21+
);
22+
throw new InvalidArgumentException($message);
23+
}
24+
$this->value = $value;
25+
}
26+
27+
public function isEnabled(): bool
28+
{
29+
return self::ENABLED === $this->value;
30+
}
31+
}

src/MockServerHelper.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,62 @@
55
use Codeception\Lib\ModuleContainer;
66
use Codeception\Module;
77
use Codeception\TestInterface;
8+
use DEVizzent\CodeceptionMockServerHelper\Config\CleanUpBefore;
9+
use DEVizzent\CodeceptionMockServerHelper\Config\NotMatchedRequest;
810
use GuzzleHttp\Client;
911
use GuzzleHttp\Psr7\Request;
1012
use PHPUnit\Framework\Assert;
1113

1214
class MockServerHelper extends Module
1315
{
14-
const CONFIG_NOT_MATCHED_REQUEST = 'not-matched-request';
15-
const NOT_MATCHED_REQUEST_ACTIVATED = 'activated';
16-
const CONFIG_URL = 'url';
16+
private const CONFIG_NOT_MATCHED_REQUEST = 'notMatchedRequest';
17+
private const CONFIG_URL = 'url';
18+
private const CONFIG_CLEANUP_BEFORE = 'cleanupBefore';
19+
public const NOT_MATCHED_REQUEST_ID = 'not-matched-request';
1720
private Client $mockserverClient;
21+
private CleanUpBefore $cleanUpBefore;
22+
private NotMatchedRequest $notMatchedRequest;
23+
/** @param array<string, string>|null $config */
1824
public function __construct(ModuleContainer $moduleContainer, ?array $config = null)
1925
{
2026
$this->requiredFields = [self::CONFIG_URL];
21-
$this->config['cleanupBefore'] = 'test';
22-
$this->config[self::CONFIG_NOT_MATCHED_REQUEST] = self::NOT_MATCHED_REQUEST_ACTIVATED;
27+
$this->cleanUpBefore = new CleanUpBefore(CleanUpBefore::TEST);
28+
$this->notMatchedRequest = new NotMatchedRequest(NotMatchedRequest::ENABLED);
2329
parent::__construct($moduleContainer, $config);
2430
}
2531

2632

2733
public function _initialize(): void
2834
{
35+
parent::_initialize();
36+
if (is_string($this->config[self::CONFIG_NOT_MATCHED_REQUEST] ?? null)) {
37+
$this->notMatchedRequest = new NotMatchedRequest($this->config[self::CONFIG_NOT_MATCHED_REQUEST]);
38+
}
39+
if (is_string($this->config[self::CONFIG_CLEANUP_BEFORE] ?? null)) {
40+
$this->cleanUpBefore = new CleanUpBefore($this->config[self::CONFIG_CLEANUP_BEFORE]);
41+
}
2942
$this->mockserverClient = new Client([
3043
'base_uri' => $this->config[self::CONFIG_URL]
3144
]);
32-
if (self::NOT_MATCHED_REQUEST_ACTIVATED === $this->config[self::CONFIG_NOT_MATCHED_REQUEST]) {
33-
$this->createMockRequest(file_get_contents(__DIR__.'/not-matched-request.json'));
45+
if ($this->notMatchedRequest->isEnabled()) {
46+
$expectationJson = file_get_contents(__DIR__ . '/not-matched-request.json');
47+
Assert::assertIsString($expectationJson);
48+
$this->createMockRequest($expectationJson);
3449
}
3550
}
3651

3752
public function _beforeSuite(array $settings = []): void
3853
{
39-
if ('suite' === $this->config['cleanupBefore']) {
54+
parent::_beforeSuite($settings);
55+
if ($this->cleanUpBefore->isSuite()) {
4056
$this->clearMockServerLogs();
4157
}
4258
}
4359

44-
public function _before(TestInterface $test):void
60+
public function _before(TestInterface $test): void
4561
{
46-
if ('test' === $this->config['cleanupBefore']) {
62+
parent::_before($test);
63+
if ($this->cleanUpBefore->isTest()) {
4764
$this->clearMockServerLogs();
4865
}
4966
}
@@ -94,21 +111,20 @@ public function clearMockServerLogs(): void
94111
$response->getStatusCode(),
95112
$response->getBody()->getContents()
96113
);
97-
98114
}
99115

100116
public function deactivateNotMatchedRequest(): void
101117
{
102118
$body = json_encode([
103-
'id' => self::CONFIG_NOT_MATCHED_REQUEST
119+
'id' => self::NOT_MATCHED_REQUEST_ID
104120
]);
121+
Assert::assertIsString($body);
105122
$request = new Request('PUT', '/mockserver/clear?type=expectations', [], $body);
106123
$response = $this->mockserverClient->sendRequest($request);
107124
Assert::assertEquals(
108125
200,
109126
$response->getStatusCode(),
110127
$response->getBody()->getContents()
111128
);
112-
113129
}
114-
}
130+
}

tests/Integration/NotMatchedRequestTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace Test\DEVizzent\CodeceptionMockServerHelper\Integration;
44

55
use Codeception\Lib\ModuleContainer;
6+
use DEVizzent\CodeceptionMockServerHelper\Config\NotMatchedRequest;
67
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
78
use GuzzleHttp\Client;
89
use PHPUnit\Framework\ExpectationFailedException;
910
use PHPUnit\Framework\TestCase;
1011

1112
class NotMatchedRequestTest extends TestCase
1213
{
13-
const NOT_MATCHED_URI = 'https://jsonplaceholder.typicode.com/posts/3';
14+
private const NOT_MATCHED_URI = 'https://jsonplaceholder.typicode.com/posts/3';
1415
private MockServerHelper $sot;
1516
private Client $client;
1617

@@ -19,7 +20,7 @@ private function initialize(string $notMatchedRequestConfig): void
1920
$moduleContainer = $this->createMock(ModuleContainer::class);
2021
$this->sot = new MockServerHelper(
2122
$moduleContainer,
22-
['url' => 'http://mockserver:1080', 'not-matched-request' => $notMatchedRequestConfig]
23+
['url' => 'http://mockserver:1080', 'notMatchedRequest' => $notMatchedRequestConfig]
2324
);
2425
$this->sot->_initialize();
2526
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
@@ -34,24 +35,15 @@ protected function tearDown(): void
3435

3536
public function testActivateNotMatchedRequestCreateExpectation()
3637
{
37-
$this->initialize('activated');
38+
$this->initialize(NotMatchedRequest::ENABLED);
3839
$this->sot->seeMockRequestWasNotCalled('not-matched-request');
3940
}
4041

41-
public function testActivateNotMatchedRequestWasCreatedAndCalled()
42-
{
43-
$this->initialize('activated');
44-
$this->client->request('GET', self::NOT_MATCHED_URI, ['http_errors' => false]);
45-
$this->sot->seeMockRequestWasCalled('not-matched-request', 1);
46-
}
47-
4842
public function testActivateNotMatchedRequestWasCreatedAndDeactivated()
4943
{
50-
$this->initialize('activated');
44+
$this->initialize(NotMatchedRequest::ENABLED);
5145
$this->sot->deactivateNotMatchedRequest();
5246
$this->client->request('GET', self::NOT_MATCHED_URI, ['http_errors' => false]);
5347
$this->sot->seeMockRequestWasNotCalled('not-matched-request');
5448
}
55-
56-
57-
}
49+
}

tests/Integration/SeeMockRequestWasCalledTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ public function testExpectationNotWasCalledThrowException2(): void
4545
);
4646
$this->sot->seeMockRequestWasCalled('not-existing-expectation');
4747
}
48-
}
48+
}

tests/Integration/SeeMockRequestWasNotCalledTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ public function testExpectationWasCalledNotExistExpectationThrowException(): voi
4848
);
4949
$this->sot->seeMockRequestWasNotCalled('not-existing-expectation');
5050
}
51-
}
51+
}

0 commit comments

Comments
 (0)