Skip to content

Commit 734db2a

Browse files
committed
ACP2E-3324: added unit test
1 parent aaf0bf2 commit 734db2a

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Theme\Test\Unit\Model\Config;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Theme\Api\Data\DesignConfigDataInterface;
12+
use Magento\Theme\Api\Data\DesignConfigInterface;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
use Magento\Theme\Model\Config\PathValidator;
16+
use Magento\Config\Model\Config\Structure;
17+
use Magento\Theme\Model\DesignConfigRepository;
18+
use Magento\Framework\Exception\ValidatorException;
19+
20+
class PathValidatorTest extends TestCase
21+
{
22+
/**
23+
* @var Structure|MockObject
24+
*/
25+
private $structure;
26+
27+
/**
28+
* @var DesignConfigRepository|MockObject
29+
*/
30+
private $designConfigRepository;
31+
32+
/**
33+
* @var PathValidator
34+
*/
35+
private $pathValidator;
36+
37+
protected function setUp(): void
38+
{
39+
$this->structure = $this->createMock(Structure::class);
40+
$this->designConfigRepository = $this->createMock(DesignConfigRepository::class);
41+
42+
$this->pathValidator = new PathValidator(
43+
$this->structure,
44+
$this->designConfigRepository
45+
);
46+
}
47+
48+
public function testValidateNonDesignPath()
49+
{
50+
$path = 'non_design/path';
51+
$this->structure->expects($this->once())
52+
->method('getElementByConfigPath')
53+
->with($path)
54+
->willReturn(null);
55+
56+
$this->structure->expects($this->once())
57+
->method('getFieldPaths')
58+
->willReturn(['non_design/path' => 'non_design/path']);
59+
60+
$result = $this->pathValidator->validate($path);
61+
$this->assertTrue($result);
62+
}
63+
64+
public function testValidateDesignPath()
65+
{
66+
$path = 'design/path';
67+
$element = $this->createMock(Structure\Element\Field::class);
68+
$designConfig = $this->createMock(DesignConfigInterface::class);
69+
$extensionAttributes = $this->createMock(DataObject::class);
70+
$designConfigData = $this->createMock(DesignConfigDataInterface::class);
71+
72+
$element->expects($this->exactly(2))
73+
->method('getConfigPath')
74+
->willReturn($path);
75+
$this->structure->expects($this->once())
76+
->method('getElementByConfigPath')
77+
->with($path)
78+
->willReturn($element);
79+
$this->structure->expects($this->once())
80+
->method('getFieldPaths')
81+
->willReturn([]);
82+
$this->designConfigRepository->expects($this->once())
83+
->method('getByScope')
84+
->with('default', null)
85+
->willReturn($designConfig);
86+
$designConfig->expects($this->once())
87+
->method('getExtensionAttributes')
88+
->willReturn($extensionAttributes);
89+
$extensionAttributes->expects($this->once())
90+
->method('__call')
91+
->with(
92+
$this->equalTo('getDesignConfigData')
93+
)->willReturn([$designConfigData]);
94+
$designConfigData->expects($this->exactly(2))
95+
->method('getFieldConfig')
96+
->willReturn(['path' => $path]);
97+
98+
$result = $this->pathValidator->validate($path);
99+
$this->assertTrue($result);
100+
}
101+
102+
public function testValidateDesignPathThrowsException()
103+
{
104+
$this->expectException(ValidatorException::class);
105+
$this->expectExceptionMessage('The "design/invalid_path" path doesn\'t exist. Verify and try again.');
106+
107+
$path = 'design/invalid_path';
108+
$element = $this->createMock(Structure\Element\Field::class);
109+
$designConfig = $this->createMock(DesignConfigInterface::class);
110+
$extensionAttributes = $this->createMock(DataObject::class);
111+
112+
$element->expects($this->exactly(2))
113+
->method('getConfigPath')
114+
->willReturn($path);
115+
$this->structure->expects($this->once())
116+
->method('getElementByConfigPath')
117+
->with($path)
118+
->willReturn($element);
119+
$this->structure->expects($this->once())
120+
->method('getFieldPaths')
121+
->willReturn([]);
122+
$this->designConfigRepository->expects($this->once())
123+
->method('getByScope')
124+
->with('default', null)
125+
->willReturn($designConfig);
126+
$designConfig->expects($this->once())
127+
->method('getExtensionAttributes')
128+
->willReturn($extensionAttributes);
129+
$extensionAttributes->expects($this->once())
130+
->method('__call')
131+
->with(
132+
$this->equalTo('getDesignConfigData')
133+
)->willReturn([]);
134+
135+
$this->pathValidator->validate($path);
136+
}
137+
}

0 commit comments

Comments
 (0)