|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Framework\View\Test\Unit\Element\Html\Link;
|
7 | 9 |
|
8 |
| -class CurrentTest extends \PHPUnit\Framework\TestCase |
| 10 | +use Magento\Framework\App\Request\Http; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\Framework\UrlInterface; |
| 13 | +use Magento\Framework\View\Element\Html\Link\Current; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @covers \Magento\Framework\View\Element\Html\Link\Current |
| 19 | + */ |
| 20 | +class CurrentTest extends TestCase |
9 | 21 | {
|
10 | 22 | /**
|
11 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 23 | + * @var UrlInterface|MockObject |
12 | 24 | */
|
13 |
| - protected $_urlBuilderMock; |
| 25 | + private $_urlBuilderMock; |
14 | 26 |
|
15 | 27 | /**
|
16 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 28 | + * @var Http|MockObject |
17 | 29 | */
|
18 |
| - protected $_requestMock; |
| 30 | + private $_requestMock; |
19 | 31 |
|
20 | 32 | /**
|
21 |
| - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 33 | + * @var Current |
22 | 34 | */
|
23 |
| - protected $_objectManager; |
| 35 | + private $currentLink; |
24 | 36 |
|
25 |
| - protected function setUp() |
| 37 | + /** |
| 38 | + * @inheritDoc |
| 39 | + */ |
| 40 | + protected function setUp(): void |
26 | 41 | {
|
27 |
| - $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
28 |
| - $this->_urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class); |
29 |
| - $this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class); |
| 42 | + $this->_urlBuilderMock = $this->createMock(UrlInterface::class); |
| 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 | + ); |
30 | 52 | }
|
31 | 53 |
|
32 |
| - public function testGetUrl() |
| 54 | + /** |
| 55 | + * Test get Url |
| 56 | + */ |
| 57 | + public function testGetUrl(): void |
33 | 58 | {
|
34 |
| - $path = 'test/path'; |
35 |
| - $url = 'http://example.com/asdasd'; |
| 59 | + $pathStub = 'test/path'; |
| 60 | + $urlStub = 'http://example.com/asdasd'; |
36 | 61 |
|
37 |
| - $this->_urlBuilderMock->expects($this->once())->method('getUrl')->with($path)->will($this->returnValue($url)); |
| 62 | + $this->_urlBuilderMock->expects($this->once()) |
| 63 | + ->method('getUrl') |
| 64 | + ->with($pathStub) |
| 65 | + ->will($this->returnValue($urlStub)); |
38 | 66 |
|
39 |
| - /** @var \Magento\Framework\View\Element\Html\Link\Current $link */ |
40 |
| - $link = $this->_objectManager->getObject( |
41 |
| - \Magento\Framework\View\Element\Html\Link\Current::class, |
42 |
| - ['urlBuilder' => $this->_urlBuilderMock] |
43 |
| - ); |
| 67 | + $this->currentLink->setPath($pathStub); |
44 | 68 |
|
45 |
| - $link->setPath($path); |
46 |
| - $this->assertEquals($url, $link->getHref()); |
| 69 | + $this->assertEquals($urlStub, $this->currentLink->getHref()); |
47 | 70 | }
|
48 | 71 |
|
49 |
| - public function testIsCurrentIfIsset() |
| 72 | + /** |
| 73 | + * Test if set current |
| 74 | + */ |
| 75 | + public function testIsCurrentIfIsset(): void |
50 | 76 | {
|
51 |
| - /** @var \Magento\Framework\View\Element\Html\Link\Current $link */ |
52 |
| - $link = $this->_objectManager->getObject(\Magento\Framework\View\Element\Html\Link\Current::class); |
53 |
| - $link->setCurrent(true); |
54 |
| - $this->assertTrue($link->isCurrent()); |
| 77 | + $this->currentLink->setCurrent(true); |
| 78 | + $this->assertTrue($this->currentLink->isCurrent()); |
55 | 79 | }
|
56 | 80 |
|
57 | 81 | /**
|
58 | 82 | * Test if the current url is the same as link path
|
59 | 83 | *
|
60 |
| - * @return void |
| 84 | + * @param string $pathStub |
| 85 | + * @param string $urlStub |
| 86 | + * @param array $request |
| 87 | + * @param bool $expected |
| 88 | + * @dataProvider isCurrentDataProvider |
61 | 89 | */
|
62 |
| - public function testIsCurrent() |
| 90 | + public function testIsCurrent($pathStub, $urlStub, $request, $expected): void |
63 | 91 | {
|
64 |
| - $path = 'test/index'; |
65 |
| - $url = 'http://example.com/test/index'; |
66 |
| - |
67 |
| - $this->_requestMock->expects($this->once()) |
| 92 | + $this->_requestMock->expects($this->any()) |
68 | 93 | ->method('getPathInfo')
|
69 |
| - ->will($this->returnValue('/test/index/')); |
70 |
| - $this->_requestMock->expects($this->once()) |
| 94 | + ->will($this->returnValue($request['pathInfoStub'])); |
| 95 | + $this->_requestMock->expects($this->any()) |
71 | 96 | ->method('getModuleName')
|
72 |
| - ->will($this->returnValue('test')); |
73 |
| - $this->_requestMock->expects($this->once()) |
| 97 | + ->will($this->returnValue($request['moduleStub'])); |
| 98 | + $this->_requestMock->expects($this->any()) |
74 | 99 | ->method('getControllerName')
|
75 |
| - ->will($this->returnValue('index')); |
76 |
| - $this->_requestMock->expects($this->once()) |
| 100 | + ->will($this->returnValue($request['controllerStub'])); |
| 101 | + $this->_requestMock->expects($this->any()) |
77 | 102 | ->method('getActionName')
|
78 |
| - ->will($this->returnValue('index')); |
| 103 | + ->will($this->returnValue($request['actionStub'])); |
| 104 | + |
79 | 105 | $this->_urlBuilderMock->expects($this->at(0))
|
80 | 106 | ->method('getUrl')
|
81 |
| - ->with($path) |
82 |
| - ->will($this->returnValue($url)); |
| 107 | + ->with($pathStub) |
| 108 | + ->will($this->returnValue($urlStub)); |
83 | 109 | $this->_urlBuilderMock->expects($this->at(1))
|
84 | 110 | ->method('getUrl')
|
85 |
| - ->with('test/index') |
86 |
| - ->will($this->returnValue($url)); |
| 111 | + ->with($request['mcaStub']) |
| 112 | + ->will($this->returnValue($request['getUrl'])); |
87 | 113 |
|
88 |
| - /** @var \Magento\Framework\View\Element\Html\Link\Current $link */ |
89 |
| - $link = $this->_objectManager->getObject( |
90 |
| - \Magento\Framework\View\Element\Html\Link\Current::class, |
91 |
| - [ |
92 |
| - 'urlBuilder' => $this->_urlBuilderMock, |
93 |
| - 'request' => $this->_requestMock |
94 |
| - ] |
95 |
| - ); |
| 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 | + } |
96 | 120 |
|
97 |
| - $link->setPath($path); |
98 |
| - $this->assertTrue($link->isCurrent()); |
| 121 | + $this->currentLink->setPath($pathStub); |
| 122 | + $this->assertEquals($expected, $this->currentLink->isCurrent()); |
99 | 123 | }
|
100 | 124 |
|
101 |
| - public function testIsCurrentFalse() |
| 125 | + /** |
| 126 | + * Data provider for is current |
| 127 | + */ |
| 128 | + public function isCurrentDataProvider(): array |
102 | 129 | {
|
103 |
| - $this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->will($this->returnValue('1')); |
104 |
| - $this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->will($this->returnValue('2')); |
105 |
| - |
106 |
| - /** @var \Magento\Framework\View\Element\Html\Link\Current $link */ |
107 |
| - $link = $this->_objectManager->getObject( |
108 |
| - \Magento\Framework\View\Element\Html\Link\Current::class, |
109 |
| - ['urlBuilder' => $this->_urlBuilderMock, 'request' => $this->_requestMock] |
110 |
| - ); |
111 |
| - $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 | + ]; |
112 | 171 | }
|
113 | 172 | }
|
0 commit comments