Skip to content

Commit 0bf36b8

Browse files
committed
Merge branch 'MC-40078' into 2.4-develop-sidecar-pr9
2 parents 1c3837c + fe4b27b commit 0bf36b8

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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\Catalog\Model\Product\Attribute;
9+
10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
11+
use Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory;
12+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
use Magento\Catalog\Setup\CategorySetup;
14+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
15+
use Magento\Framework\Exception\InputException;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Checks product attribute save behaviour.
22+
*
23+
* @see \Magento\Catalog\Model\Product\Attribute\Repository
24+
*
25+
* @magentoDbIsolation enabled
26+
*/
27+
class RepositoryTest extends TestCase
28+
{
29+
/** @var ObjectManagerInterface */
30+
private $objectManager;
31+
32+
/** @var ProductAttributeRepositoryInterface */
33+
private $repository;
34+
35+
/** @var ProductAttributeInterfaceFactory */
36+
private $attributeFactory;
37+
38+
/** @var ProductAttributeInterface */
39+
private $createdAttribute;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp(): void
45+
{
46+
parent::setUp();
47+
48+
$this->objectManager = Bootstrap::getObjectManager();
49+
$this->repository = $this->objectManager->get(ProductAttributeRepositoryInterface::class);
50+
$this->attributeFactory = $this->objectManager->get(ProductAttributeInterfaceFactory::class);
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function tearDown(): void
57+
{
58+
if ($this->createdAttribute instanceof ProductAttributeInterface) {
59+
$this->repository->delete($this->createdAttribute);
60+
}
61+
62+
parent::tearDown();
63+
}
64+
65+
/**
66+
* @return void
67+
*/
68+
public function testSaveWithoutAttributeCode(): void
69+
{
70+
$this->createdAttribute = $this->saveAttributeWithData(
71+
$this->hydrateData(['frontend_label' => 'Boolean Attribute'])
72+
);
73+
$this->assertEquals('boolean_attribute', $this->createdAttribute->getAttributeCode());
74+
}
75+
76+
/**
77+
* @return void
78+
*/
79+
public function testSaveWithoutAttributeAndInvalidLabelCode(): void
80+
{
81+
$this->createdAttribute = $this->saveAttributeWithData($this->hydrateData(['frontend_label' => '/$&!/']));
82+
$this->assertStringStartsWith('attr_', $this->createdAttribute->getAttributeCode());
83+
}
84+
85+
/**
86+
* @dataProvider errorProvider
87+
*
88+
* @param string $fieldName
89+
* @param string $fieldValue
90+
* @return void
91+
*/
92+
public function testSaveWithInvalidCode(string $fieldName, string $fieldValue): void
93+
{
94+
$this->expectExceptionObject(InputException::invalidFieldValue($fieldName, $fieldValue));
95+
$this->createdAttribute = $this->saveAttributeWithData($this->hydrateData([$fieldName => $fieldValue]));
96+
}
97+
98+
/**
99+
* @return array
100+
*/
101+
public function errorProvider(): array
102+
{
103+
return [
104+
'with_invalid_attribute_code' => [
105+
'field_name' => 'attribute_code',
106+
'field_value' => '****',
107+
],
108+
'with_invalid_frontend_input' => [
109+
'field_name' => 'frontend_input',
110+
'field_value' => 'invalid_input',
111+
],
112+
];
113+
}
114+
115+
/**
116+
* Save product attribute with data
117+
*
118+
* @param array $data
119+
* @return ProductAttributeInterface
120+
*/
121+
private function saveAttributeWithData(array $data): ProductAttributeInterface
122+
{
123+
$attribute = $this->attributeFactory->create();
124+
$attribute->addData($data);
125+
126+
return $this->repository->save($attribute);
127+
}
128+
129+
/**
130+
* Hydrate data
131+
*
132+
* @param array $data
133+
* @return array
134+
*/
135+
private function hydrateData(array $data): array
136+
{
137+
$defaultData = [
138+
'entity_type_id' => CategorySetup::CATALOG_PRODUCT_ENTITY_TYPE_ID,
139+
'is_global' => ScopedAttributeInterface::SCOPE_GLOBAL,
140+
'frontend_input' => 'boolean',
141+
'frontend_label' => 'default label',
142+
];
143+
144+
return array_merge($defaultData, $data);
145+
}
146+
}

0 commit comments

Comments
 (0)