From b99fb4f2552ef56346da147f79ce682ad3aed2ac Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Fri, 21 May 2021 19:54:17 +0300 Subject: [PATCH] fix for tests & json decoder trait --- .github/workflows/tests.yml | 1 + README.md | 2 +- composer.json | 3 +-- src/Matchers/XmlBodyMatcher.php | 3 +-- src/Traits/JsonDecoderTrait.php | 10 +++++----- tests/src/PockBuilderTest.php | 35 +++++++++++++++++++-------------- 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 96e88b0..9d5147f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,6 +23,7 @@ jobs: with: php-version: ${{ matrix.php-version }} coverage: pcov + extensions: uopz - name: Composer cache uses: actions/cache@v2 with: diff --git a/README.md b/README.md index eaaacf0..b4e8ea4 100644 --- a/README.md +++ b/README.md @@ -123,5 +123,5 @@ In order to use unsupported serializer you should create a decorator which imple - [x] `replyWithCallback` - reply using specified callback. - [x] `replyWithFactory` - reply using specified response factory (provide corresponding interface). - [ ] `symfony/http-client` support. -- [ ] Compare XML bodies using `DOMDocument`, fallback to text comparison in case of problems. +- [x] Compare XML bodies using `DOMDocument`, fallback to text comparison in case of problems. - [ ] Document everything (with examples if it’s feasible). diff --git a/composer.json b/composer.json index 5c60588..01028f6 100644 --- a/composer.json +++ b/composer.json @@ -47,8 +47,7 @@ "jms/serializer": "^2 | ^3.12", "symfony/phpunit-bridge": "^5.2", "symfony/serializer": "^5.2", - "symfony/property-access": "^5.2", - "php-mock/php-mock": "^2.3" + "symfony/property-access": "^5.2" }, "provide": { "psr/http-client-implementation": "1.0", diff --git a/src/Matchers/XmlBodyMatcher.php b/src/Matchers/XmlBodyMatcher.php index 5c6fde3..2fc932b 100644 --- a/src/Matchers/XmlBodyMatcher.php +++ b/src/Matchers/XmlBodyMatcher.php @@ -11,11 +11,10 @@ namespace Pock\Matchers; use DOMDocument; use Pock\Exception\XmlException; -use Throwable; -use XSLTProcessor; use Pock\Traits\SeekableStreamDataExtractor; use Psr\Http\Message\RequestInterface; use RuntimeException; +use XSLTProcessor; /** * Class XmlBodyMatcher diff --git a/src/Traits/JsonDecoderTrait.php b/src/Traits/JsonDecoderTrait.php index 55f36fc..d2daf81 100644 --- a/src/Traits/JsonDecoderTrait.php +++ b/src/Traits/JsonDecoderTrait.php @@ -22,11 +22,11 @@ trait JsonDecoderTrait /** * json_decode which throws exception on error. * - * @param string $json - * @param bool|null $associative - * @param int $depth + * @param string $json + * @param bool $associative + * @param int $depth * - * @param int $flags + * @param int $flags * * @return mixed * @throws \Pock\Exception\JsonException @@ -35,7 +35,7 @@ trait JsonDecoderTrait */ public static function jsonDecode( string $json, - ?bool $associative = false, + bool $associative = false, int $depth = 512, int $flags = 0 ) { diff --git a/tests/src/PockBuilderTest.php b/tests/src/PockBuilderTest.php index 5c2aab8..cc9a0cc 100644 --- a/tests/src/PockBuilderTest.php +++ b/tests/src/PockBuilderTest.php @@ -10,7 +10,9 @@ namespace Pock\Tests; use DOMDocument; +use phpmock\Mock; use phpmock\MockBuilder; +use phpmock\MockRegistry; use Pock\Enum\RequestMethod; use Pock\Enum\RequestScheme; use Pock\Exception\UnsupportedRequestException; @@ -33,6 +35,11 @@ use RuntimeException; */ class PockBuilderTest extends PockTestCase { + protected function tearDown(): void + { + uopz_unset_return('extension_loaded'); + } + public function testNoHit(): void { $this->expectException(UnsupportedRequestException::class); @@ -453,22 +460,21 @@ EOF; $this->expectException(UnsupportedRequestException::class); } - $mock = (new MockBuilder())->setNamespace('Pock\Matchers') - ->setName('extension_loaded') - ->setFunction( - static function (string $extension) { - if ('xsl' === $extension) { - return false; - } - - return \extension_loaded($extension); - } - )->build(); - $mock->enable(); - $document = new DOMDocument(); $document->loadXML($simpleObject); + uopz_set_return( + 'extension_loaded', + function (string $extension) { + if ('xsl' === $extension) { + return false; + } + + return true; + }, + true + ); + $builder = new PockBuilder(); $builder->matchMethod(RequestMethod::GET) ->matchScheme(RequestScheme::HTTPS) @@ -479,8 +485,6 @@ EOF; ->withHeader('Content-Type', 'text/xml') ->withXml(['error' => 'Forbidden']); - $mock->disable(); - $response = $builder->getClient()->sendRequest( self::getPsr17Factory() ->createRequest(RequestMethod::GET, self::TEST_URI) @@ -868,6 +872,7 @@ EOF; EOF; + return [ [$simpleObject, true], [$simpleObject . "\n", false]