Skip to content

Commit ae96ab2

Browse files
committed
MC-40078: Create automated test for: "Create attribute without attribute code"
1 parent 24cb7ac commit ae96ab2

File tree

1 file changed

+145
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)