From b68c11976a4b5fb58dc92c058799d283cc4fdb1f Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Sat, 15 May 2021 20:28:41 +0300 Subject: [PATCH] fix for query matching, very basic XML body matching, fix grammar in the readme --- README.md | 2 +- src/Matchers/QueryMatcher.php | 2 +- src/PockBuilder.php | 19 +++++++++++++++++++ tests/src/PockBuilderTest.php | 8 ++++++-- tests/utils/PockTestCase.php | 13 +++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 509d5ab..b9bc238 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ pock supports JMS serializer and Symfony serializer out of the box. Available se It will be used to serialize requests and responses in mocks which means you actually can pass an entire DTO into the corresponding methods (for example, `matchJsonBody` as an assertion or `withJsonBody` to generate a response body). -By default JMS serializer has more priority than the Symfony serializer. You can use methods below before running tests (`bootstrap.php`) +By default, JMS serializer has more priority than the Symfony serializer. You can use methods below before running tests (`bootstrap.php`) if you want to override default behavior. ```php diff --git a/src/Matchers/QueryMatcher.php b/src/Matchers/QueryMatcher.php index 01e4fe1..26a5220 100644 --- a/src/Matchers/QueryMatcher.php +++ b/src/Matchers/QueryMatcher.php @@ -44,7 +44,7 @@ class QueryMatcher extends AbstractArrayPoweredComponent implements RequestMatch return false; } - return self::isNeedlePresentInHaystack($this->query, $query); + return self::recursiveNeedlePresentInHaystack($this->query, $query); } /** diff --git a/src/PockBuilder.php b/src/PockBuilder.php index 1e595d1..f6768e4 100644 --- a/src/PockBuilder.php +++ b/src/PockBuilder.php @@ -33,6 +33,7 @@ use Pock\Matchers\SchemeMatcher; use Pock\Matchers\UriMatcher; use Pock\Traits\JsonDecoderTrait; use Pock\Traits\JsonSerializerAwareTrait; +use Pock\Traits\XmlSerializerAwareTrait; use Psr\Http\Client\ClientInterface; use Throwable; @@ -49,6 +50,7 @@ class PockBuilder { use JsonDecoderTrait; use JsonSerializerAwareTrait; + use XmlSerializerAwareTrait; /** @var \Pock\Matchers\MultipleMatcher */ private $matcher; @@ -272,6 +274,23 @@ class PockBuilder )); } + /** + * Match XML request body. + * + * **Note:** this method will use string comparison for now. It'll be improved in future. + * + * @todo Don't use simple string comparison. Match the entire body by its DOM. + * + * @param mixed $data + * + * @return self + * @throws \Pock\Exception\XmlException + */ + public function matchXmlBody($data): self + { + return $this->matchBody(self::serializeXml($data) ?? ''); + } + /** * Match request using provided callback. Callback should receive RequestInterface and return boolean. * If returned value is true then request is matched. diff --git a/tests/src/PockBuilderTest.php b/tests/src/PockBuilderTest.php index a9b3dbd..bd5effc 100644 --- a/tests/src/PockBuilderTest.php +++ b/tests/src/PockBuilderTest.php @@ -280,17 +280,21 @@ class PockBuilderTest extends PockTestCase EOF; - $builder = new PockBuilder(); $builder->matchMethod(RequestMethod::GET) ->matchScheme(RequestScheme::HTTPS) ->matchHost(self::TEST_HOST) + ->matchXmlBody(new SimpleObject()) ->reply(403) ->withHeader('Content-Type', 'text/xml') ->withXml(['error' => 'Forbidden']); $response = $builder->getClient()->sendRequest( - self::getPsr17Factory()->createRequest(RequestMethod::GET, self::TEST_URI) + self::getPsr17Factory() + ->createRequest(RequestMethod::GET, self::TEST_URI) + ->withBody(self::getPsr17Factory()->createStream( + self::getXmlSerializer()->serialize(new SimpleObject()) + )) ); self::assertEquals(403, $response->getStatusCode()); diff --git a/tests/utils/PockTestCase.php b/tests/utils/PockTestCase.php index e60bcb6..04d1f3d 100644 --- a/tests/utils/PockTestCase.php +++ b/tests/utils/PockTestCase.php @@ -13,6 +13,9 @@ use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\TestCase; use Pock\Enum\RequestMethod; use Pock\Enum\RequestScheme; +use Pock\Factory\JsonSerializerFactory; +use Pock\Factory\XmlSerializerFactory; +use Pock\Serializer\SerializerInterface; use Psr\Http\Message\RequestInterface; /** @@ -52,4 +55,14 @@ abstract class PockTestCase extends TestCase return static::$psr17Factory; } + + protected static function getJsonSerializer(): SerializerInterface + { + return JsonSerializerFactory::create(); + } + + protected static function getXmlSerializer(): SerializerInterface + { + return XmlSerializerFactory::create(); + } }