mirror of
https://github.com/Neur0toxine/pock.git
synced 2024-11-28 15:56:09 +03:00
fix for query matching, very basic XML body matching, fix grammar in the readme
This commit is contained in:
parent
285136a674
commit
b68c11976a
@ -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
|
||||
|
@ -44,7 +44,7 @@ class QueryMatcher extends AbstractArrayPoweredComponent implements RequestMatch
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::isNeedlePresentInHaystack($this->query, $query);
|
||||
return self::recursiveNeedlePresentInHaystack($this->query, $query);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
|
@ -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());
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user