slince / shopify-api-php

:rocket: Shopify API Client for PHP
MIT License
128 stars 48 forks source link

AccessToken passing error at PublicAppCredential #39

Closed maximzasorin closed 5 years ago

maximzasorin commented 5 years ago

Hello, it seems that it is impossible to use the library now, there is an error when using PublicAppCredential with a clean installation and tests are not performed correctly.

When I run the tests, I get this error:

1) Slince\Shopify\Tests\PublicAppCredentialTest::testApplyToRequest
InvalidArgumentException: Header value must be scalar or null but Slince\Shopify\AccessToken provided.

./shopify-api-php/vendor/guzzlehttp/psr7/src/MessageTrait.php:190
./shopify-api-php/vendor/guzzlehttp/psr7/src/MessageTrait.php:197
./shopify-api-php/vendor/guzzlehttp/psr7/src/MessageTrait.php:162
./shopify-api-php/vendor/guzzlehttp/psr7/src/MessageTrait.php:70
./shopify-api-php/src/PublicAppCredential.php:43
./shopify-api-php/tests/PublicAppCredentialTest.php:34

I investigated the problem and found the cause in the new version of the library guzzlehttp/psr7. In version 1.6.0 they added more careful checks of passed header values, and now passing AccessToken as the header value throws an exception. With the latest version 1.6.1, the problem is also reproduced.

This is what the condition for checking header values looks like:

if (!is_scalar($value) && null !== $value) {
    throw new \InvalidArgumentException(sprintf(
        'Header value must be scalar or null but %s provided.',
        is_object($value) ? get_class($value) : gettype($value)
    ));
}

I think we can use an explicit string conversion:

public function applyToRequest(RequestInterface $request)
{
    return $request->withHeader('X-Shopify-Access-Token', (string) $this->getAccessToken());
}

Or to specify the guzzlehttp/psr7 version:

"require": {
    "guzzlehttp/psr7": "~1.5.0"
}
slince commented 5 years ago

👍

slince commented 5 years ago

fixed