Skip to content

Commit 36c00d2

Browse files
committed
#23279: Integration test for unsubscribing from 'Back in stock notification'
Added integration tests for ProductAlert\Controller\Unsubscribe\Stock and ProductAlert\Controller\Add\Stock
1 parent b089cc2 commit 36c00d2

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

dev/tests/integration/testsuite/Magento/ProductAlert/Controller/Add/StockTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\ProductAlert\Controller\Add;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Framework\App\ResourceConnection;
1112
use Magento\Framework\ObjectManagerInterface;
1213
use Magento\Framework\App\Action\Action;
@@ -20,6 +21,7 @@
2021
* Test for Magento\ProductAlert\Controller\Add\Stock class.
2122
*
2223
* @magentoAppIsolation enabled
24+
* @magentoDbIsolation enabled
2325
*/
2426
class StockTest extends AbstractController
2527
{
@@ -50,6 +52,11 @@ class StockTest extends AbstractController
5052
*/
5153
protected $connectionMock;
5254

55+
/**
56+
* @var ProductRepositoryInterface
57+
*/
58+
private $productRepository;
59+
5360
protected function setUp()
5461
{
5562
parent::setUp();
@@ -60,16 +67,17 @@ protected function setUp()
6067

6168
$this->resource = $this->objectManager->get(ResourceConnection::class);
6269
$this->connectionMock = $this->resource->getConnection();
70+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
6371
}
6472

6573
/**
6674
* @magentoAppArea frontend
6775
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
6876
* @magentoDataFixture Magento/Customer/_files/customer.php
6977
*/
70-
public function testSubscribe()
78+
public function testSubscribeStockNotification()
7179
{
72-
$productId = 1;
80+
$productId = $this->productRepository->get('simple-out-of-stock')->getId();
7381
$customerId = 1;
7482

7583
$this->customerSession->setCustomerId($customerId);
@@ -81,17 +89,17 @@ public function testSubscribe()
8189
$this->dispatch('productalert/add/stock');
8290

8391
$select = $this->connectionMock->select()->from($this->resource->getTableName('product_alert_stock'))
84-
->where('`customer_id` LIKE ?', '1');
92+
->where('`product_id` LIKE ?', $productId);
8593
$result = $this->connectionMock->fetchAll($select);
8694
$this->assertCount(1, $result);
8795
}
8896

8997
/**
90-
* @param int $productId
98+
* @param $productId
9199
*
92100
* @return string
93101
*/
94-
private function getUrlEncodedParameter(int $productId):string
102+
private function getUrlEncodedParameter($productId):string
95103
{
96104
$baseUrl = $this->objectManager->get(StoreManagerInterface::class)->getStore()->getBaseUrl();
97105
$encodedParameterValue = urlencode(

dev/tests/integration/testsuite/Magento/ProductAlert/Controller/Unsubscribe/StockTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
namespace Magento\ProductAlert\Controller\Unsubscribe;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
1113
use Magento\Framework\ObjectManagerInterface;
12-
use Magento\Framework\Url\Helper\Data;
1314
use Magento\Customer\Model\Session;
1415
use Magento\TestFramework\Helper\Bootstrap;
1516
use Magento\TestFramework\TestCase\AbstractController;
@@ -18,6 +19,7 @@
1819
* Test for Magento\ProductAlert\Controller\Unsubscribe\Stock class.
1920
*
2021
* @magentoAppIsolation enabled
22+
* @magentoDbIsolation enabled
2123
*/
2224
class StockTest extends AbstractController
2325
{
@@ -39,17 +41,23 @@ class StockTest extends AbstractController
3941
/**
4042
* Connection adapter
4143
*
42-
* @var \Magento\Framework\DB\Adapter\AdapterInterface
44+
* @var AdapterInterface
4345
*/
4446
protected $connectionMock;
4547

48+
/**
49+
* @var ProductRepositoryInterface
50+
*/
51+
private $productRepository;
52+
4653
protected function setUp()
4754
{
4855
parent::setUp();
4956
$this->objectManager = Bootstrap::getObjectManager();
5057
$this->customerSession = $this->objectManager->get(Session::class);
5158
$this->resource = $this->objectManager->get(ResourceConnection::class);
5259
$this->connectionMock = $this->resource->getConnection();
60+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
5361
}
5462

5563
/**
@@ -58,18 +66,18 @@ protected function setUp()
5866
* @magentoDataFixture Magento/Customer/_files/customer.php
5967
* @magentoDataFixture Magento/ProductAlert/_files/customer_unsubscribe_stock.php
6068
*/
61-
public function testSubscribe()
69+
public function testUnsubscribeStockNotification()
6270
{
63-
$productId = 1;
6471
$customerId = 1;
72+
$productId = $this->productRepository->get('simple-out-of-stock')->getId();
6573

6674
$this->customerSession->setCustomerId($customerId);
67-
$select = $this->connectionMock->select()->from($this->resource->getTableName('product_alert_stock'))
68-
->where('`customer_id` LIKE ?', $customerId);
6975

7076
$this->getRequest()->setPostValue('product', $productId)->setMethod('POST');
7177
$this->dispatch('productalert/unsubscribe/stock');
7278

79+
$select = $this->connectionMock->select()->from($this->resource->getTableName('product_alert_stock'))
80+
->where('`product_id` LIKE ?', $productId);
7381
$result = $this->connectionMock->fetchAll($select);
7482
$this->assertCount(0, $result);
7583
}

dev/tests/integration/testsuite/Magento/ProductAlert/_files/customer_unsubscribe_stock.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
use Magento\Catalog\Api\ProductRepositoryInterface;
78
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
89
use Magento\ProductAlert\Model\ResourceModel\Stock;
910
use Magento\TestFramework\Helper\Bootstrap;
1011

1112
$objectManager = Bootstrap::getObjectManager();
12-
$resource = $objectManager->create(Stock::class);
13+
$resource = $objectManager->get(Stock::class);
1314

1415
/** @var \Magento\Framework\Stdlib\DateTime\DateTime $dateTime */
1516
$dateTime = $objectManager->get(DateTimeFactory::class)->create();
1617
$date = $dateTime->gmtDate(null, ($dateTime->gmtTimestamp() - 3600));
18+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
19+
$productId = $productRepository->get('simple-out-of-stock')->getId();
1720

1821
$resource->getConnection()->insert(
1922
$resource->getMainTable(),
2023
[
2124
'customer_id' => 1,
22-
'product_id' => 1,
25+
'product_id' => $productId,
2326
'website_id' => 1,
2427
'store_id' => 1,
2528
'add_date' => $date,

0 commit comments

Comments
 (0)