jeudi 27 décembre 2018

Pact for PHP - 404 response code from mock server

I've tried to configure Pact for PHP using example configuration. My problem is I can run a mockServer, but every request I make returns 404 response. Of course I set everything up like in a GitHub readme. Still, I know server is visible (localhost config) but routes could not be registered.

Code example:

class PactTest extends \Tests\BaseTestCases\V2TestCase {

/** @var MockServerConfig */
private $config;

public function setUp()
{
    // Create your basic configuration. The host and port will need to match
    // whatever your Http Service will be using to access the providers data.
    $this->config = new MockServerConfig();
    $this->config->setHost('localhost');
    $this->config->setPort(7200);
    $this->config->setConsumer('someConsumer');
    $this->config->setProvider('someProvider');
    $this->config->setHealthCheckTimeout(60);
    $this->config->setCors(true);

    // Instantiate the mock server object with the config. This can be any
    // instance of MockServerConfigInterface.
    $server = new MockServer($this->config);

    // Create the process.
    $server->start();

    // Stop the process.
    $server->stop();
}

public function testSimple()
{
    $matcher = new Matcher();

    // Create your expected request from the consumer.
    $request = new ConsumerRequest();
    $request
        ->setMethod('GET')
        ->setPath('/test/abc')
        ->addHeader('Content-Type', 'application/json');

    // Create your expected response from the provider.
    $response = new ProviderResponse();
    $response
        ->setStatus(200)
        ->addHeader('Content-Type', 'application/json;charset=utf-8')
        ->setBody([
            'message' => $matcher->term('Hello, Bob', '(Hello, )[A-Za-z]')
        ]);

    // Create a configuration that reflects the server that was started. You can
    // create a custom MockServerConfigInterface if needed. This configuration
    // is the same that is used via the PactTestListener and uses environment variables.
    $builder = new InteractionBuilder($this->config);
    $builder
        ->given('a thing exists')
        ->uponReceiving('a get request to /test/abc')
        ->with($request)
        ->willRespondWith($response); // This has to be last. This is what makes an API request to the Mock Server to set the interaction.

    $service = new HttpClientService($this->config->getBaseUri()); // Pass in the URL to the Mock Server.
    $result  = $service->getTestAbc(); // Make the real API request against the Mock Server.

    $builder->verify();

    self::assertEquals('Hello, Bob', $result); // Make your assertions.
}

Where getTestAbc() is:

public function getTestAbc(): string
{
    $uri = $this->baseUri;
    $response = $this->httpClient->get("{$uri->getHost()}/test/abc", [
        'headers' => ['Content-Type' => 'application/json']
    ]);
    $body   = $response->getBody();
    $object = \json_decode($body);

    return $object->message;
}

What do I do wrong?

Aucun commentaire:

Enregistrer un commentaire