|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Eav\Attribute; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Deploy\Model\Mode; |
|
6
|
|
|
use Magento\Eav\Model\Config as EavConfig; |
|
7
|
|
|
use Magento\Eav\Setup\EavSetup; |
|
8
|
|
|
use Magento\Eav\Setup\EavSetupFactory; |
|
9
|
|
|
use Magento\Framework\App\State; |
|
10
|
|
|
use N98\Magento\Command\TestCase; |
|
11
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
12
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
13
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
14
|
|
|
|
|
15
|
|
|
class RemoveCommandTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var RemoveCommand; |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $command; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CommandTester |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $commandTester; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
if ($this->runsInProductionMode()) { |
|
33
|
|
|
$this->markTestSkipped('This command is not available in production mode'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$application = $this->getApplication(); |
|
37
|
|
|
$application->add(new RemoveCommand()); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$this->command = $this->getApplication()->find('eav:attribute:remove'); |
|
|
|
|
|
|
40
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
private function runsInProductionMode() |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$objectManager = $this->getApplication()->getObjectManager(); |
|
|
|
|
|
|
49
|
|
|
$mode = $objectManager->create( |
|
50
|
|
|
Mode::class, |
|
51
|
|
|
[ |
|
52
|
|
|
'input' => new ArgvInput(), |
|
53
|
|
|
'output' => new ConsoleOutput(), |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return $mode->getMode() === State::MODE_PRODUCTION; |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $entityType |
|
61
|
|
|
* @param string $attributeCode |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function attributeExists($entityType, $attributeCode) |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var EavConfig $eavConfig */ |
|
67
|
|
|
$eavConfig = $this->getApplication()->getObjectManager()->create(EavConfig::class); |
|
|
|
|
|
|
68
|
|
|
$existingAttributeCodes = $eavConfig->getEntityAttributeCodes($entityType); |
|
69
|
|
|
return in_array($attributeCode, $existingAttributeCodes); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testThrowsExceptionOnNonExistingEntityType() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->setExpectedException( |
|
78
|
|
|
'\InvalidArgumentException', |
|
79
|
|
|
'Invalid entity_type specified: non_existing_entity_type' |
|
80
|
|
|
); |
|
81
|
|
|
$this->commandTester->execute([ |
|
82
|
|
|
'command' => $this->command->getName(), |
|
83
|
|
|
'entityType' => 'non_existing_entity_type', |
|
84
|
|
|
'attributeCode' => ['whatever'], |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testDetectsNonExistingAttributeCode() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->assertDisplayContains( |
|
94
|
|
|
[ |
|
95
|
|
|
'command' => $this->command->getName(), |
|
96
|
|
|
'entityType' => 'customer', |
|
97
|
|
|
'attributeCode' => ['non-existing-attribute-code'], |
|
98
|
|
|
], |
|
99
|
|
|
'Attribute "non-existing-attribute-code" does not exist for entity type "customer", skipped' |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testRemovesAttributeSuccessfully() |
|
107
|
|
|
{ |
|
108
|
|
|
$entityType = 'customer'; |
|
109
|
|
|
$attributeCode = 'temporary_attribute'; |
|
110
|
|
|
|
|
111
|
|
|
/** @var EavSetup $eavSetup */ |
|
112
|
|
|
$eavSetup = $this->getApplication()->getObjectManager() |
|
|
|
|
|
|
113
|
|
|
->get(EavSetupFactory::class) |
|
114
|
|
|
->create(); |
|
115
|
|
|
|
|
116
|
|
|
$eavSetup->addAttribute( |
|
117
|
|
|
$entityType, |
|
118
|
|
|
$attributeCode, |
|
119
|
|
|
[ |
|
120
|
|
|
'type' => 'text', |
|
121
|
|
|
'input' => 'text', |
|
122
|
|
|
'label' => 'Temporary Attribute', |
|
123
|
|
|
] |
|
124
|
|
|
); |
|
125
|
|
|
$eavSetup->cleanCache(); |
|
126
|
|
|
$this->assertTrue($this->attributeExists($entityType, $attributeCode)); |
|
127
|
|
|
|
|
128
|
|
|
$this->commandTester->execute([ |
|
129
|
|
|
'command' => $this->command->getName(), |
|
130
|
|
|
'entityType' => $entityType, |
|
131
|
|
|
'attributeCode' => [$attributeCode], |
|
132
|
|
|
]); |
|
133
|
|
|
$eavSetup->cleanCache(); |
|
134
|
|
|
$this->assertFalse($this->attributeExists($entityType, $attributeCode)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: