Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Catalog] covered product ViewModel AddToCompareAvailability by Unit Test #26050

Merged
merged 5 commits into from
Dec 18, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\ViewModel\Product\Checker;

use Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Unit test for Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability.
*/
class AddToCompareAvailabilityTest extends \PHPUnit\Framework\TestCase
{

/**
* @var AddToCompareAvailability
*/
private $viewModel;

/**
* @var StockConfigurationInterface|MockObject
*/
protected $stockConfiguration;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp(): void
{

$this->stockConfiguration =
$this->getMockBuilder(StockConfigurationInterface::class)
->getMock();

$this->viewModel = $this->getObjectManager()->getObject(
AddToCompareAvailability::class,
[
'stockConfiguration' => $this->stockConfiguration
]
);
}

/**
* Test IsAvailableForCompare() with data provider
*
* @param bool $status
* @param bool $isSalable
* @param array $isInStock
* @param bool $isShowOutOfStock
* @return boolean
* @dataProvider isAvailableForCompareDataProvider
*/
public function testIsAvailableForCompare($status, $isSalable, $isInStock, $isShowOutOfStock): bool
{
$productMock = $this->getMockBuilder(Product::class)
->disableOriginalConstructor()
->getMock();

$productMock->expects($this->once())
->method('getStatus')
->willReturn($status);

$productMock->expects($this->any())
->method('isSalable')
->willReturn($isSalable);

$productMock->expects($this->any())
->method('getQuantityAndStockStatus')
->willReturn($isInStock);

$this->stockConfiguration->expects($this->any())
->method('isShowOutOfStock')
->willReturn($isShowOutOfStock);

return $this->viewModel->isAvailableForCompare($productMock);
}

/**
* Data provider for isAvailableForCompare()
*
* @return array
*/
public function isAvailableForCompareDataProvider(): array
{
return [
[1, true, ['is_in_stock' => true], false],
[1, true, ['is_in_stock' => false], true],
[1, true, [], false],
[1, false, [], false],
[2, true, ['is_in_stock' => true], false]
];
}

/**
* @return ObjectManager
*/
private function getObjectManager(): ObjectManager
{
if (null === $this->objectManager) {
$this->objectManager = new ObjectManager($this);
}

return $this->objectManager;
}
}