Add tests
This commit is contained in:
parent
9c300796b0
commit
78d7b5ad70
13 changed files with 947 additions and 61 deletions
|
@ -1,24 +1,27 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
|
||||
<phpunit
|
||||
backupGlobals = "false"
|
||||
backupStaticAttributes = "false"
|
||||
colors = "true"
|
||||
convertErrorsToExceptions = "true"
|
||||
convertNoticesToExceptions = "true"
|
||||
convertWarningsToExceptions = "true"
|
||||
processIsolation = "true"
|
||||
stopOnFailure = "false"
|
||||
syntaxCheck = "false"
|
||||
bootstrap = "bootstrap.php" >
|
||||
<phpunit bootstrap="tests/bootstrap.php"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false">
|
||||
|
||||
<testsuites>
|
||||
|
||||
<testsuite name="php">
|
||||
<directory>./tests/*/Tests</directory>
|
||||
<directory>./tests/AtolOnlineClient</directory>
|
||||
</testsuite>
|
||||
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
|
|
420
tests/AtolOnlineClient/AtolOnlineApiTest.php
Normal file
420
tests/AtolOnlineClient/AtolOnlineApiTest.php
Normal file
|
@ -0,0 +1,420 @@
|
|||
<?php
|
||||
|
||||
namespace AtolOnlineClient\AtolOnlineClient;
|
||||
|
||||
use AtolOnlineClient\AtolOnline;
|
||||
use AtolOnlineClient\AtolOnlineApi;
|
||||
use AtolOnlineClient\Configuration\Connection;
|
||||
use AtolOnlineClient\Request\V4\PaymentReceiptRequest;
|
||||
use AtolOnlineClient\Request\V4\ReceiptRequest;
|
||||
use AtolOnlineClient\Request\V4\ServiceRequest;
|
||||
use Doctrine\Common\Cache\ArrayCache;
|
||||
use Guzzle\Http\Client;
|
||||
use Guzzle\Http\Message\Response;
|
||||
use Guzzle\Plugin\Mock\MockPlugin;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
use Psr\Log\Test\TestLogger;
|
||||
|
||||
/**
|
||||
* Class AtolOnlineApiTest
|
||||
*
|
||||
* @package AtolOnlineClient\AtolOnlineClient
|
||||
*/
|
||||
class AtolOnlineApiTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::__construct
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::setLogger
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::setCache
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::setVersion
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getVersion
|
||||
*/
|
||||
public function testCreateApi(): void
|
||||
{
|
||||
$api = new AtolOnlineApi(new Client(), new Connection());
|
||||
|
||||
$this->assertInstanceOf(AtolOnlineApi::class, $api);
|
||||
|
||||
$this->assertSame(AtolOnlineApi::API_VERSION_V3, $api->getVersion());
|
||||
|
||||
$api->setVersion(AtolOnlineApi::API_VERSION_V4);
|
||||
$this->assertSame(AtolOnlineApi::API_VERSION_V4, $api->getVersion());
|
||||
|
||||
$api->setLogger(new NullLogger());
|
||||
$this->assertInstanceOf(NullLogger::class, $this->getProperty($api, 'logger'));
|
||||
|
||||
$api->setCache(new ArrayCache());
|
||||
$this->assertInstanceOf(ArrayCache::class, $this->getProperty($api, 'cache'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::sell
|
||||
*/
|
||||
public function testSell(): void
|
||||
{
|
||||
$responses = [
|
||||
new Response(200, [], $this->getTokenSuccessResponseV4()),
|
||||
new Response(200, [], $this->getReportSuccessReportV4()),
|
||||
];
|
||||
|
||||
$request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest());
|
||||
|
||||
$api = $this->getApi($responses);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$response = $api->sell($request);
|
||||
|
||||
$this->assertSame('2ea26f17–0884–4f08–b120–306fc096a58f', json_decode($response, true)['uuid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::sellRefund
|
||||
*/
|
||||
public function testSellRefund(): void
|
||||
{
|
||||
$responses = [
|
||||
new Response(200, [], $this->getTokenSuccessResponseV4()),
|
||||
new Response(200, [], $this->getReportSuccessReportV4()),
|
||||
];
|
||||
|
||||
$request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest());
|
||||
|
||||
$api = $this->getApi($responses);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$response = $api->sellRefund($request);
|
||||
|
||||
$this->assertSame('2ea26f17–0884–4f08–b120–306fc096a58f', json_decode($response, true)['uuid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::checkStatus
|
||||
*/
|
||||
public function testCheckStatusSuccessResponse(): void
|
||||
{
|
||||
$responses = [
|
||||
new Response(200, [], $this->getTokenSuccessResponseV4()),
|
||||
new Response(200, [], $this->getReportSuccessReportV4()),
|
||||
];
|
||||
|
||||
$api = $this->getApi($responses);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$response = $this->callMethod($api, 'checkStatus', [
|
||||
'uuid' => '2ea26f17–0884–4f08–b120–306fc096a58f',
|
||||
]);
|
||||
|
||||
$this->assertSame('2ea26f17–0884–4f08–b120–306fc096a58f', json_decode($response, true)['uuid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getToken
|
||||
*/
|
||||
public function testGetTokenSuccessResponse(): void
|
||||
{
|
||||
$api = $this->getApi([new Response(200, [], $this->getTokenSuccessResponseV4())]);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getToken
|
||||
*/
|
||||
public function testGetTokenErrorResponseWithLogger(): void
|
||||
{
|
||||
$logger = new TestLogger();
|
||||
|
||||
$api = $this->getApi([new Response(200, [], $this->getErrorResponseV4())]);
|
||||
$api->setLogger($logger);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$this->assertFalse($this->callMethod($api, 'getToken'));
|
||||
$this->assertTrue($logger->hasError('12 Неверный логин или пароль'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getToken
|
||||
*/
|
||||
public function testGetTokenSuccessResponseWithCache(): void
|
||||
{
|
||||
$cache = new ArrayCache();
|
||||
|
||||
$api = $this->getApi([new Response(200, [], $this->getTokenSuccessResponseV4())]);
|
||||
$api->setCache($cache);
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken'));
|
||||
$this->assertTrue($cache->contains($this->callMethod($api, 'getTokenCacheKey')));
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getToken
|
||||
*/
|
||||
public function testGetTokenSuccessResponseV3(): void
|
||||
{
|
||||
$api = $this->getApi([new Response(200, [], $this->getTokenSuccessResponseV3())]);
|
||||
$api->setVersion(AtolOnlineApi::API_VERSION_V3);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getToken
|
||||
*/
|
||||
public function testGetTokenSuccessResponseV3WithCache(): void
|
||||
{
|
||||
$cache = new ArrayCache();
|
||||
|
||||
$api = $this->getApi([new Response(200, [], $this->getTokenSuccessResponseV3())]);
|
||||
$api->setCache($cache);
|
||||
$api->setVersion(AtolOnlineApi::API_VERSION_V3);
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken'));
|
||||
$this->assertTrue($cache->contains($this->callMethod($api, 'getTokenCacheKey')));
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', $this->callMethod($api, 'getToken'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::buildUrl
|
||||
*/
|
||||
public function testBuildUrlWithoutToken(): void
|
||||
{
|
||||
$args = ['operation' => 'test'];
|
||||
|
||||
$this->assertSame('https://online.atol.ru/possystem/v4/group/test', $this->callMethod($this->getApi(), 'buildUrl', $args));
|
||||
|
||||
$api = $this->getApi();
|
||||
$api->setVersion(AtolOnlineApi::API_VERSION_V3);
|
||||
$this->assertSame('https://online.atol.ru/possystem/v3/group/test', $this->callMethod($api, 'buildUrl', $args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::buildUrl
|
||||
*/
|
||||
public function testBuildUrlWithToken(): void
|
||||
{
|
||||
$args = ['operation' => 'test', 'token' => 'test'];
|
||||
|
||||
$this->assertSame('https://online.atol.ru/possystem/v4/group/test?token=test', $this->callMethod($this->getApi(), 'buildUrl', $args));
|
||||
|
||||
$api = $this->getApi();
|
||||
$api->setVersion(AtolOnlineApi::API_VERSION_V3);
|
||||
$this->assertSame('https://online.atol.ru/possystem/v3/group/test?tokenid=test', $this->callMethod($api, 'buildUrl', $args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::sendOperationRequest
|
||||
*/
|
||||
public function testSendOperationRequestSuccessResponse(): void
|
||||
{
|
||||
$responses = [
|
||||
new Response(200, [], $this->getTokenSuccessResponseV4()),
|
||||
new Response(200, [], $this->getTokenSuccessResponseV4())
|
||||
];
|
||||
|
||||
$request = (new AtolOnline())->serializeOperationRequest($this->getPaymentRecepientRequest());
|
||||
|
||||
$api = $this->getApi($responses);
|
||||
$api->setCache(new ArrayCache());
|
||||
|
||||
$response = $this->callMethod($api, 'sendOperationRequest', [
|
||||
'operation' => 'sell',
|
||||
'data' => $request,
|
||||
]);
|
||||
|
||||
$this->assertSame('fj45u923j59ju42395iu9423i59243u0', json_decode($response->getBody(), true)['token']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::logDebug
|
||||
*/
|
||||
public function testLogDebug(): void
|
||||
{
|
||||
$logger = new TestLogger();
|
||||
|
||||
$api = $this->getApi();
|
||||
$api->setLogger($logger);
|
||||
|
||||
$this->callMethod($api, 'logDebug', [
|
||||
'url' => '/test',
|
||||
'data' => 'test',
|
||||
'response' => new Response(200, ['X-Foo' => 'Bar', 'X-Foo2' => 'Bar2'], 'test'),
|
||||
]);
|
||||
|
||||
$this->assertTrue($logger->hasDebug("* URL: /test\n * POSTFIELDS: test\n * RESPONSE HEADERS: HTTP/1.1 200 OK\r\nX-Foo: Bar\r\nX-Foo2: Bar2\r\n\r\n\n * RESPONSE BODY: test\n * ATTEMPTS: 0"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::getTokenCacheKey
|
||||
*/
|
||||
public function testGetTokenCacheKey(): void
|
||||
{
|
||||
$this->assertSame('crm_fiscal_atol_online_token_68526766e6751745b52ae70b7bd3c6fe_v4', $this->callMethod($this->getApi(), 'getTokenCacheKey'));
|
||||
|
||||
|
||||
$api = $this->getApi();
|
||||
$api->setVersion(AtolOnlineApi::API_VERSION_V3);
|
||||
|
||||
$this->assertSame('crm_fiscal_atol_online_token_68526766e6751745b52ae70b7bd3c6fe_v3', $this->callMethod($api, 'getTokenCacheKey'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnlineApi::isTokenExpired
|
||||
*/
|
||||
public function testIsTokenExpired(): void
|
||||
{
|
||||
$body = new \stdClass();
|
||||
$body->error = new \stdClass();
|
||||
$body->error->code = 4;
|
||||
|
||||
$this->assertTrue($this->callMethod($this->getApi(), 'isTokenExpired', ['body' => $body]));
|
||||
|
||||
$body = new \stdClass();
|
||||
$this->assertFalse($this->callMethod($this->getApi(), 'isTokenExpired', ['body' => $body]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $responses
|
||||
* @param bool $debug
|
||||
* @param bool $test
|
||||
* @return AtolOnlineApi
|
||||
*/
|
||||
private function getApi(array $responses = [], bool $debug = true, bool $test = false): AtolOnlineApi
|
||||
{
|
||||
$connection = new Connection();
|
||||
$connection->login = 'login';
|
||||
$connection->pass = 'pass';
|
||||
$connection->group = 'group';
|
||||
$connection->sno = Connection::SNO_GENERAL;
|
||||
$connection->version = AtolOnlineApi::API_VERSION_V4;
|
||||
$connection->setDebug($debug);
|
||||
$connection->setTestMode($test);
|
||||
|
||||
$plugin = new MockPlugin();
|
||||
|
||||
foreach ($responses as $response) {
|
||||
$plugin->addResponse($response);
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
$client->addSubscriber($plugin);
|
||||
|
||||
return new AtolOnlineApi($client, $connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $object
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*/
|
||||
private function callMethod($object, string $name, array $args = [])
|
||||
{
|
||||
$reflection = new \ReflectionClass($object);
|
||||
$method = $reflection->getMethod($name);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invokeArgs($object, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $object
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
private function getProperty($object, string $name)
|
||||
{
|
||||
$reflection = new \ReflectionClass($object);
|
||||
$property = $reflection->getProperty($name);
|
||||
$property->setAccessible(true);
|
||||
|
||||
return $property->getValue($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
private function getTokenSuccessResponseV3()
|
||||
{
|
||||
return json_encode([
|
||||
'token' => 'fj45u923j59ju42395iu9423i59243u0',
|
||||
'code' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
private function getTokenSuccessResponseV4()
|
||||
{
|
||||
return json_encode([
|
||||
'error' => null,
|
||||
'token' => 'fj45u923j59ju42395iu9423i59243u0',
|
||||
'timestamp' => '30.11.2017 17:58:53',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
private function getErrorResponseV4()
|
||||
{
|
||||
return json_encode([
|
||||
'error' => [
|
||||
'error_id' => '4475d6d8d-844d-4d05-aa8b-e3dbdf3defd5',
|
||||
'code' => 12,
|
||||
'text' => 'Неверный логин или пароль',
|
||||
'type' => 'system',
|
||||
],
|
||||
'timestamp' => '30.11.2017 17:58:53',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getReportSuccessReportV4(): string
|
||||
{
|
||||
return file_get_contents(__DIR__.'/../fixtures/success_response_v4_3.json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PaymentReceiptRequest
|
||||
*/
|
||||
private function getPaymentRecepientRequest(): PaymentReceiptRequest
|
||||
{
|
||||
$service = new ServiceRequest();
|
||||
$service->setCallbackUrl('test.local');
|
||||
|
||||
$receipt = new ReceiptRequest();
|
||||
$receipt->setTotal('100');
|
||||
|
||||
/** @var PaymentReceiptRequest $request */
|
||||
$request = new PaymentReceiptRequest();
|
||||
$request->setExternalId('test');
|
||||
$request->setService($service);
|
||||
$request->setReceipt($receipt);
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
244
tests/AtolOnlineClient/AtolOnlineTest.php
Normal file
244
tests/AtolOnlineClient/AtolOnlineTest.php
Normal file
|
@ -0,0 +1,244 @@
|
|||
<?php
|
||||
|
||||
namespace AtolOnlineClient\Tests;
|
||||
|
||||
use AtolOnlineClient\AtolOnline;
|
||||
use AtolOnlineClient\Configuration\Connection;
|
||||
use AtolOnlineClient\ConfigurationInterface;
|
||||
use AtolOnlineClient\Request\V4\PaymentReceiptRequest;
|
||||
use AtolOnlineClient\Request\V4\ReceiptRequest;
|
||||
use AtolOnlineClient\Request\V4\ServiceRequest;
|
||||
use Guzzle\Http\Client;
|
||||
use JMS\Serializer\Exception\RuntimeException;
|
||||
use JMS\Serializer\SerializerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AtolOnlineTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var AtolOnline
|
||||
*/
|
||||
protected $atol;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->atol = new AtolOnline();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\AtolOnline::__construct
|
||||
*/
|
||||
public function testConstructor(): void
|
||||
{
|
||||
$reflection = new \ReflectionClass(get_class($this->atol));
|
||||
|
||||
$property = $reflection->getProperty('serializer');
|
||||
$property->setAccessible(true);
|
||||
|
||||
$this->assertInstanceOf(SerializerInterface::class, $property->getValue($this->atol));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \AtolOnlineClient\AtolOnline::createConfiguration
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateConfiguration(): void
|
||||
{
|
||||
$this->assertInstanceOf(ConfigurationInterface::class, $this->atol->createConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @covers \AtolOnlineClient\AtolOnline::deserializeOperationResponse
|
||||
* @dataProvider dataSuccessResponse
|
||||
*/
|
||||
public function testSuccessDeserializeOperationResponse(string $file): void
|
||||
{
|
||||
$response = file_get_contents(__DIR__.'/../fixtures/'. $file);
|
||||
|
||||
$operationResponse = $this->atol->deserializeOperationResponse($response);
|
||||
|
||||
$this->assertEquals('12.04.2017 06:15:06', $operationResponse->getTimestamp());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @covers \AtolOnlineClient\AtolOnline::deserializeOperationResponse
|
||||
* @dataProvider dataErrorResponse
|
||||
*/
|
||||
public function testErrorDeserializeOperationResponse(string $file): void
|
||||
{
|
||||
$response = file_get_contents(__DIR__.'/../fixtures/'. $file);
|
||||
|
||||
$operationResponse = $this->atol->deserializeOperationResponse($response);
|
||||
|
||||
$this->assertEquals('12.04.2017 06:15:06', $operationResponse->getTimestamp());
|
||||
$this->assertEquals('fail', $operationResponse->getStatus());
|
||||
$this->assertEquals(30, $operationResponse->getError()->getCode());
|
||||
$this->assertEquals('system', $operationResponse->getError()->getType());
|
||||
|
||||
$this->assertEquals(
|
||||
' Передан некорректный UUID : "{0}". Необходимо повторить запрос с корректными данными ',
|
||||
$operationResponse->getError()->getText()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \AtolOnlineClient\AtolOnline::deserializeOperationResponse
|
||||
*/
|
||||
public function testFailDeserializeOperationResponse(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->atol->deserializeOperationResponse('<html></html>');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @covers \AtolOnlineClient\AtolOnline::deserializeCheckStatusResponse
|
||||
* @dataProvider dataSuccessResponse
|
||||
*/
|
||||
public function testSuccessDeserializeCheckStatusResponse(string $file): void
|
||||
{
|
||||
$response = file_get_contents(__DIR__.'/../fixtures/'. $file);
|
||||
|
||||
$operationResponse = $this->atol->deserializeCheckStatusResponse($response);
|
||||
|
||||
$this->assertEquals('12.04.2017 06:15:06', $operationResponse->getTimestamp());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \AtolOnlineClient\AtolOnline::deserializeCheckStatusResponse
|
||||
*/
|
||||
public function testFailDeserializeCheckStatusResponse(): void
|
||||
{
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->atol->deserializeCheckStatusResponse('<html></html>');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \AtolOnlineClient\AtolOnline::serializeOperationRequest
|
||||
*/
|
||||
public function testSerializeOperationRequest(): void
|
||||
{
|
||||
$service = new ServiceRequest();
|
||||
$service->setCallbackUrl('test.local');
|
||||
|
||||
$receipt = new ReceiptRequest();
|
||||
$receipt->setTotal('100');
|
||||
|
||||
/** @var PaymentReceiptRequest $paymentReceipt */
|
||||
$paymentReceipt = new PaymentReceiptRequest();
|
||||
|
||||
$reflection = new \ReflectionClass($paymentReceipt);
|
||||
$property = $reflection->getProperty('timestamp');
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($paymentReceipt, 1);
|
||||
|
||||
$paymentReceipt->setExternalId('test');
|
||||
$paymentReceipt->setService($service);
|
||||
$paymentReceipt->setReceipt($receipt);
|
||||
|
||||
$request = $this->atol->serializeOperationRequest($paymentReceipt);
|
||||
|
||||
$this->assertEquals('{"external_id":"test","receipt":{"total":100},"timestamp":"1","service":{"callback_url":"test.local"}}', $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*
|
||||
* @covers \AtolOnlineClient\AtolOnline::createApi
|
||||
*/
|
||||
public function testCreateApi(): void
|
||||
{
|
||||
$api1 = $this->atol->createApi(new Client(), new Connection());
|
||||
$api2 = $this->atol->createApi(new Client(), new Connection());
|
||||
|
||||
$this->assertSame($api1, $api2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*
|
||||
* @covers \AtolOnlineClient\AtolOnline::getApi
|
||||
*/
|
||||
public function testGetApi(): void
|
||||
{
|
||||
$api1 = $this->atol->createApi(new Client(), new Connection());
|
||||
$api2 = $this->atol->getApi();
|
||||
|
||||
$this->assertSame($api1, $api2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataSuccessResponse(): array
|
||||
{
|
||||
return [
|
||||
['success_response_v4_1.json'],
|
||||
['success_response_v4_2.json'],
|
||||
['success_response_v4_3.json'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataErrorResponse(): array
|
||||
{
|
||||
return [
|
||||
['error_response_v3.json'],
|
||||
['error_response_v4.json']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataInvalidResponse(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'code' => 404,
|
||||
'message' => 'Not Found',
|
||||
'html' => '<html><head><title>404 Not Found</title></head><body><center><h1>404 Not Found</h1></center><hr><center>openresty/1.15.8.1rc1</center></body></html>',
|
||||
],
|
||||
[
|
||||
'code' => 502,
|
||||
'message' => 'Bad Gateway',
|
||||
'html' => '<html><head><title>502 Bad Gateway</title></head><body><center><h1>502 Bad Gateway</h1></center><hr><center>nginx/1.15.8</center></body></html>',
|
||||
],
|
||||
[
|
||||
'code' => null,
|
||||
'message' => null,
|
||||
'html' => '<html></html>',
|
||||
],
|
||||
[
|
||||
'code' => null,
|
||||
'message' => null,
|
||||
'html' => '',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $object
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*/
|
||||
private function callMethod($object, string $name, array $args = [])
|
||||
{
|
||||
$reflection = new \ReflectionClass($object);
|
||||
$method = $reflection->getMethod($name);
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invokeArgs($object, $args);
|
||||
}
|
||||
}
|
90
tests/AtolOnlineClient/Configuration/ConnectionTest.php
Normal file
90
tests/AtolOnlineClient/Configuration/ConnectionTest.php
Normal file
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace AtolOnlineClient\AtolOnlineClient\Connection;
|
||||
|
||||
use AtolOnlineClient\AtolOnlineApi;
|
||||
use AtolOnlineClient\Configuration\Connection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
|
||||
class ConnectionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->connection = new Connection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::setDebug
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::isDebug
|
||||
*/
|
||||
public function testDebug(): void
|
||||
{
|
||||
$this->assertFalse($this->connection->isDebug());
|
||||
|
||||
$this->connection->setDebug(true);
|
||||
$this->assertTrue($this->connection->isDebug());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::isTestMode
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::setTestMode
|
||||
*/
|
||||
public function testTestMode(): void
|
||||
{
|
||||
$this->assertFalse($this->connection->isTestMode());
|
||||
|
||||
$this->connection->setTestMode(true);
|
||||
$this->assertTrue($this->connection->isTestMode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::getVersion
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::isVersion4
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::isVersion3
|
||||
*/
|
||||
public function testVersion(): void
|
||||
{
|
||||
$this->connection->version = AtolOnlineApi::API_VERSION_V3;
|
||||
$this->assertEquals(AtolOnlineApi::API_VERSION_V3, $this->connection->getVersion());
|
||||
$this->assertTrue($this->connection->isVersion3());
|
||||
|
||||
$this->connection->version = AtolOnlineApi::API_VERSION_V4;
|
||||
$this->assertTrue($this->connection->isVersion4());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration\Connection::loadValidatorMetadata
|
||||
*/
|
||||
public function testValidator(): void
|
||||
{
|
||||
|
||||
$validator = Validation::createValidatorBuilder()
|
||||
->addMethodMapping('loadValidatorMetadata')
|
||||
->getValidator();
|
||||
|
||||
$list = $validator->validate($this->connection);
|
||||
|
||||
$this->assertEquals(3, $list->count());
|
||||
|
||||
$this->connection->login = 'login';
|
||||
$this->connection->pass = 'login';
|
||||
$this->connection->group = 'group';
|
||||
|
||||
$list = $validator->validate($this->connection);
|
||||
|
||||
$this->assertEquals(0, $list->count());
|
||||
}
|
||||
}
|
79
tests/AtolOnlineClient/ConfigurationTest.php
Normal file
79
tests/AtolOnlineClient/ConfigurationTest.php
Normal file
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace AtolOnlineClient\AtolOnlineClient;
|
||||
|
||||
use AtolOnlineClient\Configuration;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
|
||||
/**
|
||||
* Class ConfigurationTest
|
||||
*
|
||||
* @package AtolOnlineClient\AtolOnlineClient
|
||||
*/
|
||||
class ConfigurationTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Configuration
|
||||
*/
|
||||
private $configuration;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->configuration = new Configuration();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration::isDebug
|
||||
* @covers \AtolOnlineClient\Configuration::setDebug
|
||||
*/
|
||||
public function testDebug(): void
|
||||
{
|
||||
$this->assertFalse((bool) $this->configuration->isDebug());
|
||||
|
||||
$this->configuration->setDebug(true);
|
||||
$this->assertTrue($this->configuration->isDebug());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration::isEnabled
|
||||
* @covers \AtolOnlineClient\Configuration::setEnabled
|
||||
*/
|
||||
public function testEnabled(): void
|
||||
{
|
||||
$this->configuration->setEnabled(true);
|
||||
$this->assertTrue($this->configuration->isEnabled());
|
||||
|
||||
$this->configuration->setEnabled(false);
|
||||
$this->assertFalse($this->configuration->isEnabled());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Configuration::loadValidatorMetadata
|
||||
*/
|
||||
public function testValidator(): void
|
||||
{
|
||||
|
||||
$validator = Validation::createValidatorBuilder()
|
||||
->addMethodMapping('loadValidatorMetadata')
|
||||
->getValidator();
|
||||
|
||||
$list = $validator->validate($this->configuration);
|
||||
|
||||
$this->assertEquals(1, $list->count());
|
||||
|
||||
$connection = new Configuration\Connection();
|
||||
$connection->login = 'login';
|
||||
$connection->pass = 'login';
|
||||
$connection->group = 'group';
|
||||
|
||||
$this->configuration->connections[] = $connection;
|
||||
|
||||
$list = $validator->validate($this->configuration);
|
||||
|
||||
$this->assertEquals(0, $list->count());
|
||||
}
|
||||
}
|
63
tests/AtolOnlineClient/Response/ResponseTest.php
Normal file
63
tests/AtolOnlineClient/Response/ResponseTest.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace AtolOnlineClient\AtolOnlineClient\Response;
|
||||
|
||||
use AtolOnlineClient\Response\OperationResponse;
|
||||
use JMS\Serializer\SerializerBuilder;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
*/
|
||||
class ResponseTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
* @covers \AtolOnlineClient\Response\OperationResponse
|
||||
* @covers \AtolOnlineClient\Response\Error
|
||||
* @covers \AtolOnlineClient\Response\Payload
|
||||
*/
|
||||
public function testResponse(): void
|
||||
{
|
||||
$serializer = SerializerBuilder::create()->build();
|
||||
|
||||
$response = '{
|
||||
"uuid": "4355",
|
||||
"timestamp": "12.04.2017 06:15:06",
|
||||
"status": "fail",
|
||||
"error": {
|
||||
"error_id": "475d6d8d-844d-4d05-aa8b-e3dbdf4defd6",
|
||||
"code": 30,
|
||||
"text": " Передан некорректный UUID : \"{0}\". Необходимо повторить запрос с корректными данными ",
|
||||
"type": "system"
|
||||
},
|
||||
"payload": {
|
||||
"total": 1598,
|
||||
"fns_site": "www.nalog.ru",
|
||||
"fn_number": "1110000100238211",
|
||||
"shift_number": 23,
|
||||
"receipt_datetime": "12.04.2017 20:16:00",
|
||||
"fiscal_receipt_number": 6,
|
||||
"fiscal_document_number": 133,
|
||||
"ecr_registration_number": "0000111118041361",
|
||||
"fiscal_document_attribute": 3449555941
|
||||
},
|
||||
"group_code": " MyCompany_MyShop",
|
||||
"daemon_code": "prod–agent–1",
|
||||
"device_code": "KSR13.00–1–11",
|
||||
"external_id": "TRF10601_1",
|
||||
"callback_url": ""
|
||||
}';
|
||||
$response = $serializer->deserialize(
|
||||
$response,
|
||||
OperationResponse::class,
|
||||
'json'
|
||||
);
|
||||
|
||||
$serializer->serialize(
|
||||
$response,
|
||||
'json'
|
||||
);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace AtolOnlineClient\Tests;
|
||||
|
||||
use AtolOnlineClient\AtolOnline;
|
||||
use AtolOnlineClient\Configuration;
|
||||
use AtolOnlineClient\Response\OperationResponse;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AtolOnlineTest extends TestCase
|
||||
{
|
||||
|
||||
public function testCreateConfiguration()
|
||||
{
|
||||
$atol = new AtolOnline();
|
||||
$this->assertInstanceOf(Configuration::class, $atol->createConfiguration());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider dataSellErrorResponse
|
||||
*/
|
||||
public function testDeserializeOperationResponse($file)
|
||||
{
|
||||
$response = file_get_contents(__DIR__ . '/data/'. $file);
|
||||
$atol = new AtolOnline();
|
||||
$operationResponse = $atol->deserializeOperationResponse($response);
|
||||
$this->assertInstanceOf(OperationResponse::class, $operationResponse);
|
||||
$this->assertEquals('12.04.2017 06:15:06', $operationResponse->getTimestamp());
|
||||
$this->assertEquals('fail', $operationResponse->getStatus());
|
||||
$this->assertEquals(30, $operationResponse->getError()->getCode());
|
||||
$this->assertEquals('system', $operationResponse->getError()->getType());
|
||||
$this->assertEquals(
|
||||
' Передан некорректный UUID : "{0}". Необходимо повторить запрос с корректными данными ',
|
||||
$operationResponse->getError()->getText()
|
||||
);
|
||||
}
|
||||
|
||||
public function dataSellErrorResponse()
|
||||
{
|
||||
return [
|
||||
['sell_error_response_v3.json'],
|
||||
['sell_error_response_v3.json']
|
||||
];
|
||||
}
|
||||
}
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||
|
||||
/**
|
||||
* @var ClassLoader $loader
|
||||
*/
|
||||
$loader = require __DIR__.'/vendor/autoload.php';
|
||||
$loader = require __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
AnnotationRegistry::registerLoader('class_exists');
|
||||
|
||||
|
||||
return $loader;
|
5
tests/fixtures/success_response_v4_1.json
vendored
Normal file
5
tests/fixtures/success_response_v4_1.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"error": null,
|
||||
"token": "fj45u923j59ju42395iu9423i59243u0",
|
||||
"timestamp": "12.04.2017 06:15:06"
|
||||
}
|
6
tests/fixtures/success_response_v4_2.json
vendored
Normal file
6
tests/fixtures/success_response_v4_2.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"uuid": "2ea26f17–0884–4f08–b120–306fc096a58f",
|
||||
"timestamp": "12.04.2017 06:15:06",
|
||||
"error": null,
|
||||
"status": "wait"
|
||||
}
|
22
tests/fixtures/success_response_v4_3.json
vendored
Normal file
22
tests/fixtures/success_response_v4_3.json
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"uuid": "2ea26f17–0884–4f08–b120–306fc096a58f",
|
||||
"error": null,
|
||||
"status": "done",
|
||||
"payload": {
|
||||
"total": 1598,
|
||||
"fns_site": "www.nalog.ru",
|
||||
"fn_number": "1110000100238211",
|
||||
"shift_number": 23,
|
||||
"receipt_datetime": "12.04.2017 20:16:00",
|
||||
"fiscal_receipt_number": 6,
|
||||
"fiscal_document_number": 133,
|
||||
"ecr_registration_number": "0000111118041361",
|
||||
"fiscal_document_attribute": 3449555941
|
||||
},
|
||||
"timestamp": "12.04.2017 06:15:06",
|
||||
"group_code": "MyCompany_MyShop",
|
||||
"daemon_code": "prod–agent–1",
|
||||
"device_code": "KSR13.00–1–11",
|
||||
"external_id": "TRF10601_1",
|
||||
"callback_url": ""
|
||||
}
|
Loading…
Add table
Reference in a new issue