Skip to content

Commit 3f8060a

Browse files
#24645: Added unit test.
1 parent 8285e6a commit 3f8060a

File tree

1 file changed

+125
-0
lines changed
  • app/code/Magento/ProductAlert/Test/Unit/Controller/Unsubscribe

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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\ProductAlert\Test\Unit\Controller\Unsubscribe;
9+
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Customer\Model\Session;
14+
use Magento\Framework\App\Request\Http;
15+
use Magento\Framework\Controller\Result\Redirect;
16+
use Magento\Framework\Controller\ResultFactory;
17+
use Magento\Framework\Message\Manager;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\ProductAlert\Controller\Unsubscribe\Price;
20+
21+
/**
22+
* Test class for \Magento\ProductAlert\Controller\Unsubscribe\Price
23+
*/
24+
class PriceTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* @var Price
28+
*/
29+
private $priceController;
30+
31+
/**
32+
* @var ObjectManager
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var Http|\PHPUnit\Framework\MockObject\MockObject
38+
*/
39+
private $requestMock;
40+
41+
/**
42+
* @var Redirect|\PHPUnit\Framework\MockObject\MockObject
43+
*/
44+
private $resultRedirectMock;
45+
46+
/**
47+
* @var ResultFactory|\PHPUnit\Framework\MockObject\MockObject
48+
*/
49+
private $resultFactoryMock;
50+
51+
/**
52+
* @var Manager|\PHPUnit\Framework\MockObject\MockObject
53+
*/
54+
private $messageManagerMock;
55+
56+
/**
57+
* @var Product|\PHPUnit\Framework\MockObject\MockObject
58+
*/
59+
private $productMock;
60+
61+
/**
62+
* @var Session|\PHPUnit\Framework\MockObject\MockObject
63+
*/
64+
private $customerSessionMock;
65+
66+
/**
67+
* @var Context|\PHPUnit\Framework\MockObject\MockObject
68+
*/
69+
private $contextMock;
70+
71+
/**
72+
* @var ProductRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
73+
*/
74+
private $productRepositoryMock;
75+
76+
/**
77+
* @inheritdoc
78+
*/
79+
protected function setUp()
80+
{
81+
$this->objectManager = new ObjectManager($this);
82+
$this->requestMock = $this->createMock(Http::class);
83+
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
84+
$this->resultRedirectMock = $this->createMock(Redirect::class);
85+
$this->messageManagerMock = $this->createMock(Manager::class);
86+
$this->productMock = $this->createMock(Product::class);
87+
$this->contextMock = $this->createMock(Context::class);
88+
$this->customerSessionMock = $this->createMock(Session::class);
89+
$this->productRepositoryMock = $this->createMock(ProductRepositoryInterface::class);
90+
$this->resultFactoryMock->expects($this->any())
91+
->method('create')
92+
->with(ResultFactory::TYPE_REDIRECT)
93+
->willReturn($this->resultRedirectMock);
94+
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
95+
$this->contextMock->expects($this->any())->method('getResultFactory')->willReturn($this->resultFactoryMock);
96+
$this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
97+
98+
$this->priceController = $this->objectManager->getObject(
99+
Price::class,
100+
[
101+
'context' => $this->contextMock,
102+
'customerSession' => $this->customerSessionMock,
103+
'productRepository' => $this->productRepositoryMock,
104+
]
105+
);
106+
}
107+
108+
public function testProductIsNotVisibleInCatalog()
109+
{
110+
$productId = 123;
111+
$this->requestMock->expects($this->any())->method('getParam')->with('product')->willReturn($productId);
112+
$this->productRepositoryMock->expects($this->any())
113+
->method('getById')
114+
->with($productId)
115+
->willReturn($this->productMock);
116+
$this->productMock->expects($this->any())->method('isVisibleInCatalog')->willReturn(false);
117+
$this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with(__("The product wasn't found. Verify the product and try again."));
118+
$this->resultRedirectMock->expects($this->once())->method('setPath')->with('customer/account/');
119+
120+
$this->assertEquals(
121+
$this->resultRedirectMock,
122+
$this->priceController->execute()
123+
);
124+
}
125+
}

0 commit comments

Comments
 (0)