1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Observers\CatalogAttributeObserver |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-attribute |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Attribute\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Attribute\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Attribute\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Utils\EntityStatus; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test class for the catalog attribute observer implementation. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/techdivision/import-attribute |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
class CatalogAttributeObserverTest extends \PHPUnit_Framework_TestCase |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The observer we want to test. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\Attribute\Observers\CatalogAttributeObserver |
43
|
|
|
*/ |
44
|
|
|
protected $observer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The mock bunch processor instance. |
48
|
|
|
* |
49
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
50
|
|
|
*/ |
51
|
|
|
protected $mockBunchProcessor; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets up the fixture, for example, open a network connection. |
55
|
|
|
* This method is called before a test is executed. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
* @see \PHPUnit_Framework_TestCase::setUp() |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
// mock the attribute bunch processor |
64
|
|
|
$this->mockBunchProcessor = $this->getMockBuilder('TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface') |
65
|
|
|
->setMethods(get_class_methods('TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface')) |
66
|
|
|
->getMock(); |
67
|
|
|
|
68
|
|
|
// the observer instance we want to test |
69
|
|
|
$this->observer = new CatalogAttributeObserver($this->mockBunchProcessor); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Test's the handle() method successfull. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function testHandleWithoutAnyFields() |
78
|
|
|
{ |
79
|
|
|
|
80
|
|
|
// create a dummy CSV file row |
81
|
|
|
$row = array( |
82
|
|
|
0 => $attributeCode = 'test_attribute_code' |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
// create a mock subject instance |
86
|
|
|
$mockSubject = $this->getMockBuilder('TechDivision\Import\Attribute\Observers\AttributeSubjectImpl') |
87
|
|
|
->setMethods(get_class_methods('TechDivision\Import\Attribute\Observers\AttributeSubjectImpl')) |
88
|
|
|
->getMock(); |
89
|
|
|
$mockSubject->expects($this->once()) |
90
|
|
|
->method('getRow') |
91
|
|
|
->willReturn($row); |
92
|
|
|
$mockSubject->expects($this->exactly(24)) |
93
|
|
|
->method('hasHeader') |
94
|
|
|
->withConsecutive( |
95
|
|
|
array(ColumnKeys::ATTRIBUTE_CODE), |
96
|
|
|
array(ColumnKeys::FRONTEND_INPUT_RENDERER), |
97
|
|
|
array(ColumnKeys::IS_GLOBAL), |
98
|
|
|
array(ColumnKeys::IS_VISIBLE), |
99
|
|
|
array(ColumnKeys::IS_SEARCHABLE), |
100
|
|
|
array(ColumnKeys::IS_FILTERABLE), |
101
|
|
|
array(ColumnKeys::IS_COMPARABLE), |
102
|
|
|
array(ColumnKeys::IS_VISIBLE_ON_FRONT), |
103
|
|
|
array(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT), |
104
|
|
|
array(ColumnKeys::IS_USED_FOR_PRICE_RULES), |
105
|
|
|
array(ColumnKeys::IS_FILTERABLE_IN_SEARCH), |
106
|
|
|
array(ColumnKeys::USED_IN_PRODUCT_LISTING), |
107
|
|
|
array(ColumnKeys::USED_FOR_SORT_BY), |
108
|
|
|
array(ColumnKeys::APPLY_TO), |
109
|
|
|
array(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH), |
110
|
|
|
array(ColumnKeys::POSITION), |
111
|
|
|
array(ColumnKeys::IS_WYSIWYG_ENABLED), |
112
|
|
|
array(ColumnKeys::IS_USED_FOR_PROMO_RULES), |
113
|
|
|
array(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE), |
114
|
|
|
array(ColumnKeys::IS_USED_IN_GRID), |
115
|
|
|
array(ColumnKeys::IS_VISIBLE_IN_GRID), |
116
|
|
|
array(ColumnKeys::IS_FILTERABLE_IN_GRID), |
117
|
|
|
array(ColumnKeys::SEARCH_WEIGHT), |
118
|
|
|
array(ColumnKeys::ADDITIONAL_DATA) |
119
|
|
|
) |
120
|
|
|
->willReturnOnConsecutiveCalls( |
121
|
|
|
true, |
122
|
|
|
false, |
123
|
|
|
false, |
124
|
|
|
false, |
125
|
|
|
false, |
126
|
|
|
false, |
127
|
|
|
false, |
128
|
|
|
false, |
129
|
|
|
false, |
130
|
|
|
false, |
131
|
|
|
false, |
132
|
|
|
false, |
133
|
|
|
false, |
134
|
|
|
false, |
135
|
|
|
false, |
136
|
|
|
false, |
137
|
|
|
false, |
138
|
|
|
false, |
139
|
|
|
false, |
140
|
|
|
false, |
141
|
|
|
false, |
142
|
|
|
false, |
143
|
|
|
false, |
144
|
|
|
false |
145
|
|
|
); |
146
|
|
|
$mockSubject->expects($this->once()) |
147
|
|
|
->method('getHeader') |
148
|
|
|
->with(ColumnKeys::ATTRIBUTE_CODE) |
149
|
|
|
->willReturn(0); |
150
|
|
|
$mockSubject->expects($this->once()) |
151
|
|
|
->method('hasBeenProcessed') |
152
|
|
|
->with($attributeCode) |
153
|
|
|
->willReturn(false); |
154
|
|
|
$mockSubject->expects($this->once()) |
155
|
|
|
->method('getLastAttributeId') |
156
|
|
|
->willReturn($lastAttributeId = 1001); |
157
|
|
|
|
158
|
|
|
// initialize the expected entity that should be persisted |
159
|
|
|
$expectedEntity = array( |
160
|
|
|
MemberNames::ATTRIBUTE_ID => $lastAttributeId, |
161
|
|
|
EntityStatus::MEMBER_NAME => EntityStatus::STATUS_CREATE |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
// mock the method that persists the entity |
165
|
|
|
$this->mockBunchProcessor->expects($this->once()) |
|
|
|
|
166
|
|
|
->method('persistCatalogAttribute') |
167
|
|
|
->with($expectedEntity) |
168
|
|
|
->willReturn(null); |
169
|
|
|
|
170
|
|
|
// invoke the handle method |
171
|
|
|
$this->assertSame($row, $this->observer->handle($mockSubject)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.