Skip to content

Commit 29a1b33

Browse files
committed
Start using Codeception Module config to set mockserver url and log cleanup
1 parent 1c291ea commit 29a1b33

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/MockServerHelper.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,43 @@
22

33
namespace DEVizzent\CodeceptionMockServerHelper;
44

5-
use Codeception\Lib\ModuleContainer;
65
use Codeception\Module;
6+
use Codeception\TestInterface;
77
use GuzzleHttp\Client;
88
use GuzzleHttp\Psr7\Request;
99
use PHPUnit\Framework\Assert;
1010

1111
class MockServerHelper extends Module
1212
{
13+
protected array $config = [
14+
'url' => 'http://mockserver:1080',
15+
'cleanupBefore' => 'test'
16+
];
17+
1318
private Client $mockserverClient;
1419

1520
/**
1621
* @param array<string, string> $config
1722
*/
18-
public function __construct(ModuleContainer $moduleContainer, ?array $config = null)
23+
public function _initialize()
24+
{
25+
$this->mockserverClient = new Client(['base_uri' => $this->config['url']]);
26+
}
27+
28+
public function _beforeSuite(array $settings = [])
1929
{
20-
parent::__construct($moduleContainer, $config);
21-
$this->mockserverClient = new Client(['base_uri' => 'http://mockserver:1080']);
30+
if ('suite' === $this->config['cleanupBefore']) {
31+
$this->clearMockServerLogs();
32+
}
2233
}
34+
35+
public function _before(TestInterface $test)
36+
{
37+
if ('test' === $this->config['cleanupBefore']) {
38+
$this->clearMockServerLogs();
39+
}
40+
}
41+
2342
public function seeMockRequestWasCalled(string $expectationId, ?int $times = null): void
2443
{
2544
$body = json_encode([
@@ -41,7 +60,7 @@ public function seeMockRequestWasNotCalled(string $expectationId): void
4160
$this->seeMockRequestWasCalled($expectationId, 0);
4261
}
4362

44-
public function resetMockServerLogs(): void
63+
public function clearMockServerLogs(): void
4564
{
4665
$request = new Request('PUT', '/mockserver/clear?type=log');
4766
$response = $this->mockserverClient->sendRequest($request);

tests/Integration/SeeMockRequestWasCalledTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Integration;
44

5+
use Codeception\Lib\ModuleContainer;
56
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
67
use GuzzleHttp\Client;
78
use PHPUnit\Framework\ExpectationFailedException;
@@ -15,9 +16,11 @@ class SeeMockRequestWasCalledTest extends TestCase
1516
protected function setUp(): void
1617
{
1718
parent::setUp();
18-
$this->sot = new MockServerHelper();
19+
$moduleContainer = $this->createMock(ModuleContainer::class);
20+
$this->sot = new MockServerHelper($moduleContainer);
21+
$this->sot->_initialize();
1922
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
20-
$this->sot->resetMockServerLogs();
23+
$this->sot->clearMockServerLogs();
2124
}
2225

2326
public function testExpectationWasCalled(): void

tests/Integration/SeeMockRequestWasNotCalledTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Integration;
44

5+
use Codeception\Lib\ModuleContainer;
56
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
67
use GuzzleHttp\Client;
78
use PHPUnit\Framework\ExpectationFailedException;
@@ -15,15 +16,28 @@ class SeeMockRequestWasNotCalledTest extends TestCase
1516
protected function setUp(): void
1617
{
1718
parent::setUp();
18-
$this->sot = new MockServerHelper();
19+
$moduleContainer = $this->createMock(ModuleContainer::class);
20+
$this->sot = new MockServerHelper($moduleContainer);
21+
$this->sot->_initialize();
1922
$this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]);
23+
$this->sot->clearMockServerLogs();
2024
}
2125

2226
public function testExpectationWasNotCalled(): void
2327
{
2428
$this->sot->seeMockRequestWasNotCalled('get-post-2');
2529
}
2630

31+
public function testExpectationWasNotCalledButItWasThrowException(): void
32+
{$this->expectException(ExpectationFailedException::class);
33+
$this->expectExceptionMessageMatches(
34+
'#^Request not found exactly 0 times, expected:((.|\n)*) but was:((.|\n)*)'
35+
. 'Failed asserting that 406 matches expected 202\.$#'
36+
);
37+
$this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2');
38+
$this->sot->seeMockRequestWasNotCalled('get-post-2');
39+
}
40+
2741
public function testExpectationWasCalledThrowException(): void
2842
{
2943
$this->expectException(ExpectationFailedException::class);

0 commit comments

Comments
 (0)