Skip to content

Commit

Permalink
Cover changes with unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar65 committed Jan 6, 2020
1 parent 3f4642e commit 2860283
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions app/code/Magento/Theme/Test/Unit/Block/Html/PagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Test\Unit\Block\Html;

use Magento\Framework\App\Config;
use Magento\Framework\Data\Collection;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template\Context;
use Magento\Theme\Block\Html\Pager;
use PHPUnit\Framework\TestCase;

/**
* Test For Page class
*/
class PagerTest extends TestCase
{

/**
* @var Pager $pager
*/
private $pager;

/**
* @var Context $context
*/
private $context;

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

/**
* @var ObjectManager $objectManager
*/
private $objectManager;

/**
* @var UrlInterface $urlBuilderMock
*/
private $urlBuilderMock;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->context = $this->createMock(Context::class);
$this->urlBuilderMock = $this->createMock(UrlInterface::class);
$this->context->expects($this->any())
->method('getUrlBuilder')
->willReturn($this->urlBuilderMock);
$this->scopeConfig = $this->createMock(Config::class);
$this->pager = (new ObjectManager($this))->getObject(
Pager::class,
['context' => $this->context]
);
}

/**
* Verify current page Url
*
* @return void
*/
public function testGetPageUrl(): void
{
$expectedPageUrl = 'page-url';
$this->urlBuilderMock->expects($this->once())
->method('getUrl')
->willReturn($expectedPageUrl);
$this->assertEquals($expectedPageUrl, $this->pager->getPageUrl(0));
}

/**
* Verify get pages method.
*
* @return void
*/
public function testGetPages(): void
{
$expectedPages = range(1, 5);
$collectionMock = $this->createMock(Collection::class);
$collectionMock->expects($this->exactly(2))
->method('getCurPage')
->willReturn(2);
$collectionMock->expects($this->any())
->method('getLastPageNumber')
->willReturn(10);
$this->setCollectionProperty($collectionMock);
$this->assertEquals($expectedPages, $this->pager->getPages());
}

/**
* Set Collection
*
* @return void
*/
private function setCollectionProperty($collection): void
{
$reflection = new \ReflectionClass($this->pager);
$reflection_property = $reflection->getProperty('_collection');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->pager, $collection);
}
}

0 comments on commit 2860283

Please sign in to comment.