Skip to content

Commit f075d4e

Browse files
committed
[Catalog] cover ViewModel AddToCompareAvailability by Unit Test
1 parent e8c2526 commit f075d4e

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Test\Unit\ViewModel\Product\Checker;
10+
11+
use Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability;
12+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
13+
use Magento\Catalog\Model\Product;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
17+
/**
18+
* Unit test for Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability.
19+
*/
20+
class AddToCompareAvailabilityTest extends \PHPUnit\Framework\TestCase
21+
{
22+
23+
/**
24+
* @var AddToCompareAvailability
25+
*/
26+
private $viewModel;
27+
28+
/**
29+
* @var StockConfigurationInterface|MockObject
30+
*/
31+
protected $stockConfiguration;
32+
33+
/**
34+
* @var ObjectManager
35+
*/
36+
private $objectManager;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
44+
$this->stockConfiguration =
45+
$this->getMockBuilder(StockConfigurationInterface::class)
46+
->getMock();
47+
48+
$this->viewModel = $this->getObjectManager()->getObject(
49+
AddToCompareAvailability::class,
50+
[
51+
'stockConfiguration' => $this->stockConfiguration
52+
]
53+
);
54+
}
55+
56+
/**
57+
* Test IsAvailableForCompare() with data provider
58+
*
59+
* @param bool $status
60+
* @param bool $isSalable
61+
* @param array $isInStock
62+
* @param bool $isShowOutOfStock
63+
* @return boolean
64+
* @dataProvider isAvailableForCompareDataProvider
65+
*/
66+
public function testIsAvailableForCompare($status, $isSalable, $isInStock, $isShowOutOfStock): bool
67+
{
68+
$productMock = $this->getMockBuilder(Product::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
72+
$productMock->expects($this->once())
73+
->method('getStatus')
74+
->willReturn($status);
75+
76+
$productMock->expects($this->any())
77+
->method('isSalable')
78+
->willReturn($isSalable);
79+
80+
$productMock->expects($this->any())
81+
->method('getQuantityAndStockStatus')
82+
->willReturn($isInStock);
83+
84+
$this->stockConfiguration->expects($this->any())
85+
->method('isShowOutOfStock')
86+
->willReturn($isShowOutOfStock);
87+
88+
return $this->viewModel->isAvailableForCompare($productMock);
89+
}
90+
91+
/**
92+
* Data provider for isAvailableForCompare()
93+
*
94+
* @return array
95+
*/
96+
public function isAvailableForCompareDataProvider(): array
97+
{
98+
return [
99+
[1, true, ['is_in_stock' => true], false],
100+
[1, true, ['is_in_stock' => false], true],
101+
[1, true, [], false],
102+
[1, false, [], false],
103+
[2, true, ['is_in_stock' => true], false]
104+
];
105+
}
106+
107+
/**
108+
* @return ObjectManager
109+
*/
110+
private function getObjectManager(): ObjectManager
111+
{
112+
if (null === $this->objectManager) {
113+
$this->objectManager = new ObjectManager($this);
114+
}
115+
116+
return $this->objectManager;
117+
}
118+
}

0 commit comments

Comments
 (0)