2020-09-28 17:18:21 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\Test;
|
|
|
|
|
2020-09-29 13:10:54 +03:00
|
|
|
use DateTime;
|
2020-09-30 13:18:35 +03:00
|
|
|
use Http\Client\Curl\Client as CurlClient;
|
|
|
|
use Http\Discovery\Psr17FactoryDiscovery;
|
|
|
|
use Http\Mock\Client as MockClient;
|
2020-10-01 14:59:40 +03:00
|
|
|
use InvalidArgumentException;
|
2020-09-30 13:18:35 +03:00
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory;
|
2020-09-28 17:18:21 +03:00
|
|
|
use Psr\Container\ContainerInterface;
|
2020-09-30 13:18:35 +03:00
|
|
|
use Psr\Http\Client\ClientInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\StreamInterface;
|
2020-09-29 14:18:53 +03:00
|
|
|
use RetailCrm\Builder\ContainerBuilder;
|
2020-09-29 13:10:54 +03:00
|
|
|
use RetailCrm\Component\AppData;
|
2020-10-01 16:29:49 +03:00
|
|
|
use RetailCrm\Component\Authenticator\TokenAuthenticator;
|
2020-09-30 13:18:35 +03:00
|
|
|
use RetailCrm\Component\Constants;
|
2020-09-28 17:18:21 +03:00
|
|
|
use RetailCrm\Component\Environment;
|
2020-09-29 14:18:53 +03:00
|
|
|
use RetailCrm\Component\Logger\StdoutLogger;
|
2020-09-29 13:10:54 +03:00
|
|
|
use RetailCrm\Factory\FileItemFactory;
|
|
|
|
use RetailCrm\Interfaces\AppDataInterface;
|
2020-10-01 16:29:49 +03:00
|
|
|
use RetailCrm\Interfaces\AuthenticatorInterface;
|
2020-09-29 15:18:03 +03:00
|
|
|
use RetailCrm\Interfaces\FileItemFactoryInterface;
|
2020-09-28 17:18:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TestCase
|
|
|
|
*
|
|
|
|
* @category TestCase
|
|
|
|
* @package ${NAMESPACE}
|
|
|
|
* @author RetailDriver LLC <integration@retailcrm.ru>
|
|
|
|
* @license MIT
|
|
|
|
* @link http://retailcrm.ru
|
|
|
|
* @see https://help.retailcrm.ru
|
|
|
|
*/
|
2020-09-29 14:18:53 +03:00
|
|
|
abstract class TestCase extends \PHPUnit\Framework\TestCase
|
2020-09-28 17:18:21 +03:00
|
|
|
{
|
|
|
|
private $container;
|
|
|
|
|
2020-09-29 13:10:54 +03:00
|
|
|
/**
|
2020-09-30 13:18:35 +03:00
|
|
|
* @param \Psr\Http\Client\ClientInterface|null $client
|
|
|
|
* @param bool $recreate
|
2020-09-29 13:10:54 +03:00
|
|
|
*
|
|
|
|
* @return \Psr\Container\ContainerInterface
|
|
|
|
*/
|
2020-09-30 13:18:35 +03:00
|
|
|
protected function getContainer(?ClientInterface $client = null, $recreate = false): ContainerInterface
|
2020-09-28 17:18:21 +03:00
|
|
|
{
|
2020-09-30 13:18:35 +03:00
|
|
|
if (null === $this->container || null !== $client || $recreate) {
|
|
|
|
$factory = new Psr17Factory();
|
2020-09-29 14:18:53 +03:00
|
|
|
$this->container = ContainerBuilder::create()
|
|
|
|
->setEnv(Environment::TEST)
|
2020-09-30 13:18:35 +03:00
|
|
|
->setClient(is_null($client) ? self::getMockClient() : $client)
|
2020-09-29 14:18:53 +03:00
|
|
|
->setLogger(new StdoutLogger())
|
2020-09-30 13:18:35 +03:00
|
|
|
->setStreamFactory($factory)
|
|
|
|
->setRequestFactory($factory)
|
|
|
|
->setUriFactory($factory)
|
2020-09-29 14:18:53 +03:00
|
|
|
->build();
|
2020-09-28 17:18:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->container;
|
|
|
|
}
|
2020-09-29 13:10:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \RetailCrm\Interfaces\AppDataInterface
|
|
|
|
*/
|
2020-09-29 16:04:02 +03:00
|
|
|
protected function getEnvAppData(): AppDataInterface
|
2020-09-29 13:10:54 +03:00
|
|
|
{
|
2020-09-29 16:04:02 +03:00
|
|
|
return $this->getAppData(
|
|
|
|
self::getenv('ENDPOINT', AppData::OVERSEAS_ENDPOINT),
|
2020-10-01 16:29:49 +03:00
|
|
|
self::getEnvAppKey(),
|
|
|
|
self::getenv('APP_SECRET', 'helloworld'),
|
|
|
|
self::getenv('REDIRECT_URI', 'https://example.com'),
|
2020-09-29 16:04:02 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $endpoint
|
|
|
|
* @param string $appKey
|
|
|
|
* @param string $appSecret
|
|
|
|
*
|
2020-10-01 16:29:49 +03:00
|
|
|
* @param string $redirectUri
|
|
|
|
*
|
2020-09-29 16:04:02 +03:00
|
|
|
* @return \RetailCrm\Interfaces\AppDataInterface
|
|
|
|
*/
|
|
|
|
protected function getAppData(
|
|
|
|
string $endpoint = AppData::OVERSEAS_ENDPOINT,
|
|
|
|
string $appKey = 'appKey',
|
2020-10-01 16:29:49 +03:00
|
|
|
string $appSecret = 'helloworld',
|
|
|
|
string $redirectUri = 'https://example.com'
|
2020-09-29 16:04:02 +03:00
|
|
|
): AppDataInterface{
|
2020-10-01 16:29:49 +03:00
|
|
|
return new AppData($endpoint, $appKey, $appSecret, $redirectUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getEnvTokenAuthenticator(): AuthenticatorInterface
|
|
|
|
{
|
2020-10-02 12:19:50 +03:00
|
|
|
return $this->getTokenAuthenticator(self::getEnvToken());
|
2020-10-01 16:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token
|
|
|
|
*
|
|
|
|
* @return \RetailCrm\Interfaces\AuthenticatorInterface
|
|
|
|
*/
|
|
|
|
protected function getTokenAuthenticator(string $token): AuthenticatorInterface
|
|
|
|
{
|
|
|
|
return new TokenAuthenticator($token);
|
2020-09-29 16:04:02 +03:00
|
|
|
}
|
|
|
|
|
2020-09-29 13:10:54 +03:00
|
|
|
/**
|
|
|
|
* @param string $signMethod
|
|
|
|
*
|
2020-09-29 13:18:18 +03:00
|
|
|
* @param bool $withFile
|
|
|
|
* @param bool $withDto
|
2020-09-29 13:10:54 +03:00
|
|
|
*
|
|
|
|
* @return \RetailCrm\Test\TestSignerRequest
|
|
|
|
*/
|
2020-09-29 13:18:18 +03:00
|
|
|
protected function getTestRequest(string $signMethod, bool $withFile = false, bool $withDto = false): TestSignerRequest
|
2020-09-29 13:10:54 +03:00
|
|
|
{
|
|
|
|
$request = new TestSignerRequest();
|
|
|
|
$request->appKey = '12345678';
|
|
|
|
$request->session = 'test';
|
|
|
|
$request->timestamp = DateTime::createFromFormat('Y-m-d H:i:s', '2016-01-01 12:00:00');
|
|
|
|
$request->signMethod = $signMethod;
|
|
|
|
$request->serviceName = 'SPAIN_LOCAL_CORREOS';
|
|
|
|
$request->outRef = '1000006270175804';
|
|
|
|
$request->sendType = 'all';
|
|
|
|
$request->logisticsNo = 'ES2019COM0000123456';
|
|
|
|
|
|
|
|
if ($withFile) {
|
|
|
|
/** @var FileItemFactory $factory */
|
2020-09-29 15:18:03 +03:00
|
|
|
$factory = $this->getContainer()->get(FileItemFactoryInterface::class);
|
2020-09-29 13:10:54 +03:00
|
|
|
$request->document = $factory->fromString(
|
|
|
|
'file.txt',
|
|
|
|
'The quick brown fox jumps over the lazy dog'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-29 13:18:18 +03:00
|
|
|
if ($withDto) {
|
|
|
|
$request->dto = new TestDto();
|
|
|
|
}
|
|
|
|
|
2020-09-29 13:10:54 +03:00
|
|
|
return $request;
|
|
|
|
}
|
2020-09-29 16:04:02 +03:00
|
|
|
|
2020-09-30 13:18:35 +03:00
|
|
|
/**
|
|
|
|
* @param int $code
|
2020-10-01 14:59:40 +03:00
|
|
|
* @param object|array|string $response
|
2020-09-30 13:18:35 +03:00
|
|
|
*
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
2020-10-01 14:59:40 +03:00
|
|
|
* @throws InvalidArgumentException
|
2020-09-30 13:18:35 +03:00
|
|
|
*/
|
|
|
|
protected function responseJson(int $code, $response): ResponseInterface
|
|
|
|
{
|
|
|
|
/** @var \JMS\Serializer\SerializerInterface $serializer */
|
|
|
|
$serializer = $this->getContainer()->get(Constants::SERIALIZER);
|
|
|
|
$responseFactory = Psr17FactoryDiscovery::findResponseFactory();
|
|
|
|
$streamFactory = Psr17FactoryDiscovery::findStreamFactory();
|
2020-10-01 14:59:40 +03:00
|
|
|
$data = null;
|
|
|
|
|
|
|
|
switch (gettype($response)) {
|
|
|
|
case 'string':
|
|
|
|
$data = $streamFactory->createStream($response);
|
|
|
|
break;
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
$data = $streamFactory->createStream($serializer->serialize($response, 'json'));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Expected string, object, or array, got "%s"',
|
|
|
|
gettype($response)
|
|
|
|
));
|
|
|
|
}
|
2020-09-30 13:18:35 +03:00
|
|
|
|
|
|
|
return $responseFactory->createResponse($code)
|
|
|
|
->withHeader('Content-Type', 'application/json')
|
2020-10-01 14:59:40 +03:00
|
|
|
->withBody($data);
|
2020-09-30 13:18:35 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 16:29:49 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getEnvAppKey(): string
|
|
|
|
{
|
|
|
|
return self::getenv('APP_KEY', 'appKey');
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:19:50 +03:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getEnvToken(): string
|
|
|
|
{
|
|
|
|
return self::getenv('SESSION', 'test');
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:04:02 +03:00
|
|
|
/**
|
|
|
|
* @param string $variable
|
|
|
|
* @param mixed $default
|
|
|
|
*
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
protected static function getenv(string $variable, $default = null)
|
|
|
|
{
|
|
|
|
if (!array_key_exists($variable, $_ENV)) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_ENV[$variable];
|
|
|
|
}
|
2020-09-30 13:18:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Http\Mock\Client
|
|
|
|
*/
|
|
|
|
protected static function getMockClient(): MockClient
|
|
|
|
{
|
2020-10-01 16:29:49 +03:00
|
|
|
$client = new MockClient();
|
|
|
|
$client->setDefaultException(new MatcherException());
|
|
|
|
return $client;
|
2020-09-30 13:18:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Psr\Http\Client\ClientInterface
|
|
|
|
*/
|
|
|
|
protected static function getCurlClient(): ClientInterface
|
|
|
|
{
|
|
|
|
return new CurlClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Psr\Http\Message\StreamInterface $stream
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getStreamData(StreamInterface $stream): string
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
|
|
|
|
if ($stream->isSeekable()) {
|
|
|
|
$data = $stream->__toString();
|
|
|
|
$stream->seek(0);
|
|
|
|
} else {
|
|
|
|
$data = $stream->getContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2020-09-28 17:18:21 +03:00
|
|
|
}
|