fix for query matching, very basic XML body matching, fix grammar in the readme

This commit is contained in:
Pavel 2021-05-15 20:28:41 +03:00
parent 285136a674
commit b68c11976a
5 changed files with 40 additions and 4 deletions

View File

@ -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 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). 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. if you want to override default behavior.
```php ```php

View File

@ -44,7 +44,7 @@ class QueryMatcher extends AbstractArrayPoweredComponent implements RequestMatch
return false; return false;
} }
return self::isNeedlePresentInHaystack($this->query, $query); return self::recursiveNeedlePresentInHaystack($this->query, $query);
} }
/** /**

View File

@ -33,6 +33,7 @@ use Pock\Matchers\SchemeMatcher;
use Pock\Matchers\UriMatcher; use Pock\Matchers\UriMatcher;
use Pock\Traits\JsonDecoderTrait; use Pock\Traits\JsonDecoderTrait;
use Pock\Traits\JsonSerializerAwareTrait; use Pock\Traits\JsonSerializerAwareTrait;
use Pock\Traits\XmlSerializerAwareTrait;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Throwable; use Throwable;
@ -49,6 +50,7 @@ class PockBuilder
{ {
use JsonDecoderTrait; use JsonDecoderTrait;
use JsonSerializerAwareTrait; use JsonSerializerAwareTrait;
use XmlSerializerAwareTrait;
/** @var \Pock\Matchers\MultipleMatcher */ /** @var \Pock\Matchers\MultipleMatcher */
private $matcher; 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. * Match request using provided callback. Callback should receive RequestInterface and return boolean.
* If returned value is true then request is matched. * If returned value is true then request is matched.

View File

@ -280,17 +280,21 @@ class PockBuilderTest extends PockTestCase
EOF; EOF;
$builder = new PockBuilder(); $builder = new PockBuilder();
$builder->matchMethod(RequestMethod::GET) $builder->matchMethod(RequestMethod::GET)
->matchScheme(RequestScheme::HTTPS) ->matchScheme(RequestScheme::HTTPS)
->matchHost(self::TEST_HOST) ->matchHost(self::TEST_HOST)
->matchXmlBody(new SimpleObject())
->reply(403) ->reply(403)
->withHeader('Content-Type', 'text/xml') ->withHeader('Content-Type', 'text/xml')
->withXml(['error' => 'Forbidden']); ->withXml(['error' => 'Forbidden']);
$response = $builder->getClient()->sendRequest( $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()); self::assertEquals(403, $response->getStatusCode());

View File

@ -13,6 +13,9 @@ use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Pock\Enum\RequestMethod; use Pock\Enum\RequestMethod;
use Pock\Enum\RequestScheme; use Pock\Enum\RequestScheme;
use Pock\Factory\JsonSerializerFactory;
use Pock\Factory\XmlSerializerFactory;
use Pock\Serializer\SerializerInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
/** /**
@ -52,4 +55,14 @@ abstract class PockTestCase extends TestCase
return static::$psr17Factory; return static::$psr17Factory;
} }
protected static function getJsonSerializer(): SerializerInterface
{
return JsonSerializerFactory::create();
}
protected static function getXmlSerializer(): SerializerInterface
{
return XmlSerializerFactory::create();
}
} }