Completed
Push — master ( c740cf...e24aa2 )
by Tim
13s
created

CatalogAttributeUpdateObserverTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\CatalogAttributeUpdateObserverTest
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 update 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 CatalogAttributeUpdateObserverTest extends \PHPUnit_Framework_TestCase
37
{
38
39
    /**
40
     * The observer we want to test.
41
     *
42
     * @var \TechDivision\Import\Attribute\Observers\CatalogAttributeUpdateObserver
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 CatalogAttributeUpdateObserver($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 existing entity
159
        $existingEntity = array(
160
            MemberNames::ATTRIBUTE_ID                  => $lastAttributeId,
161
            MemberNames::FRONTEND_INPUT_RENDERER       => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Category',
162
            MemberNames::IS_GLOBAL                     => 0,
163
            MemberNames::IS_VISIBLE                    => 0,
164
            MemberNames::IS_SEARCHABLE                 => 1,
165
            MemberNames::IS_FILTERABLE                 => 1,
166
            MemberNames::IS_COMPARABLE                 => 1,
167
            MemberNames::IS_VISIBLE_ON_FRONT           => 1,
168
            MemberNames::IS_HTML_ALLOWED_ON_FRONT      => 1,
169
            MemberNames::IS_USED_FOR_PRICE_RULES       => 1,
170
            MemberNames::IS_FILTERABLE_IN_SEARCH       => 1,
171
            MemberNames::USED_IN_PRODUCT_LISTING       => 1,
172
            MemberNames::USED_FOR_SORT_BY              => 1,
173
            MemberNames::APPLY_TO                      => 'simple,virtual',
174
            MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => 1,
175
            MemberNames::POSITION                      => 1,
176
            MemberNames::IS_WYSIWYG_ENABLED            => 1,
177
            MemberNames::IS_USED_FOR_PROMO_RULES       => 1,
178
            MemberNames::IS_REQUIRED_IN_ADMIN_STORE    => 1,
179
            MemberNames::IS_USED_IN_GRID               => 1,
180
            MemberNames::IS_VISIBLE_IN_GRID            => 1,
181
            MemberNames::IS_FILTERABLE_IN_GRID         => 1,
182
            MemberNames::SEARCH_WEIGHT                 => 0,
183
            MemberNames::ADDITIONAL_DATA               => serialize(array()),
184
            EntityStatus::MEMBER_NAME                  => EntityStatus::STATUS_UPDATE
185
        );
186
187
        // as NO values has been passed, the expected entity EQUALS the existing entity
188
        $expectedEntity = $existingEntity;
189
190
        // mock the method that loads the existing entity
191
        $this->mockBunchProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...unchProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
192
                                 ->method('loadCatalogAttribute')
193
                                 ->with($lastAttributeId)
194
                                 ->willReturn($existingEntity);
195
        // mock the method that persists the entity
196
        $this->mockBunchProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...unchProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197
                                 ->method('persistCatalogAttribute')
198
                                 ->with($expectedEntity)
199
                                 ->willReturn(null);
200
201
        // invoke the handle method
202
        $this->assertSame($row, $this->observer->handle($mockSubject));
203
    }
204
205
    /**
206
     * Test's the handle() method successfull.
207
     *
208
     * @return void
209
     */
210
    public function testHandleWithAllFields()
211
    {
212
213
        // create a dummy CSV file row
214
        $row = array(
215
            0  => $attributeCode = 'test_attribute_code',
216
            1  => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Product',
217
            2  => 1,
218
            3  => 1,
219
            4  => 0,
220
            5  => 0,
221
            6  => 0,
222
            7  => 0,
223
            8  => 0,
224
            9  => 0,
225
            10 => 0,
226
            11 => 0,
227
            12 => 0,
228
            13 => 'virtual',
229
            14 => 0,
230
            15 => 0,
231
            16 => 0,
232
            17 => 0,
233
            18 => 0,
234
            19 => 0,
235
            20 => 0,
236
            21 => 0,
237
            22 => 1,
238
            23 => $additionaData = 'swatch_input_type=visual,update_product_preview_image=0,use_product_image_for_swatch=1'
239
        );
240
241
        // prepare the exploded additional data
242
        $explodedAdditionalData = array();
243
        foreach (explode(',', $additionaData) as $data) {
244
            list ($key, $value) = explode('=', $data);
245
            $explodedAdditionalData[$key] = $value;
246
        }
247
248
        // create a mock subject instance
249
        $mockSubject = $this->getMockBuilder('TechDivision\Import\Attribute\Observers\AttributeSubjectImpl')
250
                            ->setMethods(get_class_methods('TechDivision\Import\Attribute\Observers\AttributeSubjectImpl'))
251
                            ->getMock();
252
        $mockSubject->expects($this->once())
253
                    ->method('getRow')
254
                    ->willReturn($row);
255
        $mockSubject->expects($this->exactly(69))
256
                    ->method('hasHeader')
257
                    ->willReturn(true);
258
        $mockSubject->expects($this->exactly(46))
259
                    ->method('getHeader')
260
                    ->withConsecutive(
261
                        array(ColumnKeys::ATTRIBUTE_CODE),
262
                        array(ColumnKeys::FRONTEND_INPUT_RENDERER),
263
                        array(ColumnKeys::FRONTEND_INPUT_RENDERER),
264
                        array(ColumnKeys::IS_GLOBAL),
265
                        array(ColumnKeys::IS_GLOBAL),
266
                        array(ColumnKeys::IS_VISIBLE),
267
                        array(ColumnKeys::IS_VISIBLE),
268
                        array(ColumnKeys::IS_SEARCHABLE),
269
                        array(ColumnKeys::IS_SEARCHABLE),
270
                        array(ColumnKeys::IS_FILTERABLE),
271
                        array(ColumnKeys::IS_FILTERABLE),
272
                        array(ColumnKeys::IS_COMPARABLE),
273
                        array(ColumnKeys::IS_COMPARABLE),
274
                        array(ColumnKeys::IS_VISIBLE_ON_FRONT),
275
                        array(ColumnKeys::IS_VISIBLE_ON_FRONT),
276
                        array(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT),
277
                        array(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT),
278
                        array(ColumnKeys::IS_USED_FOR_PRICE_RULES),
279
                        array(ColumnKeys::IS_USED_FOR_PRICE_RULES),
280
                        array(ColumnKeys::IS_FILTERABLE_IN_SEARCH),
281
                        array(ColumnKeys::IS_FILTERABLE_IN_SEARCH),
282
                        array(ColumnKeys::USED_IN_PRODUCT_LISTING),
283
                        array(ColumnKeys::USED_IN_PRODUCT_LISTING),
284
                        array(ColumnKeys::USED_FOR_SORT_BY),
285
                        array(ColumnKeys::USED_FOR_SORT_BY),
286
                        array(ColumnKeys::APPLY_TO),
287
                        array(ColumnKeys::APPLY_TO),
288
                        array(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH),
289
                        array(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH),
290
                        array(ColumnKeys::POSITION),
291
                        array(ColumnKeys::POSITION),
292
                        array(ColumnKeys::IS_WYSIWYG_ENABLED),
293
                        array(ColumnKeys::IS_WYSIWYG_ENABLED),
294
                        array(ColumnKeys::IS_USED_FOR_PROMO_RULES),
295
                        array(ColumnKeys::IS_USED_FOR_PROMO_RULES),
296
                        array(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE),
297
                        array(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE),
298
                        array(ColumnKeys::IS_USED_IN_GRID),
299
                        array(ColumnKeys::IS_USED_IN_GRID),
300
                        array(ColumnKeys::IS_VISIBLE_IN_GRID),
301
                        array(ColumnKeys::IS_VISIBLE_IN_GRID),
302
                        array(ColumnKeys::IS_FILTERABLE_IN_GRID),
303
                        array(ColumnKeys::IS_FILTERABLE_IN_GRID),
304
                        array(ColumnKeys::SEARCH_WEIGHT),
305
                        array(ColumnKeys::SEARCH_WEIGHT),
306
                        array(ColumnKeys::ADDITIONAL_DATA),
307
                        array(ColumnKeys::ADDITIONAL_DATA)
308
                     )
309
                    ->willReturnOnConsecutiveCalls(
310
                        0,
311
                        1,
312
                        1,
313
                        2,
314
                        2,
315
                        3,
316
                        3,
317
                        4,
318
                        4,
319
                        5,
320
                        5,
321
                        6,
322
                        6,
323
                        7,
324
                        7,
325
                        8,
326
                        8,
327
                        9,
328
                        9,
329
                        10,
330
                        10,
331
                        11,
332
                        11,
333
                        12,
334
                        12,
335
                        13,
336
                        13,
337
                        14,
338
                        14,
339
                        15,
340
                        15,
341
                        16,
342
                        16,
343
                        17,
344
                        17,
345
                        18,
346
                        18,
347
                        19,
348
                        19,
349
                        20,
350
                        20,
351
                        21,
352
                        21,
353
                        22,
354
                        22,
355
                        23,
356
                        23
357
                     );
358
        $mockSubject->expects($this->once())
359
                    ->method('hasBeenProcessed')
360
                    ->with($attributeCode)
361
                    ->willReturn(false);
362
        $mockSubject->expects($this->exactly(4))
363
                    ->method('explode')
364
                    ->withConsecutive(
365
                        array($additionaData),
366
                        array('swatch_input_type=visual', '='),
367
                        array('update_product_preview_image=0', '='),
368
                        array('use_product_image_for_swatch=1', '=')
369
                    )
370
                    ->willReturnOnConsecutiveCalls(
371
                        array('swatch_input_type=visual', 'update_product_preview_image=0', 'use_product_image_for_swatch=1'),
372
                        array('swatch_input_type', 'visual'),
373
                        array('update_product_preview_image', '0'),
374
                        array('use_product_image_for_swatch', '1')
375
                    );
376
        $mockSubject->expects($this->once())
377
                    ->method('getLastAttributeId')
378
                    ->willReturn($lastAttributeId = 1001);
379
380
        // initialize the existing entity
381
        $existingEntity = array(
382
            MemberNames::ATTRIBUTE_ID                  => $lastAttributeId,
383
            MemberNames::FRONTEND_INPUT_RENDERER       => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Category',
384
            MemberNames::IS_GLOBAL                     => 0,
385
            MemberNames::IS_VISIBLE                    => 0,
386
            MemberNames::IS_SEARCHABLE                 => 1,
387
            MemberNames::IS_FILTERABLE                 => 1,
388
            MemberNames::IS_COMPARABLE                 => 1,
389
            MemberNames::IS_VISIBLE_ON_FRONT           => 1,
390
            MemberNames::IS_HTML_ALLOWED_ON_FRONT      => 1,
391
            MemberNames::IS_USED_FOR_PRICE_RULES       => 1,
392
            MemberNames::IS_FILTERABLE_IN_SEARCH       => 1,
393
            MemberNames::USED_IN_PRODUCT_LISTING       => 1,
394
            MemberNames::USED_FOR_SORT_BY              => 1,
395
            MemberNames::APPLY_TO                      => 'simple,virtual',
396
            MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => 1,
397
            MemberNames::POSITION                      => 1,
398
            MemberNames::IS_WYSIWYG_ENABLED            => 1,
399
            MemberNames::IS_USED_FOR_PROMO_RULES       => 1,
400
            MemberNames::IS_REQUIRED_IN_ADMIN_STORE    => 1,
401
            MemberNames::IS_USED_IN_GRID               => 1,
402
            MemberNames::IS_VISIBLE_IN_GRID            => 1,
403
            MemberNames::IS_FILTERABLE_IN_GRID         => 1,
404
            MemberNames::SEARCH_WEIGHT                 => 0,
405
            MemberNames::ADDITIONAL_DATA               => serialize(array()),
406
            EntityStatus::MEMBER_NAME                  => EntityStatus::STATUS_UPDATE
407
        );
408
409
        // initialize the expected entity
410
        $expectedEntity = array(
411
            MemberNames::ATTRIBUTE_ID                  => $lastAttributeId,
412
            MemberNames::FRONTEND_INPUT_RENDERER       => 'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Product',
413
            MemberNames::IS_GLOBAL                     => 1,
414
            MemberNames::IS_VISIBLE                    => 1,
415
            MemberNames::IS_SEARCHABLE                 => 0,
416
            MemberNames::IS_FILTERABLE                 => 0,
417
            MemberNames::IS_COMPARABLE                 => 0,
418
            MemberNames::IS_VISIBLE_ON_FRONT           => 0,
419
            MemberNames::IS_HTML_ALLOWED_ON_FRONT      => 0,
420
            MemberNames::IS_USED_FOR_PRICE_RULES       => 0,
421
            MemberNames::IS_FILTERABLE_IN_SEARCH       => 0,
422
            MemberNames::USED_IN_PRODUCT_LISTING       => 0,
423
            MemberNames::USED_FOR_SORT_BY              => 0,
424
            MemberNames::APPLY_TO                      => 'virtual',
425
            MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => 0,
426
            MemberNames::POSITION                      => 0,
427
            MemberNames::IS_WYSIWYG_ENABLED            => 0,
428
            MemberNames::IS_USED_FOR_PROMO_RULES       => 0,
429
            MemberNames::IS_REQUIRED_IN_ADMIN_STORE    => 0,
430
            MemberNames::IS_USED_IN_GRID               => 0,
431
            MemberNames::IS_VISIBLE_IN_GRID            => 0,
432
            MemberNames::IS_FILTERABLE_IN_GRID         => 0,
433
            MemberNames::SEARCH_WEIGHT                 => 1,
434
            MemberNames::ADDITIONAL_DATA               => serialize($explodedAdditionalData),
435
            EntityStatus::MEMBER_NAME                  => EntityStatus::STATUS_UPDATE
436
        );
437
438
        // mock the method that loads the existing entity
439
        $this->mockBunchProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...unchProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
440
                                 ->method('loadCatalogAttribute')
441
                                 ->with($lastAttributeId)
442
                                 ->willReturn($existingEntity);
443
        // mock the method that persists the entity
444
        $this->mockBunchProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...unchProcessorInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
445
                                 ->method('persistCatalogAttribute')
446
                                 ->with($expectedEntity)
447
                                 ->willReturn(null);
448
449
        // invoke the handle method
450
        $this->assertSame($row, $this->observer->handle($mockSubject));
451
    }
452
}
453