Skip to content

Commit 08b68c9

Browse files
ENGCOM-6477: [Catalog] covered product ViewModel AddToCompareAvailability by Unit Test #26050
2 parents 5eed9d3 + 3f75c04 commit 08b68c9

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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\Catalog\Model\Product\Attribute\Source\Status;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
18+
/**
19+
* Unit test for Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability.
20+
*/
21+
class AddToCompareAvailabilityTest extends \PHPUnit\Framework\TestCase
22+
{
23+
24+
/**
25+
* @var AddToCompareAvailability
26+
*/
27+
private $viewModel;
28+
29+
/**
30+
* @var StockConfigurationInterface|MockObject
31+
*/
32+
protected $stockConfigurationMock;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp(): void
38+
{
39+
40+
$objectManager = new ObjectManager($this);
41+
42+
$this->stockConfigurationMock =
43+
$this->getMockBuilder(StockConfigurationInterface::class)
44+
->getMock();
45+
46+
$this->viewModel = $objectManager->getObject(
47+
AddToCompareAvailability::class,
48+
[
49+
'stockConfiguration' => $this->stockConfigurationMock
50+
]
51+
);
52+
}
53+
54+
/**
55+
* Test IsAvailableForCompare() with data provider
56+
*
57+
* @param bool $status
58+
* @param bool $isSalable
59+
* @param array $isInStock
60+
* @param bool $isShowOutOfStock
61+
* @param bool $expectedBool
62+
* @return void
63+
* @dataProvider isAvailableForCompareDataProvider
64+
*/
65+
public function testIsAvailableForCompare($status, $isSalable, $isInStock, $isShowOutOfStock, $expectedBool): void
66+
{
67+
$productMock = $this->getMockBuilder(Product::class)
68+
->disableOriginalConstructor()
69+
->getMock();
70+
71+
$productMock->expects($this->once())
72+
->method('getStatus')
73+
->willReturn($status);
74+
75+
$productMock->expects($this->any())
76+
->method('isSalable')
77+
->willReturn($isSalable);
78+
79+
$productMock->expects($this->any())
80+
->method('getQuantityAndStockStatus')
81+
->willReturn($isInStock);
82+
83+
$this->stockConfigurationMock->expects($this->any())
84+
->method('isShowOutOfStock')
85+
->willReturn($isShowOutOfStock);
86+
87+
$this->assertEquals($expectedBool, $this->viewModel->isAvailableForCompare($productMock));
88+
}
89+
90+
/**
91+
* Data provider for isAvailableForCompare()
92+
*
93+
* @return array
94+
*/
95+
public function isAvailableForCompareDataProvider(): array
96+
{
97+
return [
98+
[Status::STATUS_ENABLED, true, ['is_in_stock' => true], false, true],
99+
[Status::STATUS_ENABLED, true, ['is_in_stock' => false], true, true],
100+
[Status::STATUS_ENABLED, true, [], false, true],
101+
[Status::STATUS_ENABLED, false, [], false, false],
102+
[Status::STATUS_DISABLED, true, ['is_in_stock' => true], false, false]
103+
];
104+
}
105+
}

0 commit comments

Comments
 (0)