Completed
Push — 24.x ( a95821...e8a4a7 )
by Tim
15s queued 10s
created

UrlRewriteObserverTest::createAndInvokeObserver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 9.232
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserverTest
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-product-url-rewrite
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\UrlRewrite\Observers;
22
23
use PHPUnit\Framework\TestCase;
24
use ReflectionClass;
25
use TechDivision\Import\Utils\EntityStatus;
26
use TechDivision\Import\Utils\StoreViewCodes;
27
use TechDivision\Import\Product\Utils\VisibilityKeys;
28
use TechDivision\Import\Product\UrlRewrite\Utils\ColumnKeys;
29
use TechDivision\Import\Product\UrlRewrite\Utils\MemberNames;
30
use TechDivision\Import\Adapter\SerializerAwareAdapterInterface;
31
use TechDivision\Import\Product\UrlRewrite\Utils\CoreConfigDataKeys;
32
use TechDivision\Import\Product\UrlRewrite\Subjects\UrlRewriteSubject;
33
34
/**
35
 * Test class for the product URL rewrite observer implementation.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2016 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import-product-url-rewrite
41
 * @link      http://www.techdivision.com
42
 */
43
class UrlRewriteObserverTest extends TestCase
44
{
45
46
    /**
47
     * The observer we want to test.
48
     *
49
     * @var \TechDivision\Import\Product\UrlRewrite\Observers\UrlRewriteObserver
50
     */
51
    protected $observer;
52
53
    /**
54
     * A mock processor instance.
55
     *
56
     * @var \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface
57
     */
58
    protected $mockProductUrlRewriteProcessor;
59
60
    /**
61
     * Sets up the fixture, for example, open a network connection.
62
     * This method is called before a test is executed.
63
     *
64
     * @return void
65
     * @see \PHPUnit\Framework\TestCase::setUp()
66
     */
67 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...
68
    {
69
70
        // initialize a mock processor instance
71
        $this->mockProductUrlRewriteProcessor = $this->getMockBuilder('TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface')
72
                                                     ->setMethods(get_class_methods('TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface'))
73
                                                     ->getMock();
74
75
        // initialize the observer
76
        $this->observer = new UrlRewriteObserver($this->mockProductUrlRewriteProcessor);
77
    }
78
79
    /**
80
     * Create's and invokes the UrlRewriteObserver instance.
81
     *
82
     * @param boolean $generateCategoryProductRewrites Whether or not the configuration to generare catagory/product URL rewrites has been activated
83
     * @param array   $productCategoryIds              The array with the category IDs the product has been related with
84
     * @param array   $category                        The array with the categories
85
     *
86
     * @return void
87
     */
88
    private function createAndInvokeObserver($generateCategoryProductRewrites = true, $productCategoryIds = [], $category = [])
89
    {
90
91
        // create a mock subject
92
        $mockSubject = $this->getMockBuilder(UrlRewriteSubject::class)
93
                            ->setMethods(array('getCoreConfigData'))
94
                            ->disableOriginalConstructor()
95
                            ->getMock();
96
97
        // mock the method to load the Magento configuration data with
98
        $mockSubject->expects($this->exactly(1))
99
                            ->method('getCoreConfigData')
100
                            ->with(CoreConfigDataKeys::CATALOG_SEO_GENERATE_CATEGORY_PRODUCT_REWRITES)
101
                            ->willReturn($generateCategoryProductRewrites);
102
103
        // mock the observer
104
        $observer = $this->getMockBuilder(UrlRewriteObserver::class)
105
            ->setConstructorArgs([$this->mockProductUrlRewriteProcessor])
106
            ->setMethods(['getGenerateCategoryProductRewritesOptionValue', 'isRootCategory', 'getSubject'])
107
            ->getMock();
108
109
        $observer->expects($this->any())
110
            ->method('getSubject')
111
            ->willReturn($mockSubject);
112
113
        $observer->expects($this->any())
114
            ->method('isRootCategory')
115
            ->willReturn(false);
116
117
        // prepare protected properties of observer
118
        $reflection = new ReflectionClass(UrlRewriteObserver::class);
119
        $property = $reflection->getProperty('productCategoryIds');
120
        $property->setAccessible(true);
121
        $property->setValue($observer, $productCategoryIds);
122
123
        // invoke the method to test
124
        $method = $reflection->getMethod('createProductCategoryRelation');
125
        $method->setAccessible(true);
126
        $method->invoke($observer, $category, true);
127
128
        // return category IDs the URL rewrites has been created for
129
        return $property->getValue($observer);
130
    }
131
132
    /**
133
     * Invoke a test to make sure that all categories that
134
     * have been related to a product have been processed.
135
     *
136
     * @return void
137
     */
138 View Code Duplication
    public function testCreateProductCategoryRelationWithChildCategoryAndSettingEnabled()
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...
139
    {
140
141
        // initialize method arguments
142
        $generateCategoryProductRewrites = true;
143
        $productCategoryIds = ['2'];
144
        $category = [
145
            'entity_id' => '10',
146
            'is_anchor' => '0'
147
        ];
148
149
        // create and invoke the partially mocked observer
150
        $productCategoryIds = $this->createAndInvokeObserver($generateCategoryProductRewrites, $productCategoryIds, $category);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $productCategoryIds is correct as $this->createAndInvokeOb...CategoryIds, $category) (which targets TechDivision\Import\Prod...eateAndInvokeObserver()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
151
152
        // assert that all categories have been processed
153
        $this->assertSame($productCategoryIds, ['2', '10']);
154
    }
155
156
    /**
157
     * Invoke a test to make sure that only the root category has been
158
     * processed, when the flag in the configuration has been activated.
159
     *
160
     * @return void
161
     */
162 View Code Duplication
    public function testCreateProductCategoryRelationWithChildCategoryAndSettingDisabled()
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...
163
    {
164
165
        // initialize method arguments
166
        $generateCategoryProductRewrites = false;
167
        $productCategoryIds = ['2'];
168
        $category = [
169
            'entity_id' => '10',
170
            'is_anchor' => '0'
171
        ];
172
173
        // create and invoke the partially mocked observer
174
        $productCategoryIds = $this->createAndInvokeObserver($generateCategoryProductRewrites, $productCategoryIds, $category);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $productCategoryIds is correct as $this->createAndInvokeOb...CategoryIds, $category) (which targets TechDivision\Import\Prod...eateAndInvokeObserver()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
175
176
        // assert that only the root category ID has been processed
177
        $this->assertSame($productCategoryIds, ['2']);
178
    }
179
180
    /**
181
     * Test's the handle() method with a successful URL rewrite persist.
182
     *
183
     * @return void
184
     */
185
    public function testHandleWithSuccessfullCreateWithoutCategories()
186
    {
187
188
        // create a dummy CSV file header
189
        $headers = array(
190
            'sku'             => 0,
191
            'url_key'         => 1,
192
            'store_view_code' => 2,
193
            'visibility'      => 3,
194
            'categories'      => 4
195
        );
196
197
        // create a dummy CSV file row
198
        $row = array(
199
            0 => $sku = 'TEST-01',
200
            1 => 'bruno-compete-hoodie-test',
201
            2 => $storeViewCode = 'default',
202
            3 => 'Catalog, Search',
203
            4 => null
204
        );
205
206
        // initialize category and entity ID
207
        $categoryId = 2;
208
        $entityId = 61413;
209
210
        // create a mock subject
211
        $mockSubject = $this->getMockBuilder('TechDivision\Import\Product\UrlRewrite\Subjects\UrlRewriteSubject')
212
                            ->setMethods(
213
                                array(
214
                                    'hasHeader',
215
                                    'getHeader',
216
                                    'getHeaders',
217
                                    'getRootCategory',
218
                                    'getCategory',
219
                                    'getCoreConfigData',
220
                                    'getRowStoreId',
221
                                    'getRow',
222
                                    'hasBeenProcessed',
223
                                    'addEntityIdVisibilityIdMapping',
224
                                    'getEntityIdVisibilityIdMapping',
225
                                    'getStoreViewCode',
226
                                    'isDebugMode',
227
                                    'storeIsActive'
228
                                )
229
                            )
230
                            ->disableOriginalConstructor()
231
                            ->getMock();
232
233
        // mock the methods
234
        $mockSubject->expects($this->any())
235
                    ->method('isDebugMode')
236
                    ->willReturn(false);
237
        $mockSubject->expects($this->any())
238
                    ->method('getHeaders')
239
                    ->willReturn($headers);
240
        $mockSubject->expects($this->any())
241
                    ->method('getRow')
242
                    ->willReturn($row);
243
        $mockSubject->expects($this->any())
244
                    ->method('hasHeader')
245
                    ->willReturn(true);
246
        $mockSubject->expects($this->any())
247
                    ->method('getHeader')
248
                    ->withConsecutive(
249
                        array(ColumnKeys::SKU),
250
                        array(ColumnKeys::URL_KEY),
251
                        array(ColumnKeys::URL_KEY),
252
                        array(ColumnKeys::STORE_VIEW_CODE),
253
                        array(ColumnKeys::VISIBILITY),
254
                        array(ColumnKeys::STORE_VIEW_CODE),
255
                        array(ColumnKeys::CATEGORIES),
256
                        array(ColumnKeys::STORE_VIEW_CODE)
257
                    )
258
                    ->willReturnOnConsecutiveCalls(0, 1, 1, 2, 3, 2, 4, 2);
259
        $mockSubject->expects($this->once())
260
                    ->method('hasBeenProcessed')
261
                    ->willReturn(false);
262
        $mockSubject->expects($this->once())
263
                    ->method('getEntityIdVisibilityIdMapping')
264
                    ->willReturn(VisibilityKeys::VISIBILITY_BOTH);
265
        $mockSubject->expects($this->exactly(1))
266
                    ->method('getRowStoreId')
267
                    ->willReturn($storeId = 1);
268
        $mockSubject->expects($this->exactly(2))
269
                    ->method('getCategory')
270
                    ->with($categoryId)
271
                    ->willReturn($category = array(MemberNames::ENTITY_ID => $categoryId, MemberNames::PARENT_ID => 1, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => null));
272
        $mockSubject->expects($this->exactly(5))
273
                    ->method('getRootCategory')
274
                    ->willReturn($category);
275
        $mockSubject->expects($this->once())
276
                    ->method('getStoreViewCode')
277
                    ->with(StoreViewCodes::ADMIN)
278
                    ->willReturn($storeViewCode);
279
        $mockSubject->expects($this->once())
280
                    ->method('storeIsActive')
281
                    ->with($storeViewCode)
282
                    ->willReturn(true);
283
        $mockSubject->expects($this->exactly(1))
284
                    ->method('getCoreConfigData')
285
                    ->with(CoreConfigDataKeys::CATALOG_SEO_PRODUCT_URL_SUFFIX, '.html')
286
                    ->willReturn('.html');
287
288
        // mock the processor methods
289
        $this->mockProductUrlRewriteProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...riteProcessorInterface>.

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...
290
                    ->method('loadProduct')
291
                    ->with($sku)
292
                    ->willReturn(array(MemberNames::ENTITY_ID => $entityId));
293
        $this->mockProductUrlRewriteProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...riteProcessorInterface>.

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...
294
                    ->method('persistUrlRewrite')
295
                    ->with(
296
                        array(
297
                            MemberNames::ENTITY_TYPE      => UrlRewriteObserver::ENTITY_TYPE,
298
                            MemberNames::ENTITY_ID        => $entityId,
299
                            MemberNames::REQUEST_PATH     => sprintf('%s.html', $row[$headers[ColumnKeys::URL_KEY]]),
300
                            MemberNames::TARGET_PATH      => sprintf('catalog/product/view/id/%s', $entityId),
301
                            MemberNames::REDIRECT_TYPE    => 0,
302
                            MemberNames::STORE_ID         => $storeId,
303
                            MemberNames::DESCRIPTION      => null,
304
                            MemberNames::IS_AUTOGENERATED => 1,
305
                            MemberNames::METADATA         => null,
306
                            EntityStatus::MEMBER_NAME     => EntityStatus::STATUS_CREATE
307
                        )
308
                    )
309
                    ->willReturn(1000);
310
311
        // invoke the handle() method
312
        $this->assertSame($row, $this->observer->handle($mockSubject));
0 ignored issues
show
Documentation introduced by
$mockSubject is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TechDivision\Impo...jects\SubjectInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
313
    }
314
315
    /**
316
     * Test's the handle() method with a successful URL rewrite persist when using the same categories.
317
     *
318
     * @return void
319
     */
320
    public function testHandleWithSuccessfullUpdateAndSameCategories()
321
    {
322
323
        // initialize the entity ID to use
324
        $entityId = 61413;
325
326
        // create a dummy CSV file row
327
        $headers = array(
328
            'sku'                => 0,
329
            'url_key'            => 1,
330
            'categories'         => 2,
331
            'store_view_code'    => 3,
332
            'visibility'         => 4
333
        );
334
335
        // create a dummy CSV file header
336
        $row = array(
337
            0 => $sku = 'TEST-01',
338
            1 => 'bruno-compete-hoodie',
339
            2 => 'Default Category/Men/Tops/Hoodies & Sweatshirts,Default Category/Collections/Eco Friendly,Default Category',
340
            3 => $storeViewCode = 'default',
341
            4 => 'Catalog, Search'
342
        );
343
344
        // initialize the categories
345
        $categories = array(
346
             $path1 = 'Default Category'                                => array(MemberNames::ENTITY_ID => 2, MemberNames::PARENT_ID => 1, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => null, MemberNames::PATH => $path1),
347
             $path2 = 'Default Category/Men'                            => array(MemberNames::ENTITY_ID => 3, MemberNames::PARENT_ID => 2, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => 'men', MemberNames::PATH => $path2),
348
             $path3 = 'Default Category/Men/Tops'                       => array(MemberNames::ENTITY_ID => 4, MemberNames::PARENT_ID => 3, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => 'men/tops-men', MemberNames::PATH => $path3),
349
             $path4 = 'Default Category/Men/Tops/Hoodies & Sweatshirts' => array(MemberNames::ENTITY_ID => 5, MemberNames::PARENT_ID => 4, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => 'men/tops-men/hoodies-and-sweatshirts-men', MemberNames::PATH => $path4),
350
             $path5 = 'Default Category/Collections'                    => array(MemberNames::ENTITY_ID => 6, MemberNames::PARENT_ID => 3, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => 'collections', MemberNames::PATH => $path5),
351
             $path6 = 'Default Category/Collections/Eco Friendly'       => array(MemberNames::ENTITY_ID => 7, MemberNames::PARENT_ID => 6, MemberNames::IS_ANCHOR => null, MemberNames::URL_PATH => 'collections/eco-friendly', MemberNames::PATH => $path6),
352
        );
353
354
        // initialize a mock import adapter instance
355
        $mockImportAdapter = $this->getMockBuilder(SerializerAwareAdapterInterface::class)->getMock();
356
        $mockImportAdapter->expects($this->exactly(4))
357
            ->method('explode')
358
            ->withConsecutive(
359
                array($row[2]),
360
                array('Default Category/Men/Tops/Hoodies & Sweatshirts'),
361
                array('Default Category/Collections/Eco Friendly'),
362
                array('Default Category')
363
            )
364
            ->willReturnOnConsecutiveCalls(
365
                array('Default Category/Men/Tops/Hoodies & Sweatshirts', 'Default Category/Collections/Eco Friendly', 'Default Category'),
366
                array('Default Category', 'Men', 'Tops', 'Hoodies & Sweatshirts'),
367
                array('Default Category', 'Collections', 'Eco Friendly'),
368
                array('Default Category')
369
            );
370
371
        // mock the system logger
372
        $mockSystemLogger = $this->getMockBuilder('Psr\Log\LoggerInterface')
373
                                 ->setMethods(get_class_methods('Psr\Log\LoggerInterface'))
374
                                 ->getMock();
375
376
        // create a mock subject
377
        $mockSubject = $this->getMockBuilder('TechDivision\Import\Product\UrlRewrite\Subjects\UrlRewriteSubject')
378
                            ->setMethods(
379
                                array(
380
                                    'hasHeader',
381
                                    'getHeader',
382
                                    'getHeaders',
383
                                    'getRootCategory',
384
                                    'getRowStoreId',
385
                                    'getCategory',
386
                                    'getCoreConfigData',
387
                                    'getRow',
388
                                    'hasBeenProcessed',
389
                                    'addEntityIdVisibilityIdMapping',
390
                                    'getEntityIdVisibilityIdMapping',
391
                                    'getStoreViewCode',
392
                                    'isDebugMode',
393
                                    'storeIsActive',
394
                                    'getCategoryByPath',
395
                                    'getSystemLogger',
396
                                    'getImportAdapter'
397
                                )
398
                            )
399
                            ->disableOriginalConstructor()
400
                            ->getMock();
401
402
        // mock the methods
403
        $mockSubject->expects($this->any())
404
                    ->method('getSystemLogger')
405
                    ->willReturn($mockSystemLogger);
406
        $mockSubject->expects($this->any())
407
                    ->method('isDebugMode')
408
                    ->willReturn(false);
409
        $mockSubject->expects($this->any())
410
                    ->method('getHeaders')
411
                    ->willReturn($headers);
412
        $mockSubject->expects($this->any())
413
                    ->method('getRow')
414
                    ->willReturn($row);
415
        $mockSubject->expects($this->any())
416
                    ->method('hasHeader')
417
                    ->willReturn(true);
418
        $mockSubject->expects($this->any())
419
                    ->method('getSystemLogger')
420
                    ->willReturn($mockSystemLogger);
421
        $mockSubject->expects($this->any())
422
                    ->method('getHeader')
423
                    ->withConsecutive(
424
                        array(ColumnKeys::SKU),
425
                        array(ColumnKeys::URL_KEY),
426
                        array(ColumnKeys::URL_KEY),
427
                        array(ColumnKeys::STORE_VIEW_CODE),
428
                        array(ColumnKeys::VISIBILITY),
429
                        array(ColumnKeys::STORE_VIEW_CODE),
430
                        array(ColumnKeys::CATEGORIES),
431
                        array(ColumnKeys::STORE_VIEW_CODE)
432
                    )
433
                    ->willReturnOnConsecutiveCalls(0, 1, 1, 3, 4, 3, 2, 3);
434
        $mockSubject->expects($this->once())
435
                    ->method('hasBeenProcessed')
436
                    ->willReturn(false);
437
        $mockSubject->expects($this->exactly(3))
438
                    ->method('getCategoryByPath')
439
                    ->withConsecutive(
440
                        array($path4 /* Default Category/Men/Tops/Hoodies & Sweatshirts */),
441
                        array($path6 /* Default Category/Collections/Eco Friendly */),
442
                        array($path1 /* Default Category */)
443
                    )
444
                    ->willReturnOnConsecutiveCalls(
445
                        $categories[$path4],
446
                        $categories[$path6],
447
                        $categories[$path1]
448
                    );
449
        $mockSubject->expects($this->exactly(13))
450
                    ->method('getCategory')
451
                    ->withConsecutive(
452
                        array($categories[$path4][MemberNames::ENTITY_ID]),
453
                        array($categories[$path3][MemberNames::ENTITY_ID]),
454
                        array($categories[$path2][MemberNames::ENTITY_ID]),
455
                        array($categories[$path6][MemberNames::ENTITY_ID]),
456
                        array($categories[$path5][MemberNames::ENTITY_ID]),
457
                        array($categories[$path2][MemberNames::ENTITY_ID]),
458
                        array($categories[$path1][MemberNames::ENTITY_ID]),
459
                        array($categories[$path1][MemberNames::ENTITY_ID]),
460
                        array($categories[$path4][MemberNames::ENTITY_ID]),
461
                        array($categories[$path6][MemberNames::ENTITY_ID]),
462
                        array($categories[$path1][MemberNames::ENTITY_ID]),
463
                        array($categories[$path4][MemberNames::ENTITY_ID]),
464
                        array($categories[$path6][MemberNames::ENTITY_ID])
465
                    )
466
                    ->willReturnOnConsecutiveCalls(
467
                        $categories[$path4],
468
                        $categories[$path3],
469
                        $categories[$path2],
470
                        $categories[$path6],
471
                        $categories[$path5],
472
                        $categories[$path2],
473
                        $categories[$path1],
474
                        $categories[$path1],
475
                        $categories[$path4],
476
                        $categories[$path6],
477
                        $categories[$path1],
478
                        $categories[$path4],
479
                        $categories[$path6]
480
                    );
481
        $mockSubject->expects($this->any())
482
                    ->method('getRootCategory')
483
                    ->willReturn($categories[$path1]);
484
        $mockSubject->expects($this->once())
485
                    ->method('getStoreViewCode')
486
                    ->with(StoreViewCodes::ADMIN)
487
                    ->willReturn($storeViewCode);
488
        $mockSubject->expects($this->once())
489
                    ->method('storeIsActive')
490
                    ->with($storeViewCode)
491
                    ->willReturn(true);
492
        $mockSubject->expects($this->once())
493
                    ->method('getEntityIdVisibilityIdMapping')
494
                    ->willReturn(VisibilityKeys::VISIBILITY_BOTH);
495
        $mockSubject->expects($this->any())
496
                    ->method('getRowStoreId')
497
                    ->willReturn($storeId = 1);
498
        $mockSubject->expects($this->any())
499
                    ->method('getCoreConfigData')
500
                    ->will(
501
                        $this->returnCallback(function ($arg1, $arg2) {
502
                            return $arg2;
503
                        })
504
                    );
505
       $mockSubject->expects(($this->exactly(4)))
506
                    ->method('getImportAdapter')
507
                    ->willReturn($mockImportAdapter);
508
509
        // mock the processor methods
510
        $this->mockProductUrlRewriteProcessor->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...riteProcessorInterface>.

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...
511
                    ->method('loadProduct')
512
                    ->with($sku)
513
                    ->willReturn(array(MemberNames::ENTITY_ID => $entityId));
514
        $this->mockProductUrlRewriteProcessor->expects($this->exactly(3))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...riteProcessorInterface>.

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...
515
                    ->method('persistUrlRewrite')
516
                    ->withConsecutive(
517
                        array(
518
                            array(
519
                                MemberNames::ENTITY_TYPE      => UrlRewriteObserver::ENTITY_TYPE,
520
                                MemberNames::ENTITY_ID        => $entityId,
521
                                MemberNames::REQUEST_PATH     => sprintf('%s.html', $row[$headers[ColumnKeys::URL_KEY]]),
522
                                MemberNames::TARGET_PATH      => sprintf('catalog/product/view/id/%s', $entityId),
523
                                MemberNames::REDIRECT_TYPE    => 0,
524
                                MemberNames::STORE_ID         => $storeId,
525
                                MemberNames::DESCRIPTION      => null,
526
                                MemberNames::IS_AUTOGENERATED => 1,
527
                                MemberNames::METADATA         => null,
528
                                EntityStatus::MEMBER_NAME     => EntityStatus::STATUS_CREATE
529
                            )
530
                        ),
531
                        array(
532
                            array(
533
                                MemberNames::ENTITY_TYPE      => UrlRewriteObserver::ENTITY_TYPE,
534
                                MemberNames::ENTITY_ID        => $entityId,
535
                                MemberNames::REQUEST_PATH     => sprintf('men/tops-men/hoodies-and-sweatshirts-men/%s.html', $row[$headers[ColumnKeys::URL_KEY]]),
536
                                MemberNames::TARGET_PATH      => sprintf('catalog/product/view/id/%s/category/5', $entityId),
537
                                MemberNames::REDIRECT_TYPE    => 0,
538
                                MemberNames::STORE_ID         => $storeId,
539
                                MemberNames::DESCRIPTION      => null,
540
                                MemberNames::IS_AUTOGENERATED => 1,
541
                                MemberNames::METADATA         => json_encode(array('category_id' => "5")),
542
                                EntityStatus::MEMBER_NAME     => EntityStatus::STATUS_CREATE
543
                            )
544
                        ),
545
                        array(
546
                            array(
547
                                MemberNames::ENTITY_TYPE      => UrlRewriteObserver::ENTITY_TYPE,
548
                                MemberNames::ENTITY_ID        => $entityId,
549
                                MemberNames::REQUEST_PATH     => sprintf('collections/eco-friendly/%s.html', $row[$headers[ColumnKeys::URL_KEY]]),
550
                                MemberNames::TARGET_PATH      => sprintf('catalog/product/view/id/%s/category/7', $entityId),
551
                                MemberNames::REDIRECT_TYPE    => 0,
552
                                MemberNames::STORE_ID         => $storeId,
553
                                MemberNames::DESCRIPTION      => null,
554
                                MemberNames::IS_AUTOGENERATED => 1,
555
                                MemberNames::METADATA         => json_encode(array('category_id' => "7")),
556
                                EntityStatus::MEMBER_NAME     => EntityStatus::STATUS_CREATE
557
                            )
558
                        )
559
                    )
560
                    ->willReturnOnConsecutiveCalls(1000, 1001, 1002);
561
        $this->mockProductUrlRewriteProcessor->expects($this->exactly(2))
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<TechDivision\Impo...riteProcessorInterface>.

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...
562
                    ->method('persistUrlRewriteProductCategory')
563
                    ->withConsecutive(
564
                        array(
565
                            array(
566
                                MemberNames::URL_REWRITE_ID   => 1001,
567
                                MemberNames::PRODUCT_ID       => $entityId,
568
                                MemberNames::CATEGORY_ID      => 5,
569
                                EntityStatus::MEMBER_NAME     => EntityStatus::STATUS_CREATE
570
                            )
571
                        ),
572
                        array(
573
                            array(
574
                                MemberNames::URL_REWRITE_ID   => 1002,
575
                                MemberNames::PRODUCT_ID       => $entityId,
576
                                MemberNames::CATEGORY_ID      => 7,
577
                                EntityStatus::MEMBER_NAME     => EntityStatus::STATUS_CREATE
578
                            )
579
                        )
580
                    );
581
582
        // invoke the handle() method
583
        $this->assertSame($row, $this->observer->handle($mockSubject));
0 ignored issues
show
Documentation introduced by
$mockSubject is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<TechDivision\Impo...jects\SubjectInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
584
    }
585
}
586