mirror of
https://github.com/elyby/accounts.git
synced 2025-01-11 14:32:12 +05:30
Add tests for emails renderer api client and Yii2 wrapper component
This commit is contained in:
parent
70d1999d55
commit
d5cb0f304c
@ -20,7 +20,6 @@ coverage:
|
||||
- config/*
|
||||
- runtime/*
|
||||
- tests/*
|
||||
- web/*
|
||||
- codeception.dist.yml
|
||||
- codeception.yml
|
||||
c3url: 'http://localhost/api/web/index.php'
|
||||
|
@ -8,7 +8,6 @@ use common\emails\RendererInterface;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\helpers\FileHelper;
|
||||
|
||||
class Component extends \yii\base\Component implements RendererInterface {
|
||||
|
||||
@ -63,7 +62,7 @@ class Component extends \yii\base\Component implements RendererInterface {
|
||||
return $this->getApi()->getTemplate($request);
|
||||
}
|
||||
|
||||
private function getApi(): Api {
|
||||
protected function getApi(): Api {
|
||||
if ($this->api === null) {
|
||||
$this->api = new Api($this->serviceUrl);
|
||||
}
|
||||
@ -72,7 +71,7 @@ class Component extends \yii\base\Component implements RendererInterface {
|
||||
}
|
||||
|
||||
private function buildBasePath(): string {
|
||||
return FileHelper::normalizePath($this->baseDomain . '/' . $this->basePath, '/');
|
||||
return trim($this->baseDomain, '/') . '/' . trim($this->basePath, '/');
|
||||
}
|
||||
|
||||
}
|
||||
|
58
common/tests/unit/components/EmailsRenderer/ApiTest.php
Normal file
58
common/tests/unit/components/EmailsRenderer/ApiTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\components\EmailsRenderer;
|
||||
|
||||
use common\components\EmailsRenderer\Api;
|
||||
use common\components\EmailsRenderer\Request\TemplateRequest;
|
||||
use common\tests\unit\TestCase;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Middleware;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
|
||||
class ApiTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var Api
|
||||
*/
|
||||
private $api;
|
||||
|
||||
/**
|
||||
* @var MockHandler
|
||||
*/
|
||||
private $mockHandler;
|
||||
|
||||
/**
|
||||
* @var \Psr\Http\Message\RequestInterface[]
|
||||
*/
|
||||
private $history;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->mockHandler = new MockHandler();
|
||||
$handlerStack = HandlerStack::create($this->mockHandler);
|
||||
$this->history = [];
|
||||
$handlerStack->push(Middleware::history($this->history), 'history');
|
||||
$client = new Client([
|
||||
'handler' => $handlerStack,
|
||||
'base_uri' => 'http://emails-renderer',
|
||||
]);
|
||||
$this->api = new Api('http://emails-renderer');
|
||||
$this->api->setClient($client);
|
||||
}
|
||||
|
||||
public function testGetTemplate() {
|
||||
$this->mockHandler->append(new Response(200, [], 'mock-response'));
|
||||
|
||||
$request = new TemplateRequest('mock-name', 'mock-locale', ['find-me' => 'please']);
|
||||
$this->assertSame('mock-response', $this->api->getTemplate($request));
|
||||
|
||||
/** @var \Psr\Http\Message\RequestInterface $request */
|
||||
['request' => $request] = $this->history[0];
|
||||
$this->assertSame('http://emails-renderer/templates/mock-locale/mock-name?find-me=please', (string)$request->getUri());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace common\tests\unit\components\EmailsRenderer;
|
||||
|
||||
use common\components\EmailsRenderer\Api;
|
||||
use common\components\EmailsRenderer\Component;
|
||||
use common\components\EmailsRenderer\Request\TemplateRequest;
|
||||
use common\tests\unit\TestCase;
|
||||
|
||||
class ComponentTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @var Api|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $api;
|
||||
|
||||
/**
|
||||
* @var Component
|
||||
*/
|
||||
private $component;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->api = $this->createMock(Api::class);
|
||||
$componentParams = [
|
||||
'api' => $this->api,
|
||||
'serviceUrl' => 'http://emails-renderer',
|
||||
'basePath' => '/images/emails-templates',
|
||||
];
|
||||
$this->component = new class($componentParams) extends Component {
|
||||
public $api;
|
||||
|
||||
protected function getApi(): Api {
|
||||
return $this->api;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function testRender() {
|
||||
$expectedRequest = new TemplateRequest('mock-name', 'mock-locale', [
|
||||
'find-me' => 'please',
|
||||
'assetsHost' => 'http://localhost/images/emails-templates',
|
||||
]);
|
||||
|
||||
$this->api->expects($this->once())->method('getTemplate')->with($expectedRequest)->willReturn('mock-template');
|
||||
|
||||
$result = $this->component->render('mock-name', 'mock-locale', ['find-me' => 'please']);
|
||||
$this->assertSame('mock-template', $result);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user