Skip to content

Commit cf0725a

Browse files
committed
Covering the UrlBuilder ViewModel by Unit Test
1 parent ad6c093 commit cf0725a

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
/***
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Test\Unit\ViewModel\Page\Grid;
9+
10+
use Magento\Cms\ViewModel\Page\Grid\UrlBuilder;
11+
use Magento\Framework\Url\EncoderInterface;
12+
use Magento\Framework\UrlInterface;
13+
use Magento\Store\Api\Data\StoreInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Class UrlBuilderTest
20+
*/
21+
class UrlBuilderTest extends TestCase
22+
{
23+
/**
24+
* @var UrlBuilder
25+
*/
26+
private $viewModel;
27+
28+
/**
29+
* @var UrlInterface|MockObject
30+
*/
31+
private $frontendUrlBuilderMock;
32+
33+
/**
34+
* @var EncoderInterface|MockObject
35+
*/
36+
private $urlEncoderMock;
37+
38+
/**
39+
* @var StoreManagerInterface|MockObject
40+
*/
41+
private $storeManagerMock;
42+
43+
/**
44+
* Set Up
45+
*/
46+
public function setUp()
47+
{
48+
$this->frontendUrlBuilderMock = $this->getMockBuilder(UrlInterface::class)
49+
->setMethods(['getUrl', 'setScope'])
50+
->getMockForAbstractClass();
51+
$this->urlEncoderMock = $this->createMock(EncoderInterface::class);
52+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
56+
$this->viewModel = new UrlBuilder(
57+
$this->frontendUrlBuilderMock,
58+
$this->urlEncoderMock,
59+
$this->storeManagerMock
60+
);
61+
}
62+
63+
/**
64+
* Testing url builder with no scope provided
65+
*
66+
* @dataProvider nonScopedUrlsDataProvider
67+
*
68+
* @param array $url
69+
* @param string $expected
70+
* @param string $store
71+
* @param null $scope
72+
*/
73+
public function testUrlBuilderWithNoScope(array $url, string $expected, string $store, $scope = null)
74+
{
75+
$this->frontendUrlBuilderMock->expects($this->any())
76+
->method('getUrl')
77+
->with($url['path'], $url['params'])
78+
->willReturn($expected);
79+
80+
$result = $this->viewModel->getUrl($url['path'], $scope, $store);
81+
82+
$this->assertSame($expected, $result);
83+
}
84+
85+
/**
86+
* Providing a non scoped urls
87+
*
88+
* @return array
89+
*/
90+
public function nonScopedUrlsDataProvider(): array
91+
{
92+
return [
93+
[
94+
[
95+
'path' => 'test/view',
96+
'params' => [
97+
'_current' => false,
98+
'_nosid' => true
99+
]
100+
],
101+
'http://domain.com/test/view/',
102+
'en'
103+
]
104+
];
105+
}
106+
107+
/**
108+
* Testing url builder with a scope provided
109+
*
110+
* @dataProvider scopedUrlsDataProvider
111+
*
112+
* @param string $storeCode
113+
* @param string $defaultStoreCode
114+
* @param array $urlParams
115+
* @param string $scope
116+
*/
117+
public function testScopedUrlBuilder(
118+
string $storeCode,
119+
string $defaultStoreCode,
120+
array $urlParams,
121+
string $scope = 'store'
122+
) {
123+
/** @var StoreInterface|MockObject $storeMock */
124+
$storeMock = $this->createMock(StoreInterface::class);
125+
$storeMock->expects($this->any())
126+
->method('getCode')
127+
->willReturn($defaultStoreCode);
128+
$this->storeManagerMock->expects($this->once())
129+
->method('getDefaultStoreView')
130+
->willReturn($storeMock);
131+
132+
$this->frontendUrlBuilderMock->expects($this->any())
133+
->method('getUrl')
134+
->withConsecutive(
135+
[
136+
'test/index',
137+
[
138+
'_current' => false,
139+
'_nosid' => true,
140+
'_query' => [
141+
StoreManagerInterface::PARAM_NAME => $storeCode
142+
]
143+
]
144+
], [
145+
'stores/store/switch',
146+
$urlParams
147+
]
148+
)
149+
->willReturnOnConsecutiveCalls(
150+
'http://domain.com/test',
151+
'http://domain.com/test/index'
152+
);
153+
154+
$result = $this->viewModel->getUrl('test/index', $scope, $storeCode);
155+
156+
$this->assertSame('http://domain.com/test/index', $result);
157+
}
158+
159+
/**
160+
* Providing a scoped urls
161+
*
162+
* @return array
163+
*/
164+
public function scopedUrlsDataProvider(): array
165+
{
166+
$enStoreCode = 'en';
167+
$frStoreCode = 'fr';
168+
$scopedDefaultUrlParams = $defaultUrlParams = [
169+
'_current' => false,
170+
'_nosid' => true,
171+
'_query' => [
172+
'___store' => $enStoreCode,
173+
'uenc' => null,
174+
]
175+
];
176+
$scopedDefaultUrlParams['_query']['___from_store'] = $frStoreCode;
177+
178+
return [
179+
[
180+
$enStoreCode,
181+
$enStoreCode,
182+
$defaultUrlParams,
183+
],
184+
[
185+
$enStoreCode,
186+
$frStoreCode,
187+
$scopedDefaultUrlParams
188+
]
189+
];
190+
}
191+
}

0 commit comments

Comments
 (0)