Skip to content

Commit c6fdd50

Browse files
Add unit test classes
1 parent edd9d4c commit c6fdd50

File tree

3 files changed

+151
-9
lines changed

3 files changed

+151
-9
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Bundle\Test\Unit\Model\Plugin\Frontend;
8+
9+
use Magento\Bundle\Model\Plugin\Frontend\Product as ProductPlugin;
10+
use Magento\Bundle\Model\Product\Type;
11+
use Magento\Catalog\Model\Product;
12+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13+
14+
class ProductTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/** @var \Magento\Bundle\Model\Plugin\Product */
17+
private $plugin;
18+
19+
/** @var MockObject|Type */
20+
private $type;
21+
22+
/** @var MockObject|\Magento\Catalog\Model\Product */
23+
private $product;
24+
25+
protected function setUp()
26+
{
27+
$this->product = $this->getMockBuilder(Product::class)
28+
->disableOriginalConstructor()
29+
->setMethods(['getEntityId'])
30+
->getMock();
31+
32+
$this->type = $this->getMockBuilder(Type::class)
33+
->disableOriginalConstructor()
34+
->setMethods(['getChildrenIds'])
35+
->getMock();
36+
37+
$this->plugin = new ProductPlugin($this->type);
38+
}
39+
40+
public function testAfterGetIdentities()
41+
{
42+
$baseIdentities = [
43+
'SomeCacheId',
44+
'AnotherCacheId',
45+
];
46+
$id = 12345;
47+
$childIds = [
48+
1 => [1, 2, 5, 100500],
49+
12 => [7, 22, 45, 24612]
50+
];
51+
$expectedIdentities = [
52+
'SomeCacheId',
53+
'AnotherCacheId',
54+
Product::CACHE_TAG . '_' . 1,
55+
Product::CACHE_TAG . '_' . 2,
56+
Product::CACHE_TAG . '_' . 5,
57+
Product::CACHE_TAG . '_' . 100500,
58+
Product::CACHE_TAG . '_' . 7,
59+
Product::CACHE_TAG . '_' . 22,
60+
Product::CACHE_TAG . '_' . 45,
61+
Product::CACHE_TAG . '_' . 24612,
62+
];
63+
$this->product->expects($this->once())
64+
->method('getEntityId')
65+
->will($this->returnValue($id));
66+
$this->type->expects($this->once())
67+
->method('getChildrenIds')
68+
->with($id)
69+
->will($this->returnValue($childIds));
70+
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
71+
$this->assertEquals($expectedIdentities, $identities);
72+
}
73+
}

app/code/Magento/ConfigurableProduct/Model/Plugin/Frontend/ProductIdentitiesExtender.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\ConfigurableProduct\Model\Plugin\Frontend;
99

1010
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
1211
use Magento\Catalog\Model\Product;
1312

1413
/**
@@ -21,19 +20,12 @@ class ProductIdentitiesExtender
2120
*/
2221
private $configurableType;
2322

24-
/**
25-
* @var ProductRepositoryInterface
26-
*/
27-
private $productRepository;
28-
2923
/**
3024
* @param Configurable $configurableType
31-
* @param ProductRepositoryInterface $productRepository
3225
*/
33-
public function __construct(Configurable $configurableType, ProductRepositoryInterface $productRepository)
26+
public function __construct(Configurable $configurableType)
3427
{
3528
$this->configurableType = $configurableType;
36-
$this->productRepository = $productRepository;
3729
}
3830

3931
/**
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProduct\Test\Unit\Model\Plugin\Frontend;
9+
10+
use Magento\ConfigurableProduct\Model\Plugin\Frontend\ProductIdentitiesExtender;
11+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
12+
use Magento\Catalog\Model\Product;
13+
14+
/**
15+
* Class ProductIdentitiesExtenderTest
16+
*/
17+
class ProductIdentitiesExtenderTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject|Configurable
21+
*/
22+
private $configurableTypeMock;
23+
24+
/**
25+
* @var ProductIdentitiesExtender
26+
*/
27+
private $plugin;
28+
29+
/** @var MockObject|\Magento\Catalog\Model\Product */
30+
private $product;
31+
32+
protected function setUp()
33+
{
34+
$this->product = $this->getMockBuilder(Product::class)
35+
->disableOriginalConstructor()
36+
->setMethods(['getId'])
37+
->getMock();
38+
39+
$this->configurableTypeMock = $this->getMockBuilder(Configurable::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
43+
$this->plugin = new ProductIdentitiesExtender($this->configurableTypeMock);
44+
}
45+
46+
public function testAfterGetIdentities()
47+
{
48+
$identities = [
49+
'SomeCacheId',
50+
'AnotherCacheId',
51+
];
52+
$productId = 12345;
53+
$childIdentities = [
54+
0 => [1, 2, 5, 100500]
55+
];
56+
$expectedIdentities = [
57+
'SomeCacheId',
58+
'AnotherCacheId',
59+
Product::CACHE_TAG . '_' . 1,
60+
Product::CACHE_TAG . '_' . 2,
61+
Product::CACHE_TAG . '_' . 5,
62+
Product::CACHE_TAG . '_' . 100500,
63+
];
64+
65+
$this->product->expects($this->once())
66+
->method('getId')
67+
->willReturn($productId);
68+
69+
$this->configurableTypeMock->expects($this->once())
70+
->method('getChildrenIds')
71+
->with($productId)
72+
->willReturn($childIdentities);
73+
74+
$productIdentities = $this->plugin->afterGetIdentities($this->product, $identities);
75+
$this->assertEquals($expectedIdentities, $productIdentities);
76+
}
77+
}

0 commit comments

Comments
 (0)