tienvx / codeception-pact-php-extension

Enable contract testing using Pact PHP in Codeception
MIT License
4 stars 2 forks source link

Usage test examples missing #8

Open ktogias opened 3 years ago

ktogias commented 3 years ago

I have installed tienvx/codeception-pact-php-extension and enabled it in my acceptance.suite.yml according to README. I made a free account at pactflow.io and configured PACT_BROKER_URI and PACT_BROKER_BEARER_TOKEN in my acceptance.suite.yml I have a simple Codeception Cest in my project
When I codecept run acceptance pact-mock-service is started, my cest is run and pass and then an empty pact file is uploaded to broker.
Everything seems to work, but no PACT testing is happening. I cant find out how to write a codeception test in my suite that will generate a PACT file and run a PACT test as provider or as a consumer.

I would be nice to provide some test examples for someone with no PACT experience.

tienvx commented 3 years ago

Hi @ktogias , It's great that you use my extension and report issue back.

I have something to share, hope it help you with your project:

So, can you try it out and post the result here? I'm happy to help you further.

ktogias commented 3 years ago

Hi @tienvx ,

Thanks for your quick feedback.

Following your examples I made it to successfully run consumer PACT tests and upload the PACT to broker with no problem.

I had no such easy success with provider testing though. VerifierConfig and CodeceptionPactPhp\Standalone\ProviderVerifier\ExtendedVerifier seem to ignore suite.yml PACT_ options.

I managed to overcome the problem by manualy setting up VerifierConfig and BrokerHttpClient in my Cest _before():

$this->veriferConfig = new PhpPact\Standalone\ProviderVerifier\Model\VerifierConfig();
$this->veriferConfig->setBrokerUri(new \GuzzleHttp\Psr7\Uri('https://<myname>.pactflow.io'));
$this->veriferConfig->setProviderName('HelloService');
$this->veriferConfig->setProviderBaseUrl(new \GuzzleHttp\Psr7\Uri('http://localhost'));

$this->brokerHttpClient = new BrokerHttpClient(new GuzzleClient(), $this->veriferConfig->getBrokerUri(), [
         'Authorization' => 'Bearer <MyTOKEN>',
]);

$this->verifier = new ExtendedVerifier($this->veriferConfig, null, null, $this->brokerHttpClient);

With the above hack, that I came to by looking at pact-php and CodeceptionPactPhp code, in my _before(), I can run properly a test with $this->verifier->verifyAll();

It would be good to have it solved properly inside CodeceptionPactPhp. Or maybe it already there but I have gone the wrong way?

My PACT now looks like this:

εικόνα

εικόνα

My acceptance.suite.yml is the following:

acceptance.suite.yml.txt

And my Cest is this:

HelloCest.php.txt

I don't have a consumer yet, so I included a dummy default consumer PACT test, just to generate a pact file. Quite backward logic for PACT as I start to understand, but good enough for my experiment.

My provider code, is quite simple:

<?php
header('Content-Type: application/json; charset=utf-8');
echo json_encode('hello');
tienvx commented 3 years ago

Sorry, I should mention earlier that I didn't finish coding example for provider yet. The code is still in this merge request. Please take a look at unit.suite.yaml to see the way I set it up.

Again, the bug I mentioned above prevent me from merging this merge request to master.

Can you try again and share your result here? At the end, I want to collect your feedback and update README. If you create a merge request, it would be great also.