Skip to content

Commit 5512626

Browse files
Ensure that url suffix resolvers return strings to match function declaration
1 parent a7bde28 commit 5512626

File tree

4 files changed

+330
-2
lines changed

4 files changed

+330
-2
lines changed

app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/CategoryUrlSuffix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function getCategoryUrlSuffix(int $storeId): string
7575
self::$xml_path_category_url_suffix,
7676
ScopeInterface::SCOPE_STORE,
7777
$storeId
78-
);
78+
) ?? '';
7979
}
8080
return $this->categoryUrlSuffix[$storeId];
8181
}

app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/ProductUrlSuffix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function getProductUrlSuffix(int $storeId): string
7575
self::$xml_path_product_url_suffix,
7676
ScopeInterface::SCOPE_STORE,
7777
$storeId
78-
);
78+
) ?? '';
7979
}
8080
return $this->productUrlSuffix[$storeId];
8181
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
namespace Magento\CatalogUrlRewriteGraphQl\Test\Unit\Model\Resolver;
3+
4+
use Magento\CatalogUrlRewriteGraphQl\Model\Resolver\CategoryUrlSuffix;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
7+
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
8+
use Magento\GraphQl\Model\Query\ContextInterface;
9+
use Magento\Store\Api\Data\StoreInterface;
10+
use PHPUnit\Framework\MockObject\MockObject;
11+
use PHPUnit\Framework\TestCase;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
14+
class CategoryUrlSuffixTest extends TestCase
15+
{
16+
17+
/**
18+
* @var ScopeConfigInterface|MockObject
19+
*/
20+
private $scopeConfigMock;
21+
22+
/**
23+
* @var ContextInterface|MockObject
24+
*/
25+
private $contextMock;
26+
27+
/**
28+
* @var ContextExtensionInterface|MockObject
29+
*/
30+
private $contextExtensionMock;
31+
32+
/**
33+
* @var StoreInterface|MockObject
34+
*/
35+
private $storeMock;
36+
37+
/**
38+
* @var Field|MockObject
39+
*/
40+
private $fieldMock;
41+
42+
/**
43+
* @var ResolveInfo|MockObject
44+
*/
45+
private $resolveInfoMock;
46+
47+
/**
48+
* @var CategoryUrlSuffix
49+
*/
50+
private $resolver;
51+
52+
53+
protected function setUp(): void
54+
{
55+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
56+
->disableOriginalConstructor()
57+
->setMethods(
58+
[
59+
'getExtensionAttributes'
60+
]
61+
)
62+
->getMockForAbstractClass();
63+
64+
$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
65+
->setMethods(
66+
[
67+
'getStore'
68+
]
69+
)
70+
->getMockForAbstractClass();
71+
72+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
73+
->setMethods(
74+
[
75+
'getId'
76+
]
77+
)
78+
->getMockForAbstractClass();
79+
80+
$this->fieldMock = $this->getMockBuilder(Field::class)
81+
->disableOriginalConstructor()
82+
->getMock();
83+
84+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
85+
->disableOriginalConstructor()
86+
->getMock();
87+
88+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
89+
->getMockForAbstractClass();
90+
91+
$this->resolver = new CategoryUrlSuffix(
92+
$this->scopeConfigMock
93+
);
94+
}
95+
96+
/**
97+
* Verify that empty string is returned when config value is null
98+
*/
99+
public function testNullValue()
100+
{
101+
$this->scopeConfigMock->expects($this->once())
102+
->method('getValue')
103+
->willReturn(null);
104+
105+
$this->contextMock
106+
->expects($this->once())
107+
->method('getExtensionAttributes')
108+
->willReturn($this->contextExtensionMock);
109+
110+
$this->contextExtensionMock
111+
->expects($this->once())
112+
->method('getStore')
113+
->willReturn($this->storeMock);
114+
115+
$this->storeMock
116+
->expects($this->once())
117+
->method('getId')
118+
->willReturn(1);
119+
120+
$this->assertEquals(
121+
'',
122+
$this->resolver->resolve(
123+
$this->fieldMock,
124+
$this->contextMock,
125+
$this->resolveInfoMock
126+
)
127+
);
128+
}
129+
130+
/**
131+
* Verify that the configured value is returned
132+
*/
133+
public function testNonNullValue()
134+
{
135+
$value = 'html';
136+
$this->scopeConfigMock->expects($this->once())
137+
->method('getValue')
138+
->willReturn($value);
139+
140+
$this->contextMock
141+
->expects($this->once())
142+
->method('getExtensionAttributes')
143+
->willReturn($this->contextExtensionMock);
144+
145+
$this->contextExtensionMock
146+
->expects($this->once())
147+
->method('getStore')
148+
->willReturn($this->storeMock);
149+
150+
$this->storeMock
151+
->expects($this->once())
152+
->method('getId')
153+
->willReturn(1);
154+
155+
$this->assertEquals(
156+
$value,
157+
$this->resolver->resolve(
158+
$this->fieldMock,
159+
$this->contextMock,
160+
$this->resolveInfoMock
161+
)
162+
);
163+
}
164+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
namespace Magento\CatalogUrlRewriteGraphQl\Test\Unit\Model\Resolver;
3+
4+
use Magento\CatalogUrlRewriteGraphQl\Model\Resolver\ProductUrlSuffix;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
7+
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
8+
use Magento\GraphQl\Model\Query\ContextInterface;
9+
use Magento\Store\Api\Data\StoreInterface;
10+
use PHPUnit\Framework\MockObject\MockObject;
11+
use PHPUnit\Framework\TestCase;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
14+
class ProductUrlSuffixTest extends TestCase
15+
{
16+
17+
/**
18+
* @var ScopeConfigInterface|MockObject
19+
*/
20+
private $scopeConfigMock;
21+
22+
/**
23+
* @var ContextInterface|MockObject
24+
*/
25+
private $contextMock;
26+
27+
/**
28+
* @var ContextExtensionInterface|MockObject
29+
*/
30+
private $contextExtensionMock;
31+
32+
/**
33+
* @var StoreInterface|MockObject
34+
*/
35+
private $storeMock;
36+
37+
/**
38+
* @var Field|MockObject
39+
*/
40+
private $fieldMock;
41+
42+
/**
43+
* @var ResolveInfo|MockObject
44+
*/
45+
private $resolveInfoMock;
46+
47+
/**
48+
* @var ProductUrlSuffix
49+
*/
50+
private $resolver;
51+
52+
53+
protected function setUp(): void
54+
{
55+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
56+
->disableOriginalConstructor()
57+
->setMethods(
58+
[
59+
'getExtensionAttributes'
60+
]
61+
)
62+
->getMockForAbstractClass();
63+
64+
$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
65+
->setMethods(
66+
[
67+
'getStore'
68+
]
69+
)
70+
->getMockForAbstractClass();
71+
72+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
73+
->setMethods(
74+
[
75+
'getId'
76+
]
77+
)
78+
->getMockForAbstractClass();
79+
80+
$this->fieldMock = $this->getMockBuilder(Field::class)
81+
->disableOriginalConstructor()
82+
->getMock();
83+
84+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
85+
->disableOriginalConstructor()
86+
->getMock();
87+
88+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
89+
->getMockForAbstractClass();
90+
91+
$this->resolver = new ProductUrlSuffix(
92+
$this->scopeConfigMock
93+
);
94+
}
95+
96+
/**
97+
* Verify that empty string is returned when config value is null
98+
*/
99+
public function testNullValue()
100+
{
101+
$this->scopeConfigMock->expects($this->once())
102+
->method('getValue')
103+
->willReturn(null);
104+
105+
$this->contextMock
106+
->expects($this->once())
107+
->method('getExtensionAttributes')
108+
->willReturn($this->contextExtensionMock);
109+
110+
$this->contextExtensionMock
111+
->expects($this->once())
112+
->method('getStore')
113+
->willReturn($this->storeMock);
114+
115+
$this->storeMock
116+
->expects($this->once())
117+
->method('getId')
118+
->willReturn(1);
119+
120+
$this->assertEquals(
121+
'',
122+
$this->resolver->resolve(
123+
$this->fieldMock,
124+
$this->contextMock,
125+
$this->resolveInfoMock
126+
)
127+
);
128+
}
129+
130+
/**
131+
* Verify that the configured value is returned
132+
*/
133+
public function testNonNullValue()
134+
{
135+
$value = 'html';
136+
$this->scopeConfigMock->expects($this->once())
137+
->method('getValue')
138+
->willReturn($value);
139+
140+
$this->contextMock
141+
->expects($this->once())
142+
->method('getExtensionAttributes')
143+
->willReturn($this->contextExtensionMock);
144+
145+
$this->contextExtensionMock
146+
->expects($this->once())
147+
->method('getStore')
148+
->willReturn($this->storeMock);
149+
150+
$this->storeMock
151+
->expects($this->once())
152+
->method('getId')
153+
->willReturn(1);
154+
155+
$this->assertEquals(
156+
$value,
157+
$this->resolver->resolve(
158+
$this->fieldMock,
159+
$this->contextMock,
160+
$this->resolveInfoMock
161+
)
162+
);
163+
}
164+
}

0 commit comments

Comments
 (0)