Skip to content

Commit ed33985

Browse files
committed
Use Client in helper and add the configuration to load expectation for a path
1 parent 9745017 commit ed33985

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

src/Config/ExpectationsPath.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace DEVizzent\CodeceptionMockServerHelper\Config;
4+
5+
use DEVizzent\CodeceptionMockServerHelper\MockServerHelper;
6+
use RecursiveDirectoryIterator;
7+
use RecursiveIteratorIterator;
8+
use SplFileInfo;
9+
10+
class ExpectationsPath
11+
{
12+
private string $path = '';
13+
14+
public function __construct(string $path = '')
15+
{
16+
if (!empty($path)) {
17+
$this->set($path);
18+
}
19+
}
20+
21+
private function set(string $path)
22+
{
23+
$path = realpath($path);
24+
if ($path) {
25+
$this->path = $path;
26+
return;
27+
}
28+
throw new \InvalidArgumentException(sprintf('"%s" is not a valid path for "expectationsPath"', $path));
29+
}
30+
31+
public function getExpectationsFiles(): iterable
32+
{
33+
if (empty($this->path)) {
34+
return [];
35+
}
36+
if (!is_dir($this->path)) {
37+
return [$this->path];
38+
}
39+
$recursiveIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path));
40+
$files = [];
41+
/** @var SplFileInfo $file */
42+
foreach ($recursiveIterator as $file) {
43+
if ($file->isDir()){
44+
continue;
45+
}
46+
$files[] = $file->getPathname();
47+
}
48+
return $files;
49+
}
50+
}

src/MockServerHelper.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@
77
use Codeception\TestInterface;
88
use DEVizzent\CodeceptionMockServerHelper\Client\MockServer;
99
use DEVizzent\CodeceptionMockServerHelper\Config\CleanUpBefore;
10+
use DEVizzent\CodeceptionMockServerHelper\Config\ExpectationsPath;
1011
use DEVizzent\CodeceptionMockServerHelper\Config\NotMatchedRequest;
1112
use GuzzleHttp\Client;
12-
use GuzzleHttp\Psr7\Request;
1313
use PHPUnit\Framework\Assert;
14+
use PHPUnit\Framework\AssertionFailedError;
1415
use PHPUnit\Framework\ExpectationFailedException;
1516

1617
class MockServerHelper extends Module
1718
{
1819
private const CONFIG_NOT_MATCHED_REQUEST = 'notMatchedRequest';
1920
private const CONFIG_URL = 'url';
2021
private const CONFIG_CLEANUP_BEFORE = 'cleanupBefore';
22+
private const CONFIG_EXPECTATIONS_PATH = 'expectationsPath';
2123
public const NOT_MATCHED_REQUEST_ID = 'not-matched-request';
2224
private MockServer $mockserver;
2325
private CleanUpBefore $cleanUpBefore;
2426
private NotMatchedRequest $notMatchedRequest;
27+
private ExpectationsPath $expectationPath;
2528
/** @param array<string, string>|null $config */
2629
public function __construct(ModuleContainer $moduleContainer, ?array $config = null)
2730
{
2831
$this->requiredFields = [self::CONFIG_URL];
2932
$this->cleanUpBefore = new CleanUpBefore(CleanUpBefore::TEST);
3033
$this->notMatchedRequest = new NotMatchedRequest(NotMatchedRequest::ENABLED);
34+
$this->expectationPath = new ExpectationsPath();
3135
parent::__construct($moduleContainer, $config);
3236
}
3337

@@ -41,17 +45,20 @@ public function _initialize(): void
4145
if (is_string($this->config[self::CONFIG_CLEANUP_BEFORE] ?? null)) {
4246
$this->cleanUpBefore = new CleanUpBefore($this->config[self::CONFIG_CLEANUP_BEFORE]);
4347
}
48+
if (is_string($this->config[self::CONFIG_EXPECTATIONS_PATH] ?? null)) {
49+
$this->expectationPath = new ExpectationsPath($this->config[self::CONFIG_EXPECTATIONS_PATH]);
50+
}
4451
$this->mockserver = new MockServer(new Client([
4552
'base_uri' => $this->config[self::CONFIG_URL]
4653
]));
4754
if ($this->notMatchedRequest->isEnabled()) {
48-
$expectationJson = file_get_contents(__DIR__ . '/not-matched-request.json');
49-
Assert::assertIsString($expectationJson);
50-
$this->createMockRequest($expectationJson);
55+
$this->createMockRequestFromJsonFile(__DIR__ . '/not-matched-request.json');
5156
return;
5257
}
5358

54-
$this->deactivateNotMatchedRequest();
59+
try {
60+
$this->deactivateNotMatchedRequest();
61+
} catch (AssertionFailedError $exception) {}
5562
}
5663

5764
public function _beforeSuite($settings = []): void
@@ -60,6 +67,9 @@ public function _beforeSuite($settings = []): void
6067
if ($this->cleanUpBefore->isSuite()) {
6168
$this->mockserver->clearLogs();
6269
}
70+
foreach ($this->expectationPath->getExpectationsFiles() as $expectationFile) {
71+
$this->createMockRequestFromJsonFile($expectationFile);
72+
}
6373
}
6474

6575
public function _before(TestInterface $test): void
@@ -105,6 +115,11 @@ public function removeMockRequest(string $mockRequestId): void
105115
$this->mockserver->removeById($mockRequestId);
106116
}
107117

118+
public function removeAllMockRequest(): void
119+
{
120+
$this->mockserver->removeAllExpectations();
121+
}
122+
108123
public function clearMockServerLogs(): void
109124
{
110125
$this->mockserver->clearLogs();
@@ -114,4 +129,11 @@ public function deactivateNotMatchedRequest(): void
114129
{
115130
$this->mockserver->removeById(self::NOT_MATCHED_REQUEST_ID);
116131
}
132+
133+
public function createMockRequestFromJsonFile($expectationFile): void
134+
{
135+
$expectationJson = file_get_contents($expectationFile);
136+
Assert::assertIsString($expectationJson);
137+
$this->createMockRequest($expectationJson);
138+
}
117139
}

0 commit comments

Comments
 (0)