|
14 | 14 | use PHPUnit\Framework\MockObject\MockObject;
|
15 | 15 | use PHPUnit\Framework\TestCase;
|
16 | 16 |
|
| 17 | +/** |
| 18 | + * @covers \Magento\Framework\View\Element\Html\Link\Current |
| 19 | + */ |
17 | 20 | class CurrentTest extends TestCase
|
18 | 21 | {
|
19 | 22 | /**
|
20 |
| - * @var MockObject |
| 23 | + * @var UrlInterface|MockObject |
21 | 24 | */
|
22 |
| - protected $_urlBuilderMock; |
| 25 | + private $_urlBuilderMock; |
23 | 26 |
|
24 | 27 | /**
|
25 |
| - * @var MockObject |
| 28 | + * @var Http|MockObject |
26 | 29 | */
|
27 |
| - protected $_requestMock; |
| 30 | + private $_requestMock; |
28 | 31 |
|
29 | 32 | /**
|
30 |
| - * @var ObjectManager |
| 33 | + * @var Current |
31 | 34 | */
|
32 |
| - protected $_objectManager; |
| 35 | + private $currentLink; |
33 | 36 |
|
| 37 | + /** |
| 38 | + * @inheritDoc |
| 39 | + */ |
34 | 40 | protected function setUp(): void
|
35 | 41 | {
|
36 |
| - $this->_objectManager = new ObjectManager($this); |
37 |
| - $this->_urlBuilderMock = $this->getMockForAbstractClass(UrlInterface::class); |
| 42 | + $this->_urlBuilderMock = $this->createMock(UrlInterface::class); |
38 | 43 | $this->_requestMock = $this->createMock(Http::class);
|
| 44 | + |
| 45 | + $this->currentLink = (new ObjectManager($this))->getObject( |
| 46 | + Current::class, |
| 47 | + [ |
| 48 | + 'urlBuilder' => $this->_urlBuilderMock, |
| 49 | + 'request' => $this->_requestMock |
| 50 | + ] |
| 51 | + ); |
39 | 52 | }
|
40 | 53 |
|
41 |
| - public function testGetUrl() |
| 54 | + /** |
| 55 | + * Test get Url |
| 56 | + */ |
| 57 | + public function testGetUrl(): void |
42 | 58 | {
|
43 |
| - $path = 'test/path'; |
44 |
| - $url = 'http://example.com/asdasd'; |
| 59 | + $pathStub = 'test/path'; |
| 60 | + $urlStub = 'http://example.com/asdasd'; |
45 | 61 |
|
46 |
| - $this->_urlBuilderMock->expects($this->once())->method('getUrl')->with($path)->willReturn($url); |
| 62 | + $this->_urlBuilderMock->expects($this->once()) |
| 63 | + ->method('getUrl') |
| 64 | + ->with($pathStub) |
| 65 | + ->will($this->returnValue($urlStub)); |
47 | 66 |
|
48 |
| - /** @var Current $link */ |
49 |
| - $link = $this->_objectManager->getObject( |
50 |
| - Current::class, |
51 |
| - ['urlBuilder' => $this->_urlBuilderMock] |
52 |
| - ); |
| 67 | + $this->currentLink->setPath($pathStub); |
53 | 68 |
|
54 |
| - $link->setPath($path); |
55 |
| - $this->assertEquals($url, $link->getHref()); |
| 69 | + $this->assertEquals($urlStub, $this->currentLink->getHref()); |
56 | 70 | }
|
57 | 71 |
|
58 |
| - public function testIsCurrentIfIsset() |
| 72 | + /** |
| 73 | + * Test if set current |
| 74 | + */ |
| 75 | + public function testIsCurrentIfIsset(): void |
59 | 76 | {
|
60 |
| - /** @var Current $link */ |
61 |
| - $link = $this->_objectManager->getObject(Current::class); |
62 |
| - $link->setCurrent(true); |
63 |
| - $this->assertTrue($link->isCurrent()); |
| 77 | + $this->currentLink->setCurrent(true); |
| 78 | + $this->assertTrue($this->currentLink->isCurrent()); |
64 | 79 | }
|
65 | 80 |
|
66 | 81 | /**
|
67 | 82 | * Test if the current url is the same as link path
|
68 | 83 | *
|
69 |
| - * @return void |
| 84 | + * @param string $pathStub |
| 85 | + * @param string $urlStub |
| 86 | + * @param array $request |
| 87 | + * @param bool $expected |
| 88 | + * @dataProvider isCurrentDataProvider |
70 | 89 | */
|
71 |
| - public function testIsCurrent() |
| 90 | + public function testIsCurrent($pathStub, $urlStub, $request, $expected): void |
72 | 91 | {
|
73 |
| - $path = 'test/index'; |
74 |
| - $url = 'http://example.com/test/index'; |
75 |
| - |
76 |
| - $this->_requestMock->expects($this->once()) |
| 92 | + $this->_requestMock->expects($this->any()) |
77 | 93 | ->method('getPathInfo')
|
78 |
| - ->willReturn('/test/index/'); |
79 |
| - $this->_requestMock->expects($this->once()) |
| 94 | + ->will($this->returnValue($request['pathInfoStub'])); |
| 95 | + $this->_requestMock->expects($this->any()) |
80 | 96 | ->method('getModuleName')
|
81 |
| - ->willReturn('test'); |
82 |
| - $this->_requestMock->expects($this->once()) |
| 97 | + ->will($this->returnValue($request['moduleStub'])); |
| 98 | + $this->_requestMock->expects($this->any()) |
83 | 99 | ->method('getControllerName')
|
84 |
| - ->willReturn('index'); |
85 |
| - $this->_requestMock->expects($this->once()) |
| 100 | + ->will($this->returnValue($request['controllerStub'])); |
| 101 | + $this->_requestMock->expects($this->any()) |
86 | 102 | ->method('getActionName')
|
87 |
| - ->willReturn('index'); |
| 103 | + ->will($this->returnValue($request['actionStub'])); |
| 104 | + |
88 | 105 | $this->_urlBuilderMock->expects($this->at(0))
|
89 | 106 | ->method('getUrl')
|
90 |
| - ->with($path) |
91 |
| - ->willReturn($url); |
| 107 | + ->with($pathStub) |
| 108 | + ->will($this->returnValue($urlStub)); |
92 | 109 | $this->_urlBuilderMock->expects($this->at(1))
|
93 | 110 | ->method('getUrl')
|
94 |
| - ->with('test/index') |
95 |
| - ->willReturn($url); |
96 |
| - |
97 |
| - /** @var Current $link */ |
98 |
| - $link = $this->_objectManager->getObject( |
99 |
| - Current::class, |
100 |
| - [ |
101 |
| - 'urlBuilder' => $this->_urlBuilderMock, |
102 |
| - 'request' => $this->_requestMock |
103 |
| - ] |
104 |
| - ); |
105 |
| - |
106 |
| - $link->setPath($path); |
107 |
| - $this->assertTrue($link->isCurrent()); |
| 111 | + ->with($request['mcaStub']) |
| 112 | + ->will($this->returnValue($request['getUrl'])); |
| 113 | + |
| 114 | + if ($request['mcaStub'] == '') { |
| 115 | + $this->_urlBuilderMock->expects($this->at(2)) |
| 116 | + ->method('getUrl') |
| 117 | + ->with('*/*/*', ['_current' => false, '_use_rewrite' => true]) |
| 118 | + ->will($this->returnValue($urlStub)); |
| 119 | + } |
| 120 | + |
| 121 | + $this->currentLink->setPath($pathStub); |
| 122 | + $this->assertEquals($expected, $this->currentLink->isCurrent()); |
108 | 123 | }
|
109 | 124 |
|
110 |
| - public function testIsCurrentFalse() |
| 125 | + /** |
| 126 | + * Data provider for is current |
| 127 | + */ |
| 128 | + public function isCurrentDataProvider(): array |
111 | 129 | {
|
112 |
| - $this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->willReturn('1'); |
113 |
| - $this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->willReturn('2'); |
114 |
| - |
115 |
| - /** @var Current $link */ |
116 |
| - $link = $this->_objectManager->getObject( |
117 |
| - Current::class, |
118 |
| - ['urlBuilder' => $this->_urlBuilderMock, 'request' => $this->_requestMock] |
119 |
| - ); |
120 |
| - $this->assertFalse($link->isCurrent()); |
| 130 | + return [ |
| 131 | + 'url with MCA' => [ |
| 132 | + 'pathStub' => 'test/path', |
| 133 | + 'urlStub' => 'http://example.com/asdasd', |
| 134 | + 'requestStub' => [ |
| 135 | + 'pathInfoStub' => '/test/index/', |
| 136 | + 'moduleStub' => 'test', |
| 137 | + 'controllerStub' => 'index', |
| 138 | + 'actionStub' => 'index', |
| 139 | + 'mcaStub' => 'test/index', |
| 140 | + 'getUrl' => 'http://example.com/asdasd/' |
| 141 | + ], |
| 142 | + 'excepted' => true |
| 143 | + ], |
| 144 | + 'url with CMS' => [ |
| 145 | + 'pathStub' => 'test', |
| 146 | + 'urlStub' => 'http://example.com/test', |
| 147 | + 'requestStub' => [ |
| 148 | + 'pathInfoStub' => '//test//', |
| 149 | + 'moduleStub' => 'cms', |
| 150 | + 'controllerStub' => 'page', |
| 151 | + 'actionStub' => 'view', |
| 152 | + 'mcaStub' => '', |
| 153 | + 'getUrl' => 'http://example.com/' |
| 154 | + ], |
| 155 | + 'excepted' => true |
| 156 | + ], |
| 157 | + 'Test if is current false' => [ |
| 158 | + 'pathStub' => 'test/path', |
| 159 | + 'urlStub' => 'http://example.com/tests', |
| 160 | + 'requestStub' => [ |
| 161 | + 'pathInfoStub' => '/test/index/', |
| 162 | + 'moduleStub' => 'test', |
| 163 | + 'controllerStub' => 'index', |
| 164 | + 'actionStub' => 'index', |
| 165 | + 'mcaStub' => 'test/index', |
| 166 | + 'getUrl' => 'http://example.com/asdasd/' |
| 167 | + ], |
| 168 | + 'excepted' => false |
| 169 | + ] |
| 170 | + ]; |
121 | 171 | }
|
122 | 172 | }
|
0 commit comments