Skip to content

Commit 2860283

Browse files
committed
Cover changes with unit test
1 parent 3f4642e commit 2860283

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Theme\Test\Unit\Block\Html;
7+
8+
use Magento\Framework\App\Config;
9+
use Magento\Framework\Data\Collection;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Framework\UrlInterface;
12+
use Magento\Framework\View\Element\Template\Context;
13+
use Magento\Theme\Block\Html\Pager;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test For Page class
18+
*/
19+
class PagerTest extends TestCase
20+
{
21+
22+
/**
23+
* @var Pager $pager
24+
*/
25+
private $pager;
26+
27+
/**
28+
* @var Context $context
29+
*/
30+
private $context;
31+
32+
/**
33+
* @var Config $scopeConfig
34+
*/
35+
private $scopeConfig;
36+
37+
/**
38+
* @var ObjectManager $objectManager
39+
*/
40+
private $objectManager;
41+
42+
/**
43+
* @var UrlInterface $urlBuilderMock
44+
*/
45+
private $urlBuilderMock;
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function setUp()
51+
{
52+
$this->context = $this->createMock(Context::class);
53+
$this->urlBuilderMock = $this->createMock(UrlInterface::class);
54+
$this->context->expects($this->any())
55+
->method('getUrlBuilder')
56+
->willReturn($this->urlBuilderMock);
57+
$this->scopeConfig = $this->createMock(Config::class);
58+
$this->pager = (new ObjectManager($this))->getObject(
59+
Pager::class,
60+
['context' => $this->context]
61+
);
62+
}
63+
64+
/**
65+
* Verify current page Url
66+
*
67+
* @return void
68+
*/
69+
public function testGetPageUrl(): void
70+
{
71+
$expectedPageUrl = 'page-url';
72+
$this->urlBuilderMock->expects($this->once())
73+
->method('getUrl')
74+
->willReturn($expectedPageUrl);
75+
$this->assertEquals($expectedPageUrl, $this->pager->getPageUrl(0));
76+
}
77+
78+
/**
79+
* Verify get pages method.
80+
*
81+
* @return void
82+
*/
83+
public function testGetPages(): void
84+
{
85+
$expectedPages = range(1, 5);
86+
$collectionMock = $this->createMock(Collection::class);
87+
$collectionMock->expects($this->exactly(2))
88+
->method('getCurPage')
89+
->willReturn(2);
90+
$collectionMock->expects($this->any())
91+
->method('getLastPageNumber')
92+
->willReturn(10);
93+
$this->setCollectionProperty($collectionMock);
94+
$this->assertEquals($expectedPages, $this->pager->getPages());
95+
}
96+
97+
/**
98+
* Set Collection
99+
*
100+
* @return void
101+
*/
102+
private function setCollectionProperty($collection): void
103+
{
104+
$reflection = new \ReflectionClass($this->pager);
105+
$reflection_property = $reflection->getProperty('_collection');
106+
$reflection_property->setAccessible(true);
107+
$reflection_property->setValue($this->pager, $collection);
108+
}
109+
}

0 commit comments

Comments
 (0)