mirror of
https://github.com/Neur0toxine/pock.git
synced 2024-12-01 01:16:07 +03:00
fixes, tests, ability to repeat mock
This commit is contained in:
parent
539f96a76d
commit
90ea990202
42
.github/workflows/code_quality.yml
vendored
Normal file
42
.github/workflows/code_quality.yml
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
name: "Code Quality"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- "**.php"
|
||||
- "phpcs.xml"
|
||||
- ".github/workflows/code_quality.yml"
|
||||
|
||||
jobs:
|
||||
phpcs:
|
||||
name: "PHP CodeSniffer"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code into the workspace
|
||||
uses: actions/checkout@v2
|
||||
- name: Run PHPCS
|
||||
uses: chekalsky/phpcs-action@v1
|
||||
phpmd:
|
||||
name: "PHP MessDetector"
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code into the workspace
|
||||
uses: actions/checkout@v2
|
||||
- name: Run PHPMD
|
||||
uses: GeneaLabs/action-reviewdog-phpmd@1.0.0
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
level: 'warning'
|
||||
reporter: github-pr-check
|
||||
standard: './phpmd.xml'
|
||||
target_directory: 'src'
|
||||
phpstan:
|
||||
name: PHPStan
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code into the workspace
|
||||
uses: actions/checkout@v2
|
||||
- name: Run PHPStan
|
||||
uses: docker://oskarstark/phpstan-ga
|
||||
with:
|
||||
args: analyse src -c phpstan.neon --memory-limit=1G --no-progress
|
38
.github/workflows/tests.yml
vendored
Normal file
38
.github/workflows/tests.yml
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
name: Tests
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
tags-ignore:
|
||||
- '*.*'
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: "PHPUnit"
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
php-version: ['7.1', '7.2', '7.3', '7.4', '8.0']
|
||||
steps:
|
||||
- name: Check out code into the workspace
|
||||
uses: actions/checkout@v2
|
||||
- name: Setup PHP ${{ matrix.php-version }}
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: ${{ matrix.php-version }}
|
||||
coverage: pcov
|
||||
- name: Composer cache
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ${{ env.HOME }}/.composer/cache
|
||||
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
|
||||
- name: Install dependencies
|
||||
run: composer install -o
|
||||
- name: Configure matchers
|
||||
uses: mheap/phpunit-matcher-action@v1
|
||||
- name: Run tests
|
||||
run: composer run-script phpunit-ci
|
||||
- name: Coverage
|
||||
run: bash <(curl -s https://codecov.io/bash)
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
composer.lock
|
||||
vendor/*
|
||||
.idea/*
|
||||
.php_cs.cache
|
||||
.phpunit.result.cache
|
||||
test-report.xml
|
||||
|
@ -14,15 +14,50 @@
|
||||
"Pock\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Pock\\Tests\\": "tests/src/",
|
||||
"Pock\\TestUtils\\": "tests/utils/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.1.0",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-message": "^1.0",
|
||||
"php-http/httplug": "^1.0 || ^2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.6",
|
||||
"phpmd/phpmd": "^2.10",
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
|
||||
"phpcompatibility/php-compatibility": "^9.3",
|
||||
"phpstan/phpstan": "^0.12.87",
|
||||
"jms/serializer": "^3.12",
|
||||
"symfony/phpunit-bridge": "^5.2",
|
||||
"symfony/var-dumper": "^5.2",
|
||||
"symfony/serializer": "^5.2",
|
||||
"nyholm/psr7": "^1.4"
|
||||
},
|
||||
"provide": {
|
||||
"psr/http-client-implementation": "1.0",
|
||||
"php-http/client-implementation": "1.0",
|
||||
"php-http/async-client-implementation": "1.0"
|
||||
},
|
||||
"scripts": {
|
||||
"phpunit": "./vendor/bin/simple-phpunit -c phpunit.xml.dist --coverage-text",
|
||||
"phpunit-ci": "@php -dpcov.enabled=1 -dpcov.directory=. -dpcov.exclude=\"~vendor~\" ./vendor/bin/simple-phpunit --teamcity -c phpunit.xml.dist",
|
||||
"phpmd": "./vendor/bin/phpmd src text ./phpmd.xml",
|
||||
"phpcs": "./vendor/bin/phpcs -p src --runtime-set testVersion 7.1-8.0 && ./vendor/bin/phpcs -p tests --runtime-set testVersion 7.1-8.0 --warning-severity=0",
|
||||
"phpstan": "./vendor/bin/phpstan analyse -c phpstan.neon src --memory-limit=-1",
|
||||
"lint:fix": "./vendor/bin/phpcbf src",
|
||||
"lint": [
|
||||
"@phpcs",
|
||||
"@phpmd",
|
||||
"@phpstan"
|
||||
],
|
||||
"verify": [
|
||||
"@lint",
|
||||
"@phpunit"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
13
phpcs.xml.dist
Normal file
13
phpcs.xml.dist
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
|
||||
<arg name="basepath" value="."/>
|
||||
<arg name="cache" value=".php_cs.cache"/>
|
||||
<arg name="colors"/>
|
||||
<arg name="extensions" value="php"/>
|
||||
|
||||
<rule ref="PSR12"/>
|
||||
|
||||
<file>src/</file>
|
||||
<file>tests/</file>
|
||||
</ruleset>
|
21
phpdoc.dist.xml
Normal file
21
phpdoc.dist.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<phpdocumentor
|
||||
configVersion="3"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="https://www.phpdoc.org"
|
||||
xsi:noNamespaceSchemaLocation="https://docs.phpdoc.org/latest/phpdoc.xsd"
|
||||
>
|
||||
<title>RetailCRM API Client</title>
|
||||
<paths>
|
||||
<output>docs/build/html</output>
|
||||
<cache>docs/build/cache</cache>
|
||||
</paths>
|
||||
<version number="latest">
|
||||
<api>
|
||||
<visibility>public</visibility>
|
||||
<source dsn=".">
|
||||
<path>src</path>
|
||||
</source>
|
||||
</api>
|
||||
</version>
|
||||
</phpdocumentor>
|
16
phpmd.xml
Normal file
16
phpmd.xml
Normal file
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ruleset name="Ruleset"
|
||||
xmlns="http://pmd.sf.net/ruleset/1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
|
||||
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
|
||||
<description>Ruleset</description>
|
||||
<rule ref="rulesets/controversial.xml" />
|
||||
<rule ref="rulesets/unusedcode.xml" />
|
||||
<rule ref="rulesets/design.xml" />
|
||||
<rule ref="rulesets/cleancode.xml" />
|
||||
<rule ref="rulesets/codesize.xml" />
|
||||
<rule ref="rulesets/naming.xml" />
|
||||
|
||||
<exclude-pattern>tests/*</exclude-pattern>
|
||||
</ruleset>
|
5
phpstan.neon
Normal file
5
phpstan.neon
Normal file
@ -0,0 +1,5 @@
|
||||
parameters:
|
||||
level: max
|
||||
paths:
|
||||
- src
|
||||
- tests
|
39
phpunit.xml.dist
Normal file
39
phpunit.xml.dist
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
colors="false"
|
||||
bootstrap="tests/bootstrap.php"
|
||||
backupStaticAttributes="false"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="false"
|
||||
convertWarningsToExceptions="false"
|
||||
processIsolation="false"
|
||||
stopOnError="false"
|
||||
stopOnFailure="false"
|
||||
stopOnIncomplete="false"
|
||||
stopOnSkipped="false"
|
||||
stopOnRisky="false"
|
||||
>
|
||||
<coverage>
|
||||
<include>
|
||||
<directory>src</directory>
|
||||
<directory>dev</directory>
|
||||
</include>
|
||||
<report>
|
||||
<clover outputFile="coverage.xml"/>
|
||||
</report>
|
||||
</coverage>
|
||||
<testsuites>
|
||||
<testsuite name="Project Test Suite">
|
||||
<directory>tests/src</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<logging>
|
||||
<junit outputFile="test-report.xml"/>
|
||||
</logging>
|
||||
<php>
|
||||
<ini name="memory_limit" value="1G" />
|
||||
</php>
|
||||
</phpunit>
|
@ -70,19 +70,19 @@ class Client implements ClientInterface, HttpClient, HttpAsyncClient
|
||||
public function sendAsyncRequest(RequestInterface $request): Promise
|
||||
{
|
||||
foreach ($this->mocks as $mock) {
|
||||
if ($mock->isFired()) {
|
||||
if (!$mock->available()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($mock->getMatcher()->matches($request)) {
|
||||
if (null !== $mock->getResponse()) {
|
||||
$mock->markAsFired();
|
||||
$mock->registerHit();
|
||||
|
||||
return new HttpFulfilledPromise($mock->getResponse());
|
||||
}
|
||||
|
||||
if (null !== $mock->getThrowable()) {
|
||||
$mock->markAsFired();
|
||||
$mock->registerHit();
|
||||
|
||||
return new HttpRejectedPromise($mock->getThrowable());
|
||||
}
|
||||
|
55
src/Creator/AbstractJmsSerializerCreator.php
Normal file
55
src/Creator/AbstractJmsSerializerCreator.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category AbstractJmsSerializerCreator
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
|
||||
namespace Pock\Creator;
|
||||
|
||||
use Throwable;
|
||||
use Pock\Serializer\JmsSerializerDecorator;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Class AbstractJmsSerializerCreator
|
||||
*
|
||||
* @category AbstractJmsSerializerCreator
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
abstract class AbstractJmsSerializerCreator implements SerializerCreatorInterface
|
||||
{
|
||||
private const BUILDER_CLASS = '\JMS\Serializer\SerializerBuilder';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function create(): ?SerializerInterface
|
||||
{
|
||||
if (
|
||||
class_exists(self::BUILDER_CLASS) &&
|
||||
method_exists(self::BUILDER_CLASS, 'create')
|
||||
) {
|
||||
try {
|
||||
$builder = call_user_func([self::BUILDER_CLASS, 'create']);
|
||||
|
||||
if (null !== $builder && method_exists($builder, 'build')) {
|
||||
return new JmsSerializerDecorator($builder->build(), static::getFormat()); // @phpstan-ignore-line
|
||||
}
|
||||
} catch (Throwable $throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns format for the serializer;
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected static function getFormat(): string;
|
||||
}
|
27
src/Creator/JmsJsonSerializerCreator.php
Normal file
27
src/Creator/JmsJsonSerializerCreator.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JmsJsonSerializerCreator
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
|
||||
namespace Pock\Creator;
|
||||
|
||||
/**
|
||||
* Class JmsJsonSerializerCreator
|
||||
*
|
||||
* @category JmsJsonSerializerCreator
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
class JmsJsonSerializerCreator extends AbstractJmsSerializerCreator
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static function getFormat(): string
|
||||
{
|
||||
return 'json';
|
||||
}
|
||||
}
|
27
src/Creator/JmsXmlSerializerCreator.php
Normal file
27
src/Creator/JmsXmlSerializerCreator.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JmsXmlSerializerCreator
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
|
||||
namespace Pock\Creator;
|
||||
|
||||
/**
|
||||
* Class JmsXmlSerializerCreator
|
||||
*
|
||||
* @category JmsXmlSerializerCreator
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
class JmsXmlSerializerCreator extends AbstractJmsSerializerCreator
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static function getFormat(): string
|
||||
{
|
||||
return 'xml';
|
||||
}
|
||||
}
|
28
src/Creator/SerializerCreatorInterface.php
Normal file
28
src/Creator/SerializerCreatorInterface.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category SerializerCreatorInterface
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
|
||||
namespace Pock\Creator;
|
||||
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Interface SerializerCreatorInterface
|
||||
*
|
||||
* @category SerializerCreatorInterface
|
||||
* @package Pock\Creator
|
||||
*/
|
||||
interface SerializerCreatorInterface
|
||||
{
|
||||
/**
|
||||
* Instantiates serializer and returns it. Returns null if serializer cannot be located.
|
||||
*
|
||||
* @return \Pock\Serializer\SerializerInterface|null
|
||||
*/
|
||||
public static function create(): ?SerializerInterface;
|
||||
}
|
51
src/Factory/AbstractSerializerFactory.php
Normal file
51
src/Factory/AbstractSerializerFactory.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category AbstractSerializerFactory
|
||||
* @package Pock\Factory
|
||||
*/
|
||||
|
||||
namespace Pock\Factory;
|
||||
|
||||
use Pock\Creator\SerializerCreatorInterface;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Class AbstractSerializerFactory
|
||||
*
|
||||
* @category AbstractSerializerFactory
|
||||
* @package Pock\Factory
|
||||
*/
|
||||
abstract class AbstractSerializerFactory implements SerializerCreatorInterface
|
||||
{
|
||||
/**
|
||||
* Instantiate first available serializer.
|
||||
*
|
||||
* @return \Pock\Serializer\SerializerInterface|null
|
||||
*/
|
||||
public static function create(): ?SerializerInterface
|
||||
{
|
||||
foreach (static::getCreators() as $creator) {
|
||||
if (!method_exists($creator, 'create')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$serializer = call_user_func([$creator, 'create']); // @phpstan-ignore-line
|
||||
|
||||
if ($serializer instanceof SerializerInterface) {
|
||||
return $serializer;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns list of available creators.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
abstract protected static function getCreators(): array;
|
||||
}
|
31
src/Factory/JsonSerializerFactory.php
Normal file
31
src/Factory/JsonSerializerFactory.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JsonSerializerFactory
|
||||
* @package Pock\Factory
|
||||
*/
|
||||
|
||||
namespace Pock\Factory;
|
||||
|
||||
use Pock\Creator\JmsJsonSerializerCreator;
|
||||
|
||||
/**
|
||||
* Class JsonSerializerFactory
|
||||
*
|
||||
* @category JsonSerializerFactory
|
||||
* @package Pock\Factory
|
||||
*/
|
||||
class JsonSerializerFactory extends AbstractSerializerFactory
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static function getCreators(): array
|
||||
{
|
||||
return [
|
||||
JmsJsonSerializerCreator::class,
|
||||
];
|
||||
}
|
||||
}
|
31
src/Factory/XmlSerializerFactory.php
Normal file
31
src/Factory/XmlSerializerFactory.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category XmlSerializerFactory
|
||||
* @package Pock\Factory
|
||||
*/
|
||||
|
||||
namespace Pock\Factory;
|
||||
|
||||
use Pock\Creator\JmsXmlSerializerCreator;
|
||||
|
||||
/**
|
||||
* Class XmlSerializerFactory
|
||||
*
|
||||
* @category XmlSerializerFactory
|
||||
* @package Pock\Factory
|
||||
*/
|
||||
class XmlSerializerFactory extends AbstractSerializerFactory
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected static function getCreators(): array
|
||||
{
|
||||
return [
|
||||
JmsXmlSerializerCreator::class
|
||||
];
|
||||
}
|
||||
}
|
@ -37,6 +37,6 @@ class UriMatcher implements RequestMatcherInterface
|
||||
*/
|
||||
public function matches(RequestInterface $request): bool
|
||||
{
|
||||
return strtolower((string) $request->getUri()) === strtolower((string) $this->uri);
|
||||
return ((string) $request->getUri()) === ((string) $this->uri);
|
||||
}
|
||||
}
|
||||
|
37
src/Mock.php
37
src/Mock.php
@ -30,8 +30,11 @@ class Mock implements MockInterface
|
||||
/** @var \Throwable|null */
|
||||
private $throwable;
|
||||
|
||||
/** @var bool */
|
||||
private $fired = false;
|
||||
/** @var int */
|
||||
private $hits;
|
||||
|
||||
/** @var int */
|
||||
private $maxHits;
|
||||
|
||||
/**
|
||||
* Mock constructor.
|
||||
@ -39,30 +42,40 @@ class Mock implements MockInterface
|
||||
* @param \Pock\Matchers\RequestMatcherInterface $matcher
|
||||
* @param \Psr\Http\Message\ResponseInterface|null $response
|
||||
* @param \Throwable|null $throwable
|
||||
* @param int $maxHits
|
||||
*/
|
||||
public function __construct(RequestMatcherInterface $matcher, ?ResponseInterface $response, ?Throwable $throwable)
|
||||
{
|
||||
public function __construct(
|
||||
RequestMatcherInterface $matcher,
|
||||
?ResponseInterface $response,
|
||||
?Throwable $throwable,
|
||||
int $maxHits
|
||||
) {
|
||||
$this->matcher = $matcher;
|
||||
$this->response = $response;
|
||||
$this->throwable = $throwable;
|
||||
$this->maxHits = $maxHits;
|
||||
$this->hits = 0;
|
||||
}
|
||||
|
||||
public function markAsFired(): MockInterface
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function registerHit(): MockInterface
|
||||
{
|
||||
$this->fired = true;
|
||||
++$this->hits;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isFired(): bool
|
||||
public function available(): bool
|
||||
{
|
||||
return $this->fired;
|
||||
return $this->hits < $this->maxHits;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pock\Matchers\RequestMatcherInterface
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getMatcher(): RequestMatcherInterface
|
||||
{
|
||||
@ -70,7 +83,7 @@ class Mock implements MockInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Psr\Http\Message\ResponseInterface|null
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getResponse(): ?ResponseInterface
|
||||
{
|
||||
@ -78,7 +91,7 @@ class Mock implements MockInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Throwable|null
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getThrowable(): ?Throwable
|
||||
{
|
||||
|
@ -22,18 +22,18 @@ use Throwable;
|
||||
interface MockInterface
|
||||
{
|
||||
/**
|
||||
* Marks mock as already used.
|
||||
* Registers a hit to the mock.
|
||||
*
|
||||
* @return \Pock\MockInterface
|
||||
*/
|
||||
public function markAsFired(): MockInterface;
|
||||
public function registerHit(): MockInterface;
|
||||
|
||||
/**
|
||||
* Returns true if mock was not used yet.
|
||||
* Returns true if mock is still can be used.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isFired(): bool;
|
||||
public function available(): bool;
|
||||
|
||||
/**
|
||||
* Returns matcher for the request.
|
||||
|
@ -35,6 +35,9 @@ class PockBuilder
|
||||
/** @var \Throwable|null */
|
||||
private $throwable;
|
||||
|
||||
/** @var int */
|
||||
private $maxHits;
|
||||
|
||||
/** @var \Pock\MockInterface[] */
|
||||
private $mocks;
|
||||
|
||||
@ -54,7 +57,7 @@ class PockBuilder
|
||||
*
|
||||
* @param string $scheme
|
||||
*
|
||||
* @return $this
|
||||
* @return self
|
||||
*/
|
||||
public function matchScheme(string $scheme = RequestScheme::HTTP): PockBuilder
|
||||
{
|
||||
@ -66,7 +69,7 @@ class PockBuilder
|
||||
*
|
||||
* @param string $host
|
||||
*
|
||||
* @return $this
|
||||
* @return self
|
||||
*/
|
||||
public function matchHost(string $host): PockBuilder
|
||||
{
|
||||
@ -100,6 +103,23 @@ class PockBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeat this mock provided amount of times.
|
||||
* For example, if you pass 2 as an argument mock will be able to handle two identical requests.
|
||||
*
|
||||
* @param int $hits
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function repeat(int $hits): PockBuilder
|
||||
{
|
||||
if ($hits > 0) {
|
||||
$this->maxHits = $hits;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the builder.
|
||||
*
|
||||
@ -110,6 +130,7 @@ class PockBuilder
|
||||
$this->matcher = new MultipleMatcher();
|
||||
$this->response = null;
|
||||
$this->throwable = null;
|
||||
$this->maxHits = 1;
|
||||
$this->mocks = [];
|
||||
|
||||
return $this;
|
||||
@ -143,9 +164,11 @@ class PockBuilder
|
||||
$this->matcher->addMatcher(new AnyRequestMatcher());
|
||||
}
|
||||
|
||||
$this->mocks[] = new Mock($this->matcher, $this->response, $this->throwable);
|
||||
$this->mocks[] = new Mock($this->matcher, $this->response, $this->throwable, $this->maxHits);
|
||||
$this->matcher = new MultipleMatcher();
|
||||
$this->response = null;
|
||||
$this->throwable = null;
|
||||
$this->maxHits = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,6 +62,8 @@ class HttpRejectedPromise implements Promise
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws \Throwable
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
*/
|
||||
public function wait($unwrap = true): void
|
||||
{
|
||||
|
48
src/Serializer/JmsSerializerDecorator.php
Normal file
48
src/Serializer/JmsSerializerDecorator.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JmsSerializerDecorator
|
||||
* @package Pock\Serializer
|
||||
*/
|
||||
|
||||
namespace Pock\Serializer;
|
||||
|
||||
/**
|
||||
* Class JmsSerializerDecorator
|
||||
*
|
||||
* @category JmsSerializerDecorator
|
||||
* @package Pock\Serializer
|
||||
*/
|
||||
class JmsSerializerDecorator implements SerializerInterface
|
||||
{
|
||||
/** @var object */
|
||||
private $serializer;
|
||||
|
||||
/** @var string */
|
||||
private $format;
|
||||
|
||||
/**
|
||||
* JmsSerializerDecorator constructor.
|
||||
*
|
||||
* @param object $serializer
|
||||
*/
|
||||
public function __construct($serializer, string $format = 'json')
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
$this->format = $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function serialize($data): string
|
||||
{
|
||||
if (method_exists($this->serializer, 'serialize')) {
|
||||
return $this->serializer->serialize($data, $this->format);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
28
src/Serializer/SerializerInterface.php
Normal file
28
src/Serializer/SerializerInterface.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category SerializerInterface
|
||||
* @package Pock\Serializer
|
||||
*/
|
||||
|
||||
namespace Pock\Serializer;
|
||||
|
||||
/**
|
||||
* Interface SerializerInterface
|
||||
*
|
||||
* @category SerializerInterface
|
||||
* @package Pock\Serializer
|
||||
*/
|
||||
interface SerializerInterface
|
||||
{
|
||||
/**
|
||||
* Serialize item
|
||||
*
|
||||
* @param mixed $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function serialize($data): string;
|
||||
}
|
18
tests/bootstrap.php
Normal file
18
tests/bootstrap.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if (
|
||||
function_exists('date_default_timezone_set')
|
||||
&& function_exists('date_default_timezone_get')
|
||||
) {
|
||||
date_default_timezone_set(date_default_timezone_get());
|
||||
}
|
||||
|
||||
if (!is_file($autoloadFile = __DIR__ . '/../vendor/autoload.php')) {
|
||||
throw new RuntimeException('Did not find vendor/autoload.php. Did you run "composer install --dev"?');
|
||||
}
|
||||
|
||||
$loader = require $autoloadFile;
|
||||
$loader->add('Pock\\Tests', __DIR__ . '/src');
|
||||
$loader->add('Pock\\TestUtils', __DIR__ . '/utils');
|
32
tests/src/Creator/JmsJsonSerializerCreatorTest.php
Normal file
32
tests/src/Creator/JmsJsonSerializerCreatorTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JmsJsonSerializerCreatorTest
|
||||
* @package src\Component\Creator
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Component\Creator;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Pock\Creator\JmsJsonSerializerCreator;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
use Pock\TestUtils\SimpleObject;
|
||||
|
||||
/**
|
||||
* Class JmsJsonSerializerCreatorTest
|
||||
*
|
||||
* @category JmsJsonSerializerCreatorTest
|
||||
* @package src\Component\Creator
|
||||
*/
|
||||
class JmsJsonSerializerCreatorTest extends TestCase
|
||||
{
|
||||
public function testCreate(): void
|
||||
{
|
||||
$serializer = JmsJsonSerializerCreator::create();
|
||||
|
||||
self::assertInstanceOf(SerializerInterface::class, $serializer);
|
||||
self::assertEquals(SimpleObject::JSON, $serializer->serialize(new SimpleObject()));
|
||||
}
|
||||
}
|
32
tests/src/Creator/JmsXmlSerializerCreatorTest.php
Normal file
32
tests/src/Creator/JmsXmlSerializerCreatorTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JmsXmlSerializerCreator
|
||||
* @package src\Component\Creator
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Component\Creator;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Pock\Creator\JmsXmlSerializerCreator;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
use Pock\TestUtils\SimpleObject;
|
||||
|
||||
/**
|
||||
* Class JmsXmlSerializerCreator
|
||||
*
|
||||
* @category JmsXmlSerializerCreator
|
||||
* @package src\Component\Creator
|
||||
*/
|
||||
class JmsXmlSerializerCreatorTest extends TestCase
|
||||
{
|
||||
public function testCreate(): void
|
||||
{
|
||||
$serializer = JmsXmlSerializerCreator::create();
|
||||
|
||||
self::assertInstanceOf(SerializerInterface::class, $serializer);
|
||||
self::assertEquals(SimpleObject::XML, $serializer->serialize(new SimpleObject()));
|
||||
}
|
||||
}
|
32
tests/src/Factory/JsonSerializerFactoryTest.php
Normal file
32
tests/src/Factory/JsonSerializerFactoryTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category JsonSerializerFactoryTest
|
||||
* @package Pock\Tests\Factory
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Factory;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Pock\Factory\JsonSerializerFactory;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
use Pock\TestUtils\SimpleObject;
|
||||
|
||||
/**
|
||||
* Class JsonSerializerFactoryTest
|
||||
*
|
||||
* @category JsonSerializerFactoryTest
|
||||
* @package Pock\Tests\Factory
|
||||
*/
|
||||
class JsonSerializerFactoryTest extends TestCase
|
||||
{
|
||||
public function testCreate(): void
|
||||
{
|
||||
$serializer = JsonSerializerFactory::create();
|
||||
|
||||
self::assertInstanceOf(SerializerInterface::class, $serializer);
|
||||
self::assertEquals(SimpleObject::JSON, $serializer->serialize(new SimpleObject()));
|
||||
}
|
||||
}
|
32
tests/src/Factory/XmlSerializerFactoryTest.php
Normal file
32
tests/src/Factory/XmlSerializerFactoryTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category XmlSerializerFactoryTest
|
||||
* @package Pock\Tests\Factory
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Factory;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Pock\Factory\XmlSerializerFactory;
|
||||
use Pock\Serializer\SerializerInterface;
|
||||
use Pock\TestUtils\SimpleObject;
|
||||
|
||||
/**
|
||||
* Class XmlSerializerFactoryTest
|
||||
*
|
||||
* @category XmlSerializerFactoryTest
|
||||
* @package Pock\Tests\Factory
|
||||
*/
|
||||
class XmlSerializerFactoryTest extends TestCase
|
||||
{
|
||||
public function testCreate(): void
|
||||
{
|
||||
$serializer = XmlSerializerFactory::create();
|
||||
|
||||
self::assertInstanceOf(SerializerInterface::class, $serializer);
|
||||
self::assertEquals(SimpleObject::XML, $serializer->serialize(new SimpleObject()));
|
||||
}
|
||||
}
|
53
tests/src/Matchers/AbstractRequestMatcherTest.php
Normal file
53
tests/src/Matchers/AbstractRequestMatcherTest.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category AbstractRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Pock\Enum\RequestMethod;
|
||||
use Pock\Enum\RequestScheme;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
||||
/**
|
||||
* Class AbstractRequestMatcherTest
|
||||
*
|
||||
* @category AbstractRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
abstract class AbstractRequestMatcherTest extends TestCase
|
||||
{
|
||||
protected const TEST_METHOD = RequestMethod::GET;
|
||||
protected const TEST_SCHEME = RequestScheme::HTTPS;
|
||||
protected const TEST_HOST = 'example.com';
|
||||
protected const TEST_URI = self::TEST_SCHEME . '://' . self::TEST_HOST . '/';
|
||||
|
||||
/** @var \Nyholm\Psr7\Factory\Psr17Factory */
|
||||
private static $psr17Factory;
|
||||
|
||||
/**
|
||||
* @return \Psr\Http\Message\RequestInterface
|
||||
*/
|
||||
protected static function getTestRequest(): RequestInterface
|
||||
{
|
||||
return static::getPsr17Factory()->createRequest(static::TEST_METHOD, static::TEST_URI);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Nyholm\Psr7\Factory\Psr17Factory
|
||||
*/
|
||||
protected static function getPsr17Factory(): Psr17Factory
|
||||
{
|
||||
if (null === static::$psr17Factory) {
|
||||
static::$psr17Factory = new Psr17Factory();
|
||||
}
|
||||
|
||||
return static::$psr17Factory;
|
||||
}
|
||||
}
|
26
tests/src/Matchers/AnyRequestMatcherTest.php
Normal file
26
tests/src/Matchers/AnyRequestMatcherTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category AnyRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Matchers\AnyRequestMatcher;
|
||||
|
||||
/**
|
||||
* Class AnyRequestMatcherTest
|
||||
*
|
||||
* @category AnyRequestMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class AnyRequestMatcherTest extends AbstractRequestMatcherTest
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
self::assertTrue((new AnyRequestMatcher())->matches(static::getTestRequest()));
|
||||
}
|
||||
}
|
26
tests/src/Matchers/HostMatcherTest.php
Normal file
26
tests/src/Matchers/HostMatcherTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category HostMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Matchers\HostMatcher;
|
||||
|
||||
/**
|
||||
* Class HostMatcherTest
|
||||
*
|
||||
* @category HostMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class HostMatcherTest extends AbstractRequestMatcherTest
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
self::assertTrue((new HostMatcher(self::TEST_HOST))->matches(static::getTestRequest()));
|
||||
}
|
||||
}
|
34
tests/src/Matchers/MultipleMatcherTest.php
Normal file
34
tests/src/Matchers/MultipleMatcherTest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category MultipleMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Enum\RequestMethod;
|
||||
use Pock\Matchers\AnyRequestMatcher;
|
||||
use Pock\Matchers\HostMatcher;
|
||||
use Pock\Matchers\MultipleMatcher;
|
||||
|
||||
/**
|
||||
* Class MultipleMatcherTest
|
||||
*
|
||||
* @category MultipleMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class MultipleMatcherTest extends AbstractRequestMatcherTest
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
$matcher = new MultipleMatcher([new HostMatcher(self::TEST_HOST)]);
|
||||
$matcher->addMatcher(new AnyRequestMatcher());
|
||||
|
||||
self::assertTrue($matcher->matches(static::getTestRequest()));
|
||||
self::assertFalse($matcher->matches(static::getPsr17Factory()
|
||||
->createRequest(RequestMethod::GET, 'https://test.com')));
|
||||
}
|
||||
}
|
27
tests/src/Matchers/SchemeMatcherTest.php
Normal file
27
tests/src/Matchers/SchemeMatcherTest.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category SchemeMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Enum\RequestScheme;
|
||||
use Pock\Matchers\SchemeMatcher;
|
||||
|
||||
/**
|
||||
* Class SchemeMatcherTest
|
||||
*
|
||||
* @category SchemeMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class SchemeMatcherTest extends AbstractRequestMatcherTest
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
self::assertTrue((new SchemeMatcher(RequestScheme::HTTPS))->matches(static::getTestRequest()));
|
||||
}
|
||||
}
|
28
tests/src/Matchers/UriMatcherTest.php
Normal file
28
tests/src/Matchers/UriMatcherTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category UriMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
|
||||
namespace Pock\Tests\Matchers;
|
||||
|
||||
use Pock\Matchers\UriMatcher;
|
||||
|
||||
/**
|
||||
* Class UriMatcherTest
|
||||
*
|
||||
* @category UriMatcherTest
|
||||
* @package Pock\Tests\Matchers
|
||||
*/
|
||||
class UriMatcherTest extends AbstractRequestMatcherTest
|
||||
{
|
||||
public function testMatches(): void
|
||||
{
|
||||
self::assertTrue((new UriMatcher(self::TEST_URI))->matches(static::getTestRequest()));
|
||||
self::assertTrue((new UriMatcher(static::getPsr17Factory()->createUri(self::TEST_URI)))
|
||||
->matches(static::getTestRequest()));
|
||||
}
|
||||
}
|
38
tests/utils/SimpleObject.php
Normal file
38
tests/utils/SimpleObject.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP 7.3
|
||||
*
|
||||
* @category SimpleObject
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
|
||||
namespace Pock\TestUtils;
|
||||
|
||||
use JMS\Serializer\Annotation as JMS;
|
||||
|
||||
/**
|
||||
* Class SimpleObject
|
||||
*
|
||||
* @category SimpleObject
|
||||
* @package Pock\TestUtils
|
||||
*/
|
||||
class SimpleObject
|
||||
{
|
||||
public const JSON = '{"field":"test"}';
|
||||
public const XML = <<<'EOF'
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<result>
|
||||
<field><![CDATA[test]]></field>
|
||||
</result>
|
||||
|
||||
EOF;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("field")
|
||||
*/
|
||||
private $field = 'test';
|
||||
}
|
Loading…
Reference in New Issue
Block a user