Skip to content

Commit 71aa113

Browse files
ENGCOM-6488: [CMS] Improving the test coverage for UrlBuilder ViewModel #26069
2 parents eaa0617 + a2d9596 commit 71aa113

File tree

1 file changed

+194
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)