Skip to content

Commit 800e1c9

Browse files
Created test for generator
1 parent e571e6c commit 800e1c9

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Foo\Bar\Setup\Patch\Data;
4+
5+
use Magento\Eav\Setup\EavSetup;
6+
use Magento\Eav\Setup\EavSetupFactory;
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Magento\Framework\Setup\Patch\DataPatchInterface;
9+
10+
class AddTestAttribute implements DataPatchInterface
11+
{
12+
13+
/**
14+
* @var ModuleDataSetupInterface
15+
*/
16+
private $moduleDataSetup;
17+
18+
/**
19+
* @var EavSetupFactory
20+
*/
21+
private $eavSetupFactory;
22+
23+
/**
24+
* AddRecommendedAttribute constructor.
25+
*
26+
* @param ModuleDataSetupInterface $moduleDataSetup
27+
* @param EavSetupFactory $eavSetupFactory
28+
*/
29+
public function __construct(
30+
ModuleDataSetupInterface $moduleDataSetup,
31+
EavSetupFactory $eavSetupFactory
32+
)
33+
{
34+
$this->moduleDataSetup = $moduleDataSetup;
35+
$this->eavSetupFactory = $eavSetupFactory;
36+
}
37+
38+
/**
39+
* Get array of patches that have to be executed prior to this.
40+
*
41+
* Example of implementation:
42+
*
43+
* [
44+
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
45+
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
46+
* ]
47+
*
48+
* @return string[]
49+
*/
50+
public static function getDependencies()
51+
{
52+
return [];
53+
}
54+
55+
/**
56+
* Get aliases (previous names) for the patch.
57+
*
58+
* @return string[]
59+
*/
60+
public function getAliases()
61+
{
62+
return [];
63+
}
64+
65+
/**
66+
* Run code inside patch
67+
* If code fails, patch must be reverted, in case when we are speaking about schema - then under revert
68+
* means run PatchInterface::revert()
69+
*
70+
* If we speak about data, under revert means: $transaction->rollback()
71+
*
72+
* @return $this
73+
*/
74+
public function apply()
75+
{
76+
/** @var EavSetup $eavSetup */
77+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
78+
79+
$eavSetup->addAttribute(
80+
\Magento\Catalog\Model\Product::ENTITY,
81+
'test',
82+
[
83+
'is_visible_in_grid' => false,
84+
'is_html_allowed_on_front' => false,
85+
'visible_on_front' => false,
86+
'visible' => true,
87+
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
88+
'label' => 'Test Label',
89+
'source' => null,
90+
'type' => 'static',
91+
'is_used_in_grid' => false,
92+
'required' => false,
93+
'input' => 'boolean',
94+
'is_filterable_in_grid' => false,
95+
'sort_order' => 10,
96+
'group' => 'General',
97+
]
98+
);
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.actions.generation.generator;
6+
7+
import com.intellij.openapi.project.Project;
8+
import com.intellij.psi.PsiFile;
9+
import com.magento.idea.magento2plugin.actions.generation.data.ProductEntityData;
10+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeScopes;
11+
12+
public class EavAttributeSetupPatchGeneratorTest extends BaseGeneratorTestCase {
13+
14+
/**
15+
* Test Data patch for product's eav attribute generator.
16+
*/
17+
public void testGenerateFile() {
18+
final Project project = myFixture.getProject();
19+
20+
final ProductEntityData productEntityData = new ProductEntityData();
21+
productEntityData.setCode("test");
22+
productEntityData.setVisibleInGrid(false);
23+
productEntityData.setHtmlAllowedOnFront(false);
24+
productEntityData.setVisibleOnFront(false);
25+
productEntityData.setVisible(true);
26+
productEntityData.setScope(AttributeScopes.GLOBAL.getScope());
27+
productEntityData.setLabel("Test Label");
28+
productEntityData.setType("static");
29+
productEntityData.setUsedInGrid(false);
30+
productEntityData.setRequired(false);
31+
productEntityData.setInput("boolean");
32+
productEntityData.setFilterableInGrid(false);
33+
productEntityData.setSortOrder(10);
34+
productEntityData.setGroup("General");
35+
36+
productEntityData.setDataPatchName("AddTestAttribute");
37+
productEntityData.setNamespace("Foo\\Bar\\Setup\\Patch\\Data");
38+
productEntityData.setModuleName("Foo_Bar");
39+
40+
final EavAttributeSetupPatchGenerator setupPatchGenerator =
41+
new EavAttributeSetupPatchGenerator(productEntityData, project);
42+
final PsiFile dataPatchFile = setupPatchGenerator.generate("test");
43+
44+
final String filePatch = this.getFixturePath("AddTestAttribute.php");
45+
final PsiFile expectedFile = myFixture.configureByFile(filePatch);
46+
47+
assertGeneratedFileIsCorrect(expectedFile, "src/app/code/Foo/Bar/Setup/Patch/Data", dataPatchFile);
48+
}
49+
}

0 commit comments

Comments
 (0)