mirror of
https://github.com/Neur0toxine/pock.git
synced 2024-12-01 01:16:07 +03:00
new functionality and tests, documentation generator, update readme
This commit is contained in:
parent
02423dfb54
commit
25654d921f
47
.github/workflows/documentation.yml
vendored
Normal file
47
.github/workflows/documentation.yml
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
name: Documentation
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- 'master'
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: "phpDocumentor"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check GitHub Pages status
|
||||
if: ${{ github.ref != 'refs/heads/master' }}
|
||||
uses: crazy-max/ghaction-github-status@v2
|
||||
with:
|
||||
pages_threshold: major_outage
|
||||
- name: Check out code into the workspace
|
||||
if: success() && ${{ github.ref != 'refs/heads/master' }}
|
||||
uses: actions/checkout@v2
|
||||
- name: Setup PHP 7.4
|
||||
if: ${{ github.ref != 'refs/heads/master' }}
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: "7.4"
|
||||
- name: Cache phpDocumentor
|
||||
id: cache-phpdocumentor
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: phpDocumentor.phar
|
||||
key: phpdocumentor
|
||||
- name: Download latest phpDocumentor
|
||||
if: steps.cache-phpdocumentor.outputs.cache-hit != 'true'
|
||||
run: curl -O -L https://phpdoc.org/phpDocumentor.phar
|
||||
- name: Generate documentation
|
||||
if: ${{ github.ref != 'refs/heads/master' }}
|
||||
run: php phpDocumentor.phar
|
||||
- name: Deploy documentation to GitHub Pages
|
||||
if: ${{ github.ref != 'refs/heads/master' }}
|
||||
uses: crazy-max/ghaction-github-pages@v2
|
||||
with:
|
||||
target_branch: gh-pages
|
||||
build_dir: docs/build/html
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
30
.gitignore
vendored
30
.gitignore
vendored
@ -1,6 +1,30 @@
|
||||
# Composer files.
|
||||
/vendor
|
||||
composer.lock
|
||||
vendor/*
|
||||
.idea/*
|
||||
composer.phar
|
||||
|
||||
# Code Quality tools artifacts.
|
||||
coverage.xml
|
||||
test-report.xml
|
||||
phpunit.xml
|
||||
.php_cs.cache
|
||||
.phpunit.result.cache
|
||||
test-report.xml
|
||||
|
||||
# phpDocumentor files.
|
||||
.phpdoc
|
||||
docs
|
||||
phpDocumentor.phar
|
||||
|
||||
# Ignore autogenerated code.
|
||||
models/*.php
|
||||
models/checksum.json
|
||||
|
||||
# Different environment-related files.
|
||||
.idea
|
||||
.DS_Store
|
||||
.settings
|
||||
.buildpath
|
||||
.project
|
||||
.swp
|
||||
/nbproject
|
||||
.env
|
||||
|
@ -1,3 +1,8 @@
|
||||
[![Build Status](https://github.com/Neur0toxine/pock/workflows/Tests/badge.svg)](https://github.com/Neur0toxine/pock/actions)
|
||||
[![Coverage](https://img.shields.io/codecov/c/gh/Neur0toxine/pock/master.svg?logo=codecov&logoColor=white)](https://codecov.io/gh/Neur0toxine/pock)
|
||||
[![Latest stable](https://img.shields.io/packagist/v/neur0toxine/pock.svg)](https://packagist.org/packages/neur0toxine/pock)
|
||||
[![PHP from Packagist](https://img.shields.io/packagist/php-v/neur0toxine/pock.svg?logo=php&logoColor=white)](https://packagist.org/packages/neur0toxine/pock)
|
||||
|
||||
# pock
|
||||
|
||||
Easy to use HTTP mocking solution, compatible with PSR-18 and HTTPlug. Should resemble [gock](https://github.com/h2non/gock) in the end.
|
||||
|
@ -13,7 +13,7 @@ use Http\Client\HttpAsyncClient;
|
||||
use Http\Client\HttpClient;
|
||||
use Http\Client\Promise\HttpFulfilledPromise;
|
||||
use Http\Promise\Promise;
|
||||
use Pock\Exception\BrokenMockException;
|
||||
use Pock\Exception\IncompleteMockException;
|
||||
use Pock\Exception\UnsupportedRequestException;
|
||||
use Pock\Promise\HttpRejectedPromise;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
@ -87,7 +87,7 @@ class Client implements ClientInterface, HttpClient, HttpAsyncClient
|
||||
return new HttpRejectedPromise($mock->getThrowable());
|
||||
}
|
||||
|
||||
throw new BrokenMockException($mock);
|
||||
throw new IncompleteMockException($mock);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
/**
|
||||
* PHP 7.2
|
||||
*
|
||||
* @category BrokenMockException
|
||||
* @category IncompleteMockException
|
||||
* @package Pock\Exception
|
||||
*/
|
||||
|
||||
@ -13,12 +13,12 @@ use Exception;
|
||||
use Pock\MockInterface;
|
||||
|
||||
/**
|
||||
* Class BrokenMockException
|
||||
* Class IncompleteMockException
|
||||
*
|
||||
* @category BrokenMockException
|
||||
* @category IncompleteMockException
|
||||
* @package Pock\Exception
|
||||
*/
|
||||
class BrokenMockException extends Exception
|
||||
class IncompleteMockException extends Exception
|
||||
{
|
||||
/** @var \Pock\MockInterface */
|
||||
private $mock;
|
22
src/Exception/XmlException.php
Normal file
22
src/Exception/XmlException.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category XmlException
|
||||
* @package Pock\Exception
|
||||
*/
|
||||
|
||||
namespace Pock\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class XmlException
|
||||
*
|
||||
* @category XmlException
|
||||
* @package Pock\Exception
|
||||
*/
|
||||
class XmlException extends Exception
|
||||
{
|
||||
}
|
@ -29,7 +29,7 @@ class HostMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function __construct(string $host)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->host = strtolower($host);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,6 +37,6 @@ class HostMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function matches(RequestInterface $request): bool
|
||||
{
|
||||
return strtolower($request->getUri()->getHost()) === strtolower($this->host);
|
||||
return strtolower($request->getUri()->getHost()) === $this->host;
|
||||
}
|
||||
}
|
||||
|
43
src/Matchers/MethodMatcher.php
Normal file
43
src/Matchers/MethodMatcher.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category MethodMatcher
|
||||
* @package Pock\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Matchers;
|
||||
|
||||
use Pock\Enum\RequestMethod;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Class MethodMatcher
|
||||
*
|
||||
* @category MethodMatcher
|
||||
* @package Pock\Matchers
|
||||
*/
|
||||
class MethodMatcher implements RequestMatcherInterface
|
||||
{
|
||||
/** @var string */
|
||||
private $method;
|
||||
|
||||
/**
|
||||
* MethodMatcher constructor.
|
||||
*
|
||||
* @param string $method
|
||||
*/
|
||||
public function __construct(string $method = RequestMethod::GET)
|
||||
{
|
||||
$this->method = strtoupper($method);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function matches(RequestInterface $request): bool
|
||||
{
|
||||
return strtoupper($request->getMethod()) === $this->method;
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ class SchemeMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function __construct(string $scheme = RequestScheme::HTTP)
|
||||
{
|
||||
$this->scheme = $scheme;
|
||||
$this->scheme = strtolower($scheme);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -38,6 +38,6 @@ class SchemeMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function matches(RequestInterface $request): bool
|
||||
{
|
||||
return strtolower($request->getUri()->getScheme()) === strtolower($this->scheme);
|
||||
return strtolower($request->getUri()->getScheme()) === $this->scheme;
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,11 @@
|
||||
|
||||
namespace Pock;
|
||||
|
||||
use Pock\Enum\RequestMethod;
|
||||
use Pock\Enum\RequestScheme;
|
||||
use Pock\Matchers\AnyRequestMatcher;
|
||||
use Pock\Matchers\HostMatcher;
|
||||
use Pock\Matchers\MethodMatcher;
|
||||
use Pock\Matchers\MultipleMatcher;
|
||||
use Pock\Matchers\RequestMatcherInterface;
|
||||
use Pock\Matchers\SchemeMatcher;
|
||||
@ -52,6 +54,18 @@ class PockBuilder
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Match request by its method.
|
||||
*
|
||||
* @param string $method
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function matchMethod(string $method): self
|
||||
{
|
||||
return $this->addMatcher(new MethodMatcher($method));
|
||||
}
|
||||
|
||||
/**
|
||||
* Match request by its scheme.
|
||||
*
|
||||
@ -59,7 +73,7 @@ class PockBuilder
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function matchScheme(string $scheme = RequestScheme::HTTP): PockBuilder
|
||||
public function matchScheme(string $scheme): self
|
||||
{
|
||||
return $this->addMatcher(new SchemeMatcher($scheme));
|
||||
}
|
||||
@ -71,7 +85,7 @@ class PockBuilder
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function matchHost(string $host): PockBuilder
|
||||
public function matchHost(string $host): self
|
||||
{
|
||||
return $this->addMatcher(new HostMatcher($host));
|
||||
}
|
||||
@ -81,9 +95,9 @@ class PockBuilder
|
||||
*
|
||||
* @param \Psr\Http\Message\UriInterface|string $uri
|
||||
*
|
||||
* @return \Pock\PockBuilder
|
||||
* @return self
|
||||
*/
|
||||
public function matchUri($uri): PockBuilder
|
||||
public function matchUri($uri): self
|
||||
{
|
||||
return $this->addMatcher(new UriMatcher($uri));
|
||||
}
|
||||
@ -93,9 +107,9 @@ class PockBuilder
|
||||
*
|
||||
* @param \Pock\Matchers\RequestMatcherInterface $matcher
|
||||
*
|
||||
* @return \Pock\PockBuilder
|
||||
* @return self
|
||||
*/
|
||||
public function addMatcher(RequestMatcherInterface $matcher): PockBuilder
|
||||
public function addMatcher(RequestMatcherInterface $matcher): self
|
||||
{
|
||||
$this->closePrevious();
|
||||
$this->matcher->addMatcher($matcher);
|
||||
@ -111,7 +125,7 @@ class PockBuilder
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function repeat(int $hits): PockBuilder
|
||||
public function repeat(int $hits): self
|
||||
{
|
||||
if ($hits > 0) {
|
||||
$this->maxHits = $hits;
|
||||
@ -139,9 +153,9 @@ class PockBuilder
|
||||
/**
|
||||
* Resets the builder.
|
||||
*
|
||||
* @return \Pock\PockBuilder
|
||||
* @return self
|
||||
*/
|
||||
public function reset(): PockBuilder
|
||||
public function reset(): self
|
||||
{
|
||||
$this->matcher = new MultipleMatcher();
|
||||
$this->responseBuilder = null;
|
||||
@ -157,9 +171,9 @@ class PockBuilder
|
||||
*
|
||||
* @param \Psr\Http\Client\ClientInterface|null $fallbackClient
|
||||
*
|
||||
* @return \Pock\PockBuilder
|
||||
* @return self
|
||||
*/
|
||||
public function setFallbackClient(?ClientInterface $fallbackClient = null): PockBuilder
|
||||
public function setFallbackClient(?ClientInterface $fallbackClient = null): self
|
||||
{
|
||||
$this->fallbackClient = $fallbackClient;
|
||||
return $this;
|
||||
@ -170,6 +184,7 @@ class PockBuilder
|
||||
*/
|
||||
public function getClient(): Client
|
||||
{
|
||||
$this->closePrevious();
|
||||
return new Client($this->mocks, $this->fallbackClient);
|
||||
}
|
||||
|
||||
|
@ -13,10 +13,12 @@ use InvalidArgumentException;
|
||||
use JsonSerializable;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Pock\Exception\JsonException;
|
||||
use Pock\Exception\XmlException;
|
||||
use Pock\Factory\JsonSerializerFactory;
|
||||
use Pock\Factory\XmlSerializerFactory;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class PockResponseBuilder
|
||||
@ -27,16 +29,16 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class PockResponseBuilder
|
||||
{
|
||||
/** @var \Psr\Http\Message\ResponseInterface */
|
||||
private $response;
|
||||
protected $response;
|
||||
|
||||
/** @var Psr17Factory */
|
||||
private $factory;
|
||||
protected $factory;
|
||||
|
||||
/** @var SerializerInterface|null */
|
||||
private static $jsonSerializer;
|
||||
protected static $jsonSerializer;
|
||||
|
||||
/** @var SerializerInterface|null */
|
||||
private static $xmlSerializer;
|
||||
protected static $xmlSerializer;
|
||||
|
||||
/**
|
||||
* PockResponseBuilder constructor.
|
||||
@ -54,15 +56,79 @@ class PockResponseBuilder
|
||||
*
|
||||
* @param int $statusCode
|
||||
*
|
||||
* @return \Pock\PockResponseBuilder
|
||||
* @return self
|
||||
*/
|
||||
public function withStatusCode(int $statusCode = 200): PockResponseBuilder
|
||||
public function withStatusCode(int $statusCode = 200): self
|
||||
{
|
||||
$this->response = $this->response->withStatus($statusCode);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond with specified header value.
|
||||
* @see \Psr\Http\Message\MessageInterface::withHeader()
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|string[] $value
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function withHeader(string $name, $value): self
|
||||
{
|
||||
$this->response = $this->response->withHeader($name, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond with specified header value appended to existing header.
|
||||
* @see \Psr\Http\Message\MessageInterface::withAddedHeader()
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|string[] $value
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function withAddedHeader(string $name, $value): self
|
||||
{
|
||||
$this->response = $this->response->withAddedHeader($name, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond with specified headers. Works exactly like calling PockResponseBuilder::withHeader() would work.
|
||||
*
|
||||
* @param array<string, string|string[]> $headers
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function withHeaders(array $headers): self
|
||||
{
|
||||
foreach ($headers as $name => $value) {
|
||||
$this->response = $this->response->withHeader($name, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond with specified headers. Works exactly like calling PockResponseBuilder::withAddedHeader() would work.
|
||||
*
|
||||
* @param array<string, string|string[]> $headers
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function withAddedHeaders(array $headers): self
|
||||
{
|
||||
foreach ($headers as $name => $value) {
|
||||
$this->response = $this->response->withAddedHeader($name, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reply with specified body. It can be:
|
||||
* - PSR-7 StreamInterface - it will be used without any changes.
|
||||
@ -71,9 +137,9 @@ class PockResponseBuilder
|
||||
*
|
||||
* @param \Psr\Http\Message\StreamInterface|resource|string $stream
|
||||
*
|
||||
* @return $this
|
||||
* @return self
|
||||
*/
|
||||
public function withBody($stream): PockResponseBuilder
|
||||
public function withBody($stream): self
|
||||
{
|
||||
if (is_string($stream)) {
|
||||
$stream = $this->factory->createStream($stream);
|
||||
@ -83,6 +149,33 @@ class PockResponseBuilder
|
||||
$stream = $this->factory->createStreamFromResource($stream);
|
||||
}
|
||||
|
||||
if ($stream->isSeekable()) {
|
||||
$stream->seek(0);
|
||||
}
|
||||
|
||||
$this->response = $this->response->withBody($stream);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reply with data from specified file.
|
||||
* For available modes @see \fopen()
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
*
|
||||
* @return self
|
||||
* @throws InvalidArgumentException|RuntimeException
|
||||
*/
|
||||
public function withFile(string $path, string $mode = 'r'): self
|
||||
{
|
||||
$stream = $this->factory->createStreamFromFile($path, $mode);
|
||||
|
||||
if ($stream->isSeekable()) {
|
||||
$stream->seek(0);
|
||||
}
|
||||
|
||||
$this->response = $this->response->withBody($stream);
|
||||
|
||||
return $this;
|
||||
@ -91,10 +184,10 @@ class PockResponseBuilder
|
||||
/**
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return $this
|
||||
* @return self
|
||||
* @throws \Pock\Exception\JsonException
|
||||
*/
|
||||
public function withJson($data): PockResponseBuilder
|
||||
public function withJson($data): self
|
||||
{
|
||||
if (is_string($data) || is_numeric($data)) {
|
||||
return $this->withBody((string) $data);
|
||||
@ -118,10 +211,10 @@ class PockResponseBuilder
|
||||
/**
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return $this
|
||||
* @throws \Pock\Exception\JsonException
|
||||
* @return self
|
||||
* @throws \Pock\Exception\XmlException
|
||||
*/
|
||||
public function withXml($data): PockResponseBuilder
|
||||
public function withXml($data): self
|
||||
{
|
||||
if (is_string($data)) {
|
||||
return $this->withBody($data);
|
||||
@ -150,7 +243,7 @@ class PockResponseBuilder
|
||||
* @return string
|
||||
* @throws \Pock\Exception\JsonException
|
||||
*/
|
||||
private static function jsonEncode($data): string
|
||||
protected static function jsonEncode($data): string
|
||||
{
|
||||
$data = json_encode($data);
|
||||
|
||||
@ -167,7 +260,7 @@ class PockResponseBuilder
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.StaticAccess)
|
||||
*/
|
||||
private static function jsonSerializer(): SerializerInterface
|
||||
protected static function jsonSerializer(): SerializerInterface
|
||||
{
|
||||
if (null !== static::$jsonSerializer) {
|
||||
return static::$jsonSerializer;
|
||||
@ -186,11 +279,11 @@ class PockResponseBuilder
|
||||
|
||||
/**
|
||||
* @return \Pock\Serializer\SerializerInterface
|
||||
* @throws \Pock\Exception\JsonException
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.StaticAccess)
|
||||
* @throws \Pock\Exception\XmlException
|
||||
*/
|
||||
private static function xmlSerializer(): SerializerInterface
|
||||
protected static function xmlSerializer(): SerializerInterface
|
||||
{
|
||||
if (null !== static::$xmlSerializer) {
|
||||
return static::$xmlSerializer;
|
||||
@ -199,7 +292,7 @@ class PockResponseBuilder
|
||||
$serializer = XmlSerializerFactory::create();
|
||||
|
||||
if (null === $serializer) {
|
||||
throw new JsonException('No XML serializer available');
|
||||
throw new XmlException('No XML serializer available');
|
||||
}
|
||||
|
||||
static::$xmlSerializer = $serializer;
|
||||
|
@ -14,6 +14,8 @@ use Pock\Factory\JsonSerializerFactory;
|
||||
use Pock\Factory\XmlSerializerFactory;
|
||||
use Pock\Serializer\CallbackSerializerDecorator;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
use Pock\TestUtils\EmptyJsonSerializerDecorator;
|
||||
use Pock\TestUtils\EmptyXmlSerializerDecorator;
|
||||
|
||||
/**
|
||||
* Class AbstractSerializerFactoryTest
|
||||
@ -25,12 +27,10 @@ class AbstractSerializerFactoryTest extends TestCase
|
||||
{
|
||||
public function testSetSerializer(): void
|
||||
{
|
||||
$jsonSerializer = new CallbackSerializerDecorator(function ($data) {
|
||||
return 'jsonSerializer';
|
||||
});
|
||||
$xmlSerializer = new CallbackSerializerDecorator(function ($data) {
|
||||
return 'xmlSerializer';
|
||||
});
|
||||
$jsonSerializer = new EmptyJsonSerializerDecorator();
|
||||
$xmlSerializer = new EmptyXmlSerializerDecorator();
|
||||
|
||||
self::assertNotEquals($jsonSerializer, $xmlSerializer);
|
||||
|
||||
JsonSerializerFactory::setSerializer($jsonSerializer);
|
||||
XmlSerializerFactory::setSerializer($xmlSerializer);
|
||||
|
@ -10,6 +10,7 @@
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Matchers\AnyRequestMatcher;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
|
||||
/**
|
||||
* Class AnyRequestMatcherTest
|
||||
@ -17,7 +18,7 @@ use Pock\Matchers\AnyRequestMatcher;
|
||||
* @category AnyRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class AnyRequestMatcherTest extends AbstractRequestMatcherTest
|
||||
class AnyRequestMatcherTest extends PockTestCase
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
|
@ -10,6 +10,7 @@
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Matchers\HostMatcher;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
|
||||
/**
|
||||
* Class HostMatcherTest
|
||||
@ -17,7 +18,7 @@ use Pock\Matchers\HostMatcher;
|
||||
* @category HostMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class HostMatcherTest extends AbstractRequestMatcherTest
|
||||
class HostMatcherTest extends PockTestCase
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
|
@ -13,6 +13,7 @@ use Pock\Enum\RequestMethod;
|
||||
use Pock\Matchers\AnyRequestMatcher;
|
||||
use Pock\Matchers\HostMatcher;
|
||||
use Pock\Matchers\MultipleMatcher;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
|
||||
/**
|
||||
* Class MultipleMatcherTest
|
||||
@ -20,7 +21,7 @@ use Pock\Matchers\MultipleMatcher;
|
||||
* @category MultipleMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class MultipleMatcherTest extends AbstractRequestMatcherTest
|
||||
class MultipleMatcherTest extends PockTestCase
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
|
@ -11,6 +11,7 @@ namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Enum\RequestScheme;
|
||||
use Pock\Matchers\SchemeMatcher;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
|
||||
/**
|
||||
* Class SchemeMatcherTest
|
||||
@ -18,7 +19,7 @@ use Pock\Matchers\SchemeMatcher;
|
||||
* @category SchemeMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class SchemeMatcherTest extends AbstractRequestMatcherTest
|
||||
class SchemeMatcherTest extends PockTestCase
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
|
@ -10,6 +10,7 @@
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Matchers\UriMatcher;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
|
||||
/**
|
||||
* Class UriMatcherTest
|
||||
@ -17,7 +18,7 @@ use Pock\Matchers\UriMatcher;
|
||||
* @category UriMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class UriMatcherTest extends AbstractRequestMatcherTest
|
||||
class UriMatcherTest extends PockTestCase
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
|
95
tests/src/PockBuilderTest.php
Normal file
95
tests/src/PockBuilderTest.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category PockBuilderTest
|
||||
* @package Pock\Tests
|
||||
*/
|
||||
|
||||
namespace Pock\Tests;
|
||||
|
||||
use Pock\Enum\RequestMethod;
|
||||
use Pock\Enum\RequestScheme;
|
||||
use Pock\Exception\UnsupportedRequestException;
|
||||
use Pock\PockBuilder;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
|
||||
/**
|
||||
* Class PockBuilderTest
|
||||
*
|
||||
* @category PockBuilderTest
|
||||
* @package Pock\Tests
|
||||
*/
|
||||
class PockBuilderTest extends PockTestCase
|
||||
{
|
||||
public function testNoHit(): void
|
||||
{
|
||||
$this->expectException(UnsupportedRequestException::class);
|
||||
(new PockBuilder())->getClient()->sendRequest(self::getPsr17Factory()
|
||||
->createRequest(RequestMethod::GET, 'https://example.com'));
|
||||
}
|
||||
|
||||
public function testTextResponse(): void
|
||||
{
|
||||
$builder = new PockBuilder();
|
||||
$builder->matchMethod(RequestMethod::GET)
|
||||
->matchScheme(RequestScheme::HTTPS)
|
||||
->matchHost('example.com')
|
||||
->reply(403)
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withBody('Forbidden');
|
||||
|
||||
$response = $builder->getClient()->sendRequest(self::getPsr17Factory()
|
||||
->createRequest(RequestMethod::GET, 'https://example.com'));
|
||||
|
||||
self::assertEquals(403, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/plain']], $response->getHeaders());
|
||||
self::assertEquals('Forbidden', $response->getBody()->getContents());
|
||||
}
|
||||
|
||||
public function testJsonResponse(): void
|
||||
{
|
||||
$builder = new PockBuilder();
|
||||
$builder->matchMethod(RequestMethod::GET)
|
||||
->matchScheme(RequestScheme::HTTPS)
|
||||
->matchHost('example.com')
|
||||
->reply(403)
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withJson(['error' => 'Forbidden']);
|
||||
|
||||
$response = $builder->getClient()->sendRequest(self::getPsr17Factory()
|
||||
->createRequest(RequestMethod::GET, 'https://example.com'));
|
||||
|
||||
self::assertEquals(403, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['application/json']], $response->getHeaders());
|
||||
self::assertEquals(['error' => 'Forbidden'], json_decode($response->getBody()->getContents(), true));
|
||||
}
|
||||
|
||||
public function testXmlResponse(): void
|
||||
{
|
||||
$xml = <<<'EOF'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<result>
|
||||
<entry><![CDATA[Forbidden]]></entry>
|
||||
</result>
|
||||
|
||||
EOF;
|
||||
|
||||
|
||||
$builder = new PockBuilder();
|
||||
$builder->matchMethod(RequestMethod::GET)
|
||||
->matchScheme(RequestScheme::HTTPS)
|
||||
->matchHost('example.com')
|
||||
->reply(403)
|
||||
->withHeader('Content-Type', 'text/xml')
|
||||
->withXml(['error' => 'Forbidden']);
|
||||
|
||||
$response = $builder->getClient()->sendRequest(self::getPsr17Factory()
|
||||
->createRequest(RequestMethod::GET, 'https://example.com'));
|
||||
|
||||
self::assertEquals(403, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/xml']], $response->getHeaders());
|
||||
self::assertEquals($xml, $response->getBody()->getContents());
|
||||
}
|
||||
}
|
184
tests/src/PockResponseBuilderTest.php
Normal file
184
tests/src/PockResponseBuilderTest.php
Normal file
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category PockResponseBuilderTest
|
||||
* @package Pock\Tests
|
||||
*/
|
||||
|
||||
namespace Pock\Tests;
|
||||
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Pock\PockResponseBuilder;
|
||||
use Pock\TestUtils\PockTestCase;
|
||||
use Pock\TestUtils\SimpleObject;
|
||||
use Pock\TestUtils\SimpleObjectJsonSerializable;
|
||||
|
||||
/**
|
||||
* Class PockResponseBuilderTest
|
||||
*
|
||||
* @category PockResponseBuilderTest
|
||||
* @package Pock\Tests
|
||||
*/
|
||||
class PockResponseBuilderTest extends PockTestCase
|
||||
{
|
||||
public function testBuildText(): void
|
||||
{
|
||||
$response = (new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withBody('test text')
|
||||
->getResponse();
|
||||
|
||||
self::assertNotNull($response);
|
||||
self::assertEquals(400, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/plain']], $response->getHeaders());
|
||||
self::assertEquals('test text', (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testBuildResource(): void
|
||||
{
|
||||
$resource = fopen(__FILE__, 'r');
|
||||
self::assertIsResource($resource);
|
||||
|
||||
$response = (new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withBody($resource)
|
||||
->getResponse();
|
||||
|
||||
self::assertNotNull($response);
|
||||
self::assertEquals(400, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/plain']], $response->getHeaders());
|
||||
self::assertStringEqualsFile(__FILE__, (string) $response->getBody());
|
||||
|
||||
fclose($resource);
|
||||
}
|
||||
|
||||
public function testBuildStream(): void
|
||||
{
|
||||
$response = (new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withBody(self::getPsr17Factory()->createStream('test text'))
|
||||
->getResponse();
|
||||
|
||||
self::assertNotNull($response);
|
||||
self::assertEquals(400, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/plain']], $response->getHeaders());
|
||||
self::assertEquals('test text', (string) $response->getBody());
|
||||
}
|
||||
|
||||
public function testBuildFile(): void
|
||||
{
|
||||
$response = (new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeader('Content-Type', 'text/plain')
|
||||
->withFile(__FILE__)
|
||||
->getResponse();
|
||||
|
||||
self::assertNotNull($response);
|
||||
self::assertEquals(400, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/plain']], $response->getHeaders());
|
||||
self::assertStringEqualsFile(__FILE__, (string) $response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider buildJsonProvider
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $expected
|
||||
*
|
||||
* @throws \Pock\Exception\JsonException
|
||||
*/
|
||||
public function testBuildJson($data, string $expected): void
|
||||
{
|
||||
$response = (new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withAddedHeader('Content-Type', 'application/json')
|
||||
->withJson($data)
|
||||
->getResponse();
|
||||
|
||||
self::assertNotNull($response);
|
||||
self::assertEquals(400, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['application/json']], $response->getHeaders());
|
||||
self::assertEquals($expected, (string) $response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Pock\Exception\JsonException
|
||||
*/
|
||||
public function testBuildJsonException(): void
|
||||
{
|
||||
$this->expectExceptionMessage('Cannot serialize data with type NULL');
|
||||
(new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withJson(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider buildXmlProvider
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $expected
|
||||
*
|
||||
* @throws \Pock\Exception\XmlException
|
||||
*/
|
||||
public function testBuildXml($data, string $expected): void
|
||||
{
|
||||
$response = (new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeaders(['Content-Type' => 'text/xml'])
|
||||
->withXml($data)
|
||||
->getResponse();
|
||||
|
||||
self::assertNotNull($response);
|
||||
self::assertEquals(400, $response->getStatusCode());
|
||||
self::assertEquals(['Content-Type' => ['text/xml']], $response->getHeaders());
|
||||
self::assertEquals($expected, (string) $response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Pock\Exception\XmlException
|
||||
*/
|
||||
public function testBuildXmlException(): void
|
||||
{
|
||||
$this->expectExceptionMessage('Cannot serialize data with type NULL');
|
||||
(new PockResponseBuilder(200))
|
||||
->withStatusCode(400)
|
||||
->withHeader('Content-Type', 'text/xml')
|
||||
->withXml(null);
|
||||
}
|
||||
|
||||
public function buildJsonProvider(): array
|
||||
{
|
||||
return [
|
||||
[1, '1'],
|
||||
['{}', '{}'],
|
||||
[['key' => 'value'], '{"key":"value"}'],
|
||||
[new SimpleObjectJsonSerializable(), SimpleObjectJsonSerializable::JSON],
|
||||
[new SimpleObject(), SimpleObject::JSON]
|
||||
];
|
||||
}
|
||||
|
||||
public function buildXmlProvider(): array
|
||||
{
|
||||
$xmlArray = <<<'EOF'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<result>
|
||||
<entry>
|
||||
<field><![CDATA[test]]></field>
|
||||
</entry>
|
||||
</result>
|
||||
|
||||
EOF;
|
||||
|
||||
return [
|
||||
[SimpleObject::XML, SimpleObject::XML],
|
||||
[new SimpleObject(), SimpleObject::XML],
|
||||
[[new SimpleObject()], $xmlArray]
|
||||
];
|
||||
}
|
||||
}
|
29
tests/utils/EmptyJsonSerializerDecorator.php
Normal file
29
tests/utils/EmptyJsonSerializerDecorator.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category EmptyJsonSerializerDecorator
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
|
||||
namespace Pock\TestUtils;
|
||||
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Class EmptyJsonSerializerDecorator
|
||||
*
|
||||
* @category EmptyJsonSerializerDecorator
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
class EmptyJsonSerializerDecorator implements SerializerInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function serialize($data): string
|
||||
{
|
||||
return '{}';
|
||||
}
|
||||
}
|
29
tests/utils/EmptyXmlSerializerDecorator.php
Normal file
29
tests/utils/EmptyXmlSerializerDecorator.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category EmptyXmlSerializerDecorator
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
|
||||
namespace Pock\TestUtils;
|
||||
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Class EmptyXmlSerializerDecorator
|
||||
*
|
||||
* @category EmptyXmlSerializerDecorator
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
class EmptyXmlSerializerDecorator implements SerializerInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function serialize($data): string
|
||||
{
|
||||
return '<?xml version="1.0" encoding="UTF-8"?>';
|
||||
}
|
||||
}
|
20
tests/utils/PockBuilderTestCase.php
Normal file
20
tests/utils/PockBuilderTestCase.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category PockBuilderTestCase
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
|
||||
namespace Pock\TestUtils;
|
||||
|
||||
/**
|
||||
* Class PockBuilderTestCase
|
||||
*
|
||||
* @category PockBuilderTestCase
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
class PockBuilderTestCase
|
||||
{
|
||||
}
|
@ -3,11 +3,11 @@
|
||||
/**
|
||||
* PHP 7.2
|
||||
*
|
||||
* @category AbstractRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
* @category PockTestCase
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
namespace Pock\TestUtils;
|
||||
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@ -16,12 +16,12 @@ use Pock\Enum\RequestScheme;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Class AbstractRequestMatcherTest
|
||||
* Class PockTestCase
|
||||
*
|
||||
* @category AbstractRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
* @category PockTestCase
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
abstract class AbstractRequestMatcherTest extends TestCase
|
||||
abstract class PockTestCase extends TestCase
|
||||
{
|
||||
protected const TEST_METHOD = RequestMethod::GET;
|
||||
protected const TEST_SCHEME = RequestScheme::HTTPS;
|
@ -34,5 +34,5 @@ EOF;
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("field")
|
||||
*/
|
||||
private $field = 'test';
|
||||
protected $field = 'test';
|
||||
}
|
||||
|
29
tests/utils/SimpleObjectJsonSerializable.php
Normal file
29
tests/utils/SimpleObjectJsonSerializable.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category SimpleObjectJsonSerializable
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
|
||||
namespace Pock\TestUtils;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* Class SimpleObjectJsonSerializable
|
||||
*
|
||||
* @category SimpleObjectJsonSerializable
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
class SimpleObjectJsonSerializable extends SimpleObject implements JsonSerializable
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return ['field' => $this->field];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user