Skip to content

Feature/beg 144 implement unit tests #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

class Config
{
private const XML_PATH_RECACHE_ENABLED = 'system/prerender/enabled';
private const XML_PATH_PRERENDER_TOKEN = 'system/prerender/token';
private const XML_PATH_RECACHE_SERVICE_URL = 'system/prerender/service_url';
public const XML_PATH_RECACHE_ENABLED = 'system/prerender/enabled';
public const XML_PATH_PRERENDER_TOKEN = 'system/prerender/token';
public const XML_PATH_RECACHE_SERVICE_URL = 'system/prerender/service_url';

/** @var ScopeConfigInterface */
private ScopeConfigInterface $scopeConfig;
Expand Down
141 changes: 141 additions & 0 deletions Test/Unit/Api/PrerenderClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* Aligent Consulting
* Copyright (c) Aligent Consulting (https://www.aligent.com.au)
*/

declare(strict_types=1);

namespace Aligent\Prerender\Test\Unit\Api;

use Aligent\Prerender\Model\Api\PrerenderClient;
use Aligent\Prerender\Helper\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\ClientInterface;
use Magento\Framework\Serialize\SerializerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class PrerenderClientTest extends TestCase
{
/**
* @var PrerenderClient
*/
private PrerenderClient $prerenderClient;


/**
* @var Config
*/
private Config $prerenderConfigHelperMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->prerenderConfigHelperMock = $this->getMockBuilder(Config::class)
->setMethods(['isRecacheEnabled','getToken','getPrerenderServiceUrl'])
->disableOriginalConstructor()
->getMock();

$clientMock = $this->getMockForAbstractClass(ClientInterface::class);
$clientMock->expects($this->any())
->method('post')
->willReturnSelf();

$jsonSerializerMock = $this->getMockForAbstractClass(SerializerInterface::class);
$jsonSerializerMock->expects($this->any())
->method('serialize')
->willReturn(
'{"prerenderToken":"Pi7g7R7pNtDfgsDy1","urls":["abc.html","cde.html"]}'
);

$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
$this->prerenderClient = new PrerenderClient(
$this->prerenderConfigHelperMock,
$clientMock,
$jsonSerializerMock,
$loggerMock
);
}

/**
* test for recacheUrls method
*
* @return void
*/
public function testRecacheUrls(): void
{
$this->prerenderConfigHelperMock->expects($this->any())
->method('isRecacheEnabled')
->willReturn(true);

$this->prerenderConfigHelperMock->expects($this->any())
->method('getToken')
->willReturn('Pi7g7R7pNtDfgsDy1');

$this->prerenderConfigHelperMock->expects($this->any())
->method('getPrerenderServiceUrl')
->willReturn('https://api.prerender.io/recache');

$this->prerenderClient->recacheUrls(
[
'abc.html',
'cde.html',
],
1
);
}

/**
* test empty token
*
* @return void
*/
public function testEmptyToken(): void
{
$this->prerenderConfigHelperMock->expects($this->any())
->method('isRecacheEnabled')
->willReturn(true);
$this->prerenderConfigHelperMock->expects($this->any())
->method('getToken')
->willReturn('');

$this->prerenderClient->recacheUrls(
[
'abc.html',
'cde.html',
],
1
);
}

/**
* test empty PrerenderServiceUrl
*
* @return void
*/
public function testEmptyPrerenderServiceUrl(): void
{
$this->prerenderConfigHelperMock->expects($this->any())
->method('isRecacheEnabled')
->willReturn(true);
$this->prerenderConfigHelperMock->expects($this->any())
->method('getToken')
->willReturn('Pi7g7R7pNtDfgsDy1');

$this->prerenderConfigHelperMock->expects($this->any())
->method('getPrerenderServiceUrl')
->willReturn('');

$this->prerenderClient->recacheUrls(
[
'abc.html',
'cde.html',
],
1
);
}
}
87 changes: 87 additions & 0 deletions Test/Unit/Helper/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Aligent Consulting
* Copyright (c) Aligent Consulting (https://www.aligent.com.au)
*/

declare(strict_types=1);

namespace Aligent\Prerender\Test\Unit\Helper;

use Aligent\Prerender\Helper\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
use PHPUnit\Framework\TestCase;

class ConfigTest extends TestCase
{
/**
* Helper
*
* @var Config
*/
private Config $helper;

/**
* @var ScopeConfigInterface
*/
private ScopeConfigInterface $scopeConfigMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->helper = new Config($this->scopeConfigMock);
}

/**
* Test isRecacheEnabled()
*/
public function testIsRecacheEnabled(): void
{
$this->scopeConfigMock->expects($this->any())
->method('isSetFlag')
->willReturn(true);

$this->assertIsBool($this->helper->isRecacheEnabled());
}

/**
* Test if is not recache Enabled
*/
public function testIsNotRecacheEnabled(): void
{
$this->scopeConfigMock->expects($this->any())
->method('isSetFlag')
->willReturn(false);

$this->assertIsBool($this->helper->isRecacheEnabled());
}

/**
* Test getToken()
*/
public function testGetToken(): void
{
$this->scopeConfigMock->expects($this->any())
->method('getValue')
->with(Config::XML_PATH_PRERENDER_TOKEN)
->willReturn('Pi7g7R7pNtDfgsDy1');

$this->assertIsString($this->helper->getToken());
}

/**
* Test getPrerenderServiceUrl()
*/
public function testGetPrerenderServiceUrl(): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with(Config::XML_PATH_RECACHE_SERVICE_URL)
->willReturn('https://api.prerender.io/recache');

$this->assertIsString($this->helper->getPrerenderServiceUrl());
}
}