@@ -17,11 +17,6 @@ class CurrentTest extends \PHPUnit\Framework\TestCase
17
17
*/
18
18
protected $ _requestMock ;
19
19
20
- /**
21
- * @var \PHPUnit_Framework_MockObject_MockObject
22
- */
23
- protected $ _defaultPathMock ;
24
-
25
20
/**
26
21
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
27
22
*/
@@ -32,7 +27,6 @@ protected function setUp()
32
27
$ this ->_objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
33
28
$ this ->_urlBuilderMock = $ this ->createMock (\Magento \Framework \UrlInterface::class);
34
29
$ this ->_requestMock = $ this ->createMock (\Magento \Framework \App \Request \Http::class);
35
- $ this ->_defaultPathMock = $ this ->createMock (\Magento \Framework \App \DefaultPathInterface::class);
36
30
}
37
31
38
32
public function testGetUrl ()
@@ -60,31 +54,54 @@ public function testIsCurrentIfIsset()
60
54
$ this ->assertTrue ($ link ->isCurrent ());
61
55
}
62
56
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 )
64
67
{
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 ));
77
80
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
78
81
$ link = $ this ->_objectManager ->getObject (
79
82
\Magento \Framework \View \Element \Html \Link \Current::class,
80
83
[
81
84
'urlBuilder ' => $ this ->_urlBuilderMock ,
82
- 'request ' => $ this ->_requestMock ,
83
- 'defaultPath ' => $ this ->_defaultPathMock
85
+ 'request ' => $ this ->_requestMock
84
86
]
85
87
);
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
+ ];
88
105
}
89
106
90
107
public function testIsCurrentFalse ()
0 commit comments