Hello. I'm trying out pact for the first time and ...
# pact-php
m
Hello. I'm trying out pact for the first time and I can't seem to get a basic example working. I am getting:
Copy code
PhpPact\Standalone\Exception\HealthCheckFailedException: Failed to make connection to Mock Server in 100 attempts
Here is my test setup:
Copy code
private \PhpPact\Standalone\MockService\MockServer $mockServer;

    private \PhpPact\Standalone\MockService\MockServerEnvConfig $config;

    public function setUp(): void
    {
        $this->config = new \PhpPact\Standalone\MockService\MockServerEnvConfig();
        $this->config->setCors(true);

        $this->config->setLog('file.log');

        $this->mockServer = new \PhpPact\Standalone\MockService\MockServer($this->config);
        $this->mockServer->start();
    }

    public function tearDown(): void
    {
        // Stop the Pact Mock Server
        $this->mockServer->stop();
    }
I would paste the whole test but this fails during the set-up even before I get to the test. Not sure what to try. This is running locally on PHP 8.2 but also fails when running on PHP 8.1 via a docker container. Is there something obvious (probably) I am missing here? Here is my phpunit.xml
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>" xsi:noNamespaceSchemaLocation="<https://schema.phpunit.de/10.1/phpunit.xsd>" bootstrap="tests/bootstrap.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true">
  <testsuites>
    <testsuite name="Contract">
      <directory>./tests/contract/</directory>
    </testsuite>
  </testsuites>
  <php>
    <env name="PACT_MOCK_SERVER_HOST" value="localhost"/>
    <env name="PACT_MOCK_SERVER_PORT" value="7200"/>
    <env name="PACT_CONSUMER_NAME" value="ExampleOne"/>
    <env name="PACT_PROVIDER_NAME" value="ExampleAPI"/>
  </php>
  <coverage>
    <report>
      <clover outputFile="clover.xml"/>
    </report>
  </coverage>
</phpunit>
y
Hey Mike, What os/arch are you running on, and what docker image have you tried? If you drop into the pact-php repo and do
composer install
/
composer test
does that run for you?
m
Hey @Yousaf Nabi (pactflow.io). I can run composer test locally (osx). I get a timeout eventually:
Copy code
> phpunit --debug -c example/phpunit.all.xml
PHPUnit 9.6.10 by Sebastian Bergmann and contributors.

Test 'PhpPactTest\Broker\Service\BrokerHttpClientTest::testAllConsumerUrlsAreExtractedCorrectly' started
Test 'PhpPactTest\Broker\Service\BrokerHttpClientTest::testAllConsumerUrlsAreExtractedCorrectly' ended
Test 'PhpPactTest\Consumer\InteractionBuilderTest::testSimpleGet' started
[2023-07-17T14:25:49.583843+00:00] server.debug: start background command: /app/src/PhpPact/Standalone/Installer/Model/../../../../../bin/pact-ruby-standalone/bin/pact-mock-service service --consumer='someConsumer' --provider='someProvider' --pact-dir='./example/output//' --pact-file-write-mode=overwrite --host=localhost --port=7200 --cors=true --pact-specification-version=2.0.0 [] []
[2023-07-17T14:25:49.776588+00:00] server.debug: started process pid=35 [] []
Test 'PhpPactTest\Consumer\InteractionBuilderTest::testSimpleGet' ended

Stopping Process Id: 35
Test 'PhpPactTest\Consumer\InteractionBuilderTest::testPostWithBody' started
[2023-07-17T14:29:12.398424+00:00] server.debug: start background command: /app/src/PhpPact/Standalone/Installer/Model/../../../../../bin/pact-ruby-standalone/bin/pact-mock-service service --consumer='someConsumer' --provider='someProvider' --pact-dir='./example/output//' --pact-file-write-mode=overwrite --host=localhost --port=7200 --cors=true --pact-specification-version=2.0.0 [] []
[2023-07-17T14:29:12.409379+00:00] server.debug: started process pid=46 [] []
The following exception is caused by a process timeout
Check <https://getcomposer.org/doc/06-config.md#process-timeout> for details

In Process.php line 1204:

  The process "phpunit --debug -c example/phpunit.all.xml" exceeded the timeout of 300 seconds.


test [--dev] [--no-dev] [--] [<args>...]
Sorry, not locally! Actually I am using a docker image to do this:
Copy code
docker run --rm --interactive --tty --volume $PWD:/app composer test
y
which docker image? is it alpine based at all?
If you can create a repro repo, happy to take a look
m
Thanks 🙂 I ran the above using the official composer image: https://hub.docker.com/_/composer
y
m
Ah, yes. This has worked! Thank you so much 😄
y
saweet
m
@Yousaf Nabi (pactflow.io) Is there anything I need to do in particular to get my output file to be a collection of interactions? At the moment, each test seems to be overwriting the previous. I thought maybe using the
<extension class="PhpPact\Consumer\Listener\PactTestListener">
in my phpunit.xml but that doesn't seem to work with my version of phpunit (9.5.28). At the end of each test should I be calling $builder->writePact() or finalize() or neither? I'm not using pact-broker just yet. I am building up to that. I'm still just trying to get a poc working here.
Ah, hold on. I am now seeing the setPactFileWriteMode method!
🙌 1
y
was about to mention that and great, starting a poc without the broker is a great first step 👍