diff --git a/tests/Helper/WireMockTestTrait.php b/tests/Helper/WireMockTestTrait.php index 9b42f7e..7d47b49 100644 --- a/tests/Helper/WireMockTestTrait.php +++ b/tests/Helper/WireMockTestTrait.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace App\Test\Helper; use \WireMock\Client\WireMock; -use Exception; +use \WireMock\Client\ValueMatchingStrategy; +use \WireMock\Client\RequestPatternBuilder; +use \WireMock\Stubbing\StubMapping; trait WireMockTestTrait { @@ -26,7 +28,7 @@ trait WireMockTestTrait ); if (!$this->wiremock->isAlive()) { - throw new Exception('Failed to connect to WireMock server.'); + throw new \Exception('Failed to connect to WireMock server.'); } $this->clearWireMockStubs(); @@ -46,4 +48,42 @@ trait WireMockTestTrait { return sprintf('http://%s:%s', $this->config['hostname'], $this->config['port']); } + + /** + * Verify all WireMock stubs were called. + * + * @return void + */ + public function verifyAllStubsCalled(): void + { + $stubs = $this->wiremock->listAllStubMappings()->getMappings(); + foreach ((array)$stubs as $stub) { + $this->verifyStubCalled($stub); + } + } + + /** + * Verify the WireMock stub was called. + * + * @param StubMapping $stub + * @return void + */ + public function verifyStubCalled(StubMapping $stub): void + { + $validator = new RequestPatternBuilder($stub->getRequest()->getMethod(), $stub->getRequest()->getUrlMatchingStrategy()); + + // validate headers + $headers = $stub->getRequest()->getHeaders(); + if (is_array($headers)) { + foreach ($headers as $header => $rule) { + $validator = $validator->withHeader($header, ValueMatchingStrategy::fromArray($rule)); + } + } + + // TODO: Add body matching + // TODO: Add query matching + // TODO: Add cookie matching + + $this->wiremock->verify($validator); + } } diff --git a/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php b/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php index 7045ceb..c562c85 100644 --- a/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php +++ b/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php @@ -31,17 +31,12 @@ class TestBroodConnectionApiTest extends TestCase { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); $this->initializeWireMock(); - $this->mockCerebrateStatusResponse(); + $stub = $this->mockCerebrateStatusResponse(); $url = sprintf('%s/%d', self::ENDPOINT, BroodsFixture::BROOD_WIREMOCK_ID); $this->get($url); - $this->getWireMock()->verify( - WireMock::getRequestedFor(WireMock::urlEqualTo('/instance/status.json')) - ->withHeader('Content-Type', WireMock::equalTo('application/json')) - ->withHeader('Authorization', WireMock::equalTo(BroodsFixture::BROOD_WIREMOCK_API_KEY)) - ); - + $this->verifyStubCalled($stub); $this->assertResponseOk(); $this->assertResponseContains('"user": "wiremock"'); } diff --git a/tests/TestCase/Api/LocalTools/MispInterConnectionTest.php b/tests/TestCase/Api/LocalTools/MispInterConnectionTest.php index bc893f9..7f643d3 100644 --- a/tests/TestCase/Api/LocalTools/MispInterConnectionTest.php +++ b/tests/TestCase/Api/LocalTools/MispInterConnectionTest.php @@ -258,6 +258,7 @@ class MispInterConnectionTest extends TestCase $this->post(sprintf('/inbox/process/%s', $acceptRequest['data']['id'])); $this->assertResponseOk(); $this->assertResponseContains('"success": true'); + $this->verifyAllStubsCalled(); } private function mockCerebrateGetExposedToolsResponse(string $instance, string $cerebrateAuthkey): \WireMock\Stubbing\StubMapping