pact-foundation / pact-php

PHP version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project
Apache License 2.0
273 stars 92 forks source link

Pact-PHP 9.x.x - Is there any support multiple providers #237

Closed ALTELMA closed 5 months ago

ALTELMA commented 2 years ago

Thanks for hardwork to create this package.

As we trying and look for more in pact-php. I did not see any example to switch or setup provider while run testing.

In my case, my app is frontend. It call to 2 or 3 services just need to generate the contracts for all.

Is it possible?

tienvx commented 2 years ago

Are these what you are looking for?

Note: this pull request is not reviewed and merge yet.

ALTELMA commented 2 years ago

@tienvx to answer you. Yes, I want this, but not sure How it works? Need to review more. And do you still using MockServerEnvConfig?

However, let's see your PR will be approved. I was wondering why your PR name is related to Rust?

tienvx commented 2 years ago

And do you still using MockServerEnvConfig?

Yes, I am trying to make the PR small and easy to migrate to new version as possible. But I removed almost half of environment variables and only keep the one that make sense.

I was wondering why your PR name is related to Rust?

Pact moved from Ruby implementation to Rust implementation. You can find the reasons here. But it's still not perfect. There are some features are missing, so you can see in my PR there are still something related to Ruby.

cfmack commented 2 years ago

By the way, thanks for the PR @tienvx . It's such a large shift that I have no had the time to do a deep dive into the Rust implementation. It's needs to happen, just a huge shift

ALTELMA commented 2 years ago

@cfmack Do you have any plan for this? I have been sawing a new request from @bethesque to add something like this.

However, while I waiting for any support for this feature. I try to go with customizing PhpPactListener by myself. To get a new configuration per test suite like the example below.

phpunit.xml

<testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="PhpPact User Tests">
            <file>./tests/Consumer/Api/UserApi.php</file>
        </testsuite>
        <testsuite name="PhpPact Order Tests">
            <file>./tests/Consumer/Api/OrderApi.php</file>
        </testsuite>
    </testsuites>
    <listeners>
        <listener class="App\Pact\PactTestListener">
            <arguments>
                <array>
                    <element key="0">
                        <string>PhpPact User Tests</string>
                    </element>
                </array>
                <array>
                    <element key="provider_name">
                        <string>RabbitMemberUserService</string>
                    </element>
                </array>
            </arguments>
        </listener>
        <listener class="App\Pact\PactTestListener">
            <arguments>
                <array>
                    <element key="0">
                        <string>PhpPact Order Tests</string>
                    </element>
                </array>
                <array>
                    <element key="provider_name">
                        <string>AuthService</string>
                    </element>
                </array>
            </arguments>
        </listener>
    </listeners>
public function __construct(array $testSuiteNames, array $config)
{
    $this->testSuiteNames   = $testSuiteNames;
    $this->mockServerConfig = new MockServerConfig();
    $this->mockServerConfig->setHost(env('PACT_MOCK_SERVER_HOST'));
    $this->mockServerConfig->setPort(env('PACT_MOCK_SERVER_PORT'));
    $this->mockServerConfig->setProvider($config['provider_name']);
    $this->mockServerConfig->setConsumer(env('PACT_CONSUMER_NAME'));
    $this->mockServerConfig->setHealthCheckRetrySec(1);
    $this->mockServerConfig->setHealthCheckTimeout(1);
}

This is a temporary solution for me. @cfmack What do you think about this? I was discussing with @mefellows to know what it should be doing about this. If you agree to go with this way. I will try to implement it like this and create PR.

YOU54F commented 1 year ago

That is a neat solution!

YOU54F commented 5 months ago

If someone still wants to implement this in the now release/9.x branch you can

https://github.com/pact-foundation/pact-php/tree/release/9.x

Please consider there is a request to update to phpunit 10 in that branch as well https://github.com/pact-foundation/pact-php/issues/338 which will impact the Listener functionality and therefore the proposed solution here

otherwise support for multiple providers in available in v10.x beta, and in the mainline master branch

Going to close this request down for now but anyone feel free to give us a shout if they want to backport to 9.x, or if they have issues trying this out with multiple providers in v10.x