Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some issues when upgrade pact-php from version 8 to 10 #703

Open
xinnings opened this issue Dec 13, 2024 · 9 comments
Open

Some issues when upgrade pact-php from version 8 to 10 #703

xinnings opened this issue Dec 13, 2024 · 9 comments

Comments

@xinnings
Copy link

xinnings commented Dec 13, 2024

  • In version 8, there is a mock server, but seems it's gone in version 10, so how to migrate that part? I assume we still need the mock server?
  • In this example of version 10, there is a class use App\Service\HttpClientService; outside the package, what is it?
  • I tried to run the testing, but I got this error, any idea how to fix it?
    [FFI\Exception] Failed loading '/app/vendor/pact-foundation/pact-php/src/PhpPact/Standalone/Installer/Model/../../../../../bin/pact-ffi-lib/pact.so'  
                                                                                                                                                            
    #1  /app/vendor/pact-foundation/pact-php/src/PhpPact/FFI/Client.php:19
    #2  /app/vendor/pact-foundation/pact-php/src/PhpPact/Consumer/Factory/InteractionDriverFactory.php:16
    #3  /app/vendor/pact-foundation/pact-php/src/PhpPact/Consumer/InteractionBuilder.php:23
    

Thanks

@tienvx
Copy link
Contributor

tienvx commented Dec 13, 2024

In version 8, there is a mock server, but seems it's gone in version 10, so how to migrate that part? I assume we still need the mock server?

No, you don't need to create mock server manually. It will be created automatically by passing true (default value) to willRespondWith().

You can ignore the mock server. It will be started automatically with random port, and will be destroyed automatically after the test.

In this example of version 10, there is a class use App\Service\HttpClientService; outside the package, what is it?

This is the code under test. It is responsible for sending the API request. It has the ability to change the API's base URI to that of the mock server.

I believe the example code is not changed since version 8: https://github.com/pact-foundation/pact-php/tree/8.1.0?tab=readme-ov-file#make-the-request

I tried to run the testing, but I got this error, any idea how to fix it?

I guess pact-php is not installed correctly. pact-php is installed by composer. You need to allow this plugin in order to make composer download extra dependencies for you:

# composer.json
{
"config": {
        "allow-plugins": {
            "pact-foundation/composer-downloads-plugin": true
        }
    },
}

composer will ask, and you just need to answer yes

@xinnings
Copy link
Author

@tienvx Thanks for your quick response

I saw all the amphp packages, including the http server, have been removed. But there is a new pact-stub-server, is it an alternate for the mock server?

I already added the setting to my composer.json, and these are the packages that have been installed, but I don't see /bin/pact-ffi-lib/pact.so file

  - Installing pact-foundation/composer-downloads-plugin (v2.1.1): Extracting archive
  - Upgrading pact-foundation/pact-php (8.1.0 => 10.0.0): Extracting archive
  - Installing pact-foundation/pact-php:pact-ffi-lib (0.4.23): Extracting archive
  - Installing pact-foundation/pact-php:pact-ffi-headers (0.4.23)
  - Installing pact-foundation/pact-php:pact-stub-server (0.6.0): Extracting archive
image

@tienvx
Copy link
Contributor

tienvx commented Dec 13, 2024

I saw all the amphp packages, including the http server, have been removed.

Yes, pact-php is no longer depend on those packages

But there is a new pact-stub-server, is it an alternate for the mock server?

No, they are not the same. Mock server is for contract testing, while stub server is for helping frontend (web/mobile) development. Here is a note from stub server home page:

Note: The stub server was designed to supporting prototyping of mobile applications by stubbing out the backend servers. It will always try to return a response, even when there is not an extract match with the pact files.

but I don't see /bin/pact-ffi-lib/pact.so file

Thanks for including the screenshot. It is supposed to pick up the dylib file. But I don't know why it choose the so extension. Can you show me the output of this command?

php -r "echo PHP_OS_FAMILY;"

If the output is Linux, I guess you installed the pact-php on a Mac machine, then run the tests inside a Linux container?

@xinnings
Copy link
Author

If the output is Linux, I guess you installed the pact-php on a Mac machine, then run the tests inside a Linux container?

@tienvx Indeed, I have the so file now. thanks a lot.

@tienvx
Copy link
Contributor

tienvx commented Dec 13, 2024

This issue is quite similar with #692

TLDR: Install on Ubuntu but run tests on Alpine, and vice versa.

@xinnings
Copy link
Author

xinnings commented Dec 13, 2024

@tienvx Another problem is that I can't find the log file.
I can see the json files were generated, but I don't see the pact.log even though there is an error.
My .env file setting is below.

PACT_OUTPUT_DIR=./tests/_output/php/pacts
PACT_LOG=./tests/_output/php/pacts/pact.log

Any idea? thanks

@xinnings
Copy link
Author

I almost make all the APIs work, except those APIs with content-type application/x-www-form-urlencoded,
The APIs with content-type application/json are working fine.

Here is how I use Guzzle to call the API

$content = $this->client->post('/oauth/token', [
        'form_params' => [
          'grant_type' => 'password',
          'username'   => $username,
          'password'   => $password,
        ],
        'headers'     => [
          'Content-Type' => 'application/x-www-form-urlencoded',
        ],
      ])->getBody()->getContents();

This is how I build the request

$request = new ConsumerRequest();
    $request
      ->setMethod('POST')
      ->setPath('/oauth/token')
      ->setBody('grant_type=password&username=homer.simpson&password=s3ecr3t')
      ->addHeader('Content-Type', 'application/x-www-form-urlencoded');

And this is the Request-Mismatch error message

[GuzzleHttp\Exception\ServerException] Server error: `POST http://localhost:7200/oauth/token` resulted in a `500 Internal Server Error` response:
{"error":"Request-Mismatch : HttpRequest { method: \"POST\", path: \"/oauth/token\", query: None, headers: Some({\"content-type\": [\"application/x-www-form-urlencoded\"], \"user-agent\": [\"GuzzleHttp/7\"], \"host\": [\"localhost:7200\"], \"content-length\": [\"59\"]}), body: Present(b\"grant_type=password&username=homer.simpson&password=s3ecr3t\", Some(ContentType { main_type: \"application\", sub_type: \"x-www-form-urlencoded\", attributes: {}, suffix: None }), None), matching_rules: MatchingRules { rules: {} }, generators: Generators { categories: {} } }"}

Just wondering if you happen to know why, thanks

@tienvx
Copy link
Contributor

tienvx commented Dec 13, 2024

Another problem is that I can't find the log file

Version 10.0 doesn't support putting logs into a file. It's supported in version 10.1, but it's currently on beta. Here is the log usage in version 10.1: https://github.com/pact-foundation/pact-php/pull/704/files

I almost make all the APIs work, except those APIs with content-type application/x-www-form-urlencoded,

I suggest you set the log level to DEBUG and check the mismatch in the log.

I guess the problem is: $username is not equals to homer.simpson and $password is not equals to s3ecr3t. Matchers are currently not supported for application/x-www-form-urlencoded, so you have to set the exact values.

I'm implementing matchers support for application/x-www-form-urlencoded. Once core maintainers merge my pull requests, I can release version 10.1, then you can use both features.

@tienvx
Copy link
Contributor

tienvx commented Dec 13, 2024

To make this issue focus, I suggest you ask questions on Slack (pact-php channel). I will answer when possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants