Skip to content

Commit fea9aaf

Browse files
committed
Adjusting the Unit Test for the isCurrent method
1 parent 29640f1 commit fea9aaf

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

lib/internal/Magento/Framework/View/Test/Unit/Element/Html/Link/CurrentTest.php

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ class CurrentTest extends \PHPUnit\Framework\TestCase
1717
*/
1818
protected $_requestMock;
1919

20-
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
22-
*/
23-
protected $_defaultPathMock;
24-
2520
/**
2621
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
2722
*/
@@ -32,7 +27,6 @@ protected function setUp()
3227
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3328
$this->_urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
3429
$this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
35-
$this->_defaultPathMock = $this->createMock(\Magento\Framework\App\DefaultPathInterface::class);
3630
}
3731

3832
public function testGetUrl()
@@ -60,31 +54,54 @@ public function testIsCurrentIfIsset()
6054
$this->assertTrue($link->isCurrent());
6155
}
6256

63-
public function testIsCurrent()
57+
/**
58+
* Test if the current url is the same as link path
59+
*
60+
* @dataProvider linkPathProvider
61+
* @param string $linkPath
62+
* @param string $currentPathInfo
63+
* @param bool $expected
64+
* @return void
65+
*/
66+
public function testIsCurrent($linkPath, $currentPathInfo, $expected)
6467
{
65-
$path = 'test/path';
66-
$url = 'http://example.com/a/b';
67-
68-
$this->_requestMock->expects($this->once())->method('getModuleName')->will($this->returnValue('a'));
69-
$this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
70-
$this->_requestMock->expects($this->once())->method('getActionName')->will($this->returnValue('d'));
71-
$this->_defaultPathMock->expects($this->atLeastOnce())->method('getPart')->will($this->returnValue('d'));
72-
73-
$this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->with($path)->will($this->returnValue($url));
74-
$this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->with('a/b')->will($this->returnValue($url));
75-
76-
$this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
68+
$baseUrl = 'http://example.com/';
69+
$trimmed = trim($currentPathInfo, '/');
70+
71+
$this->_requestMock->expects($this->any())->method('getPathInfo')->willReturn($currentPathInfo);
72+
$this->_urlBuilderMock->expects($this->at(0))
73+
->method('getUrl')
74+
->with($linkPath)
75+
->will($this->returnValue($baseUrl . $linkPath));
76+
$this->_urlBuilderMock->expects($this->at(1))
77+
->method('getUrl')
78+
->with($trimmed)
79+
->will($this->returnValue($baseUrl . $trimmed));
7780
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
7881
$link = $this->_objectManager->getObject(
7982
\Magento\Framework\View\Element\Html\Link\Current::class,
8083
[
8184
'urlBuilder' => $this->_urlBuilderMock,
82-
'request' => $this->_requestMock,
83-
'defaultPath' => $this->_defaultPathMock
85+
'request' => $this->_requestMock
8486
]
8587
);
86-
$link->setPath($path);
87-
$this->assertTrue($link->isCurrent());
88+
89+
$link->setCurrent(false);
90+
$link->setPath($linkPath);
91+
$this->assertEquals($expected, $link->isCurrent());
92+
}
93+
94+
/**
95+
* @return array
96+
*/
97+
public function linkPathProvider()
98+
{
99+
return [
100+
['test/index', '/test/index/', true],
101+
['test/index/index', '/test/index/index/', true],
102+
['test/route', '/test/index/', false],
103+
['test/index', '/test/', false]
104+
];
88105
}
89106

90107
public function testIsCurrentFalse()

0 commit comments

Comments
 (0)