1
0
mirror of synced 2024-11-22 20:06:01 +03:00
mg-bot-api-client-php/tests/Bot/Test/TestCase.php

150 lines
4.2 KiB
PHP
Raw Normal View History

2019-06-10 16:24:22 +03:00
<?php
/**
2019-07-16 15:04:54 +03:00
* PHP version 7.1
2019-06-10 16:24:22 +03:00
*
* Test case class
*
* @package Test
* @author RetailCrm <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
*/
namespace RetailCrm\Mg\Bot\Test;
use PHPUnit\Framework\TestCase as BaseCase;
2019-06-17 12:43:54 +03:00
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
2019-06-17 13:59:14 +03:00
use GuzzleHttp\Psr7\Response;
2019-06-10 16:24:22 +03:00
use RetailCrm\Mg\Bot\Client;
/**
* Class TestCase
*
* @package Test
* @author RetailCrm <integration@retailcrm.ru>
* @license https://opensource.org/licenses/MIT MIT License
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion5
*/
class TestCase extends BaseCase
{
/**
* Return bot api client object
*
2019-07-16 15:04:54 +03:00
* @param string $url (default: null)
* @param string $key (default: null)
* @param bool $debug (default: false)
* @param array ...$response (default: null)
2019-06-10 16:24:22 +03:00
*
2019-06-17 12:43:54 +03:00
* @return Client
2019-06-10 16:24:22 +03:00
*/
public static function getApiClient(
$url = null,
$key = null,
2019-06-17 12:43:54 +03:00
$debug = false,
...$response
2019-06-10 16:24:22 +03:00
) {
$configUrl = getenv('MG_BOT_URL');
$configKey = getenv('MG_BOT_KEY');
$configDbg = getenv('MG_BOT_DBG');
2019-06-17 12:43:54 +03:00
$mock = new MockHandler($response ?: []);
2019-06-10 16:24:22 +03:00
return new Client(
$url ?: $configUrl,
$key ?: $configKey,
2019-06-17 12:43:54 +03:00
$debug ?: $configDbg,
empty($response) ? null : HandlerStack::create($mock)
2019-06-10 16:24:22 +03:00
);
}
2019-06-17 13:59:14 +03:00
/**
* Returns mocked GuzzleHttp response
*
2019-07-16 15:04:54 +03:00
* @param string|null $body HTTP Body
* @param int $statusCode HTTP status code
2019-06-17 13:59:14 +03:00
*
* @return Response
*/
public function getResponse(string $body = null, int $statusCode = 200)
{
return new Response(
$statusCode,
array_filter(
[
'Server' => 'openresty/1.13.6.2',
'Date' => gmdate("D, d M Y H:m:s \G\M\T"),
'Content-Type' => 'application/json; charset=utf-8',
'Content-Length' => is_null($body) ? null : strlen($body),
'Connection' => 'keep-alive',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type, X-Api-Key, X-Client-Token',
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS'
]
),
$body
);
}
/**
* Generate and return mocked response.
* Response data should be stored in Resources directory as json file.
* Only file name (without extension or any other data) should be provided,
* e.g. `getJsonResponse('bots', 200)`
*
2019-07-16 15:04:54 +03:00
* @param string $jsonFile mocked body
* @param int $statusCode mocked status code
2019-06-17 13:59:14 +03:00
*
* @return Response|null
*/
public function getJsonResponse(string $jsonFile, int $statusCode = 200)
{
2019-06-24 09:34:59 +03:00
$fileName = realpath(
join(
DIRECTORY_SEPARATOR,
[__DIR__, '..', '..', 'Resources', \sprintf('%s.json', $jsonFile)]
)
);
2019-06-17 13:59:14 +03:00
if (file_exists($fileName)) {
$json = file_get_contents($fileName);
json_decode($json, true);
if (json_last_error() == JSON_ERROR_NONE) {
return $this->getResponse($json, $statusCode);
}
} else {
return null;
}
}
2019-06-21 17:51:57 +03:00
/**
2019-07-16 15:04:54 +03:00
* @param int $statusCode response code
* @param array ...$errors response errors
2019-06-21 17:51:57 +03:00
*
* @return Response
*/
public function getErrorsResponse(int $statusCode = 400, ...$errors)
{
$json = ['errors' => []];
foreach ($errors as $error) {
$json['errors'][] = is_string($error) ? $error : null;
}
return $this->getResponse(json_encode(array_filter($json)), $statusCode);
}
2019-06-17 13:59:14 +03:00
/**
* Generate and return empty response
*
2019-07-16 15:04:54 +03:00
* @param int $statusCode HTTP status code
2019-06-17 13:59:14 +03:00
*
* @return Response|null
*/
public function getEmptyResponse(int $statusCode = 200)
{
return $this->getResponse(null, $statusCode);
}
2019-06-10 16:24:22 +03:00
}