chg: add wiremock stub verification
parent
2f659ff99f
commit
ac4275db10
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"');
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue