Skip to content

Commit 8a9a649

Browse files
ENGCOM-5940: 23279: Implementation of integration tests for ProductAlert Stock notifications #24291
- Merge Pull Request #24291 from yuriichayka/magento2:23279 - Merged commits: 1. b089cc2 2. 36c00d2 3. 665aad5 4. f590127
2 parents d6ef3ef + f590127 commit 8a9a649

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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\Controller\Add;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\App\Action\Action;
14+
use Magento\Framework\Url\Helper\Data;
15+
use Magento\Customer\Model\Session;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\TestFramework\TestCase\AbstractController;
19+
20+
/**
21+
* Test for Magento\ProductAlert\Controller\Add\Stock class.
22+
*
23+
* @magentoAppIsolation enabled
24+
* @magentoDbIsolation enabled
25+
*/
26+
class StockTest extends AbstractController
27+
{
28+
/**
29+
* @var Session
30+
*/
31+
private $customerSession;
32+
33+
/**
34+
* @var ObjectManagerInterface
35+
*/
36+
private $objectManager;
37+
38+
/**
39+
* @var Data
40+
*/
41+
private $dataUrlHelper;
42+
43+
/**
44+
* @var ResourceConnection
45+
*/
46+
protected $resource;
47+
48+
/**
49+
* Connection adapter
50+
*
51+
* @var \Magento\Framework\DB\Adapter\AdapterInterface
52+
*/
53+
protected $connectionMock;
54+
55+
/**
56+
* @var ProductRepositoryInterface
57+
*/
58+
private $productRepository;
59+
60+
protected function setUp()
61+
{
62+
parent::setUp();
63+
$this->objectManager = Bootstrap::getObjectManager();
64+
65+
$this->customerSession = $this->objectManager->get(Session::class);
66+
$this->dataUrlHelper = $this->objectManager->get(Data::class);
67+
68+
$this->resource = $this->objectManager->get(ResourceConnection::class);
69+
$this->connectionMock = $this->resource->getConnection();
70+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
71+
}
72+
73+
/**
74+
* @magentoAppArea frontend
75+
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
76+
* @magentoDataFixture Magento/Customer/_files/customer.php
77+
*/
78+
public function testSubscribeStockNotification()
79+
{
80+
$productId = $this->productRepository->get('simple-out-of-stock')->getId();
81+
$customerId = 1;
82+
83+
$this->customerSession->setCustomerId($customerId);
84+
85+
$encodedParameterValue = $this->getUrlEncodedParameter($productId);
86+
$this->getRequest()->setMethod('GET');
87+
$this->getRequest()->setQueryValue('product_id', $productId);
88+
$this->getRequest()->setQueryValue(Action::PARAM_NAME_URL_ENCODED, $encodedParameterValue);
89+
$this->dispatch('productalert/add/stock');
90+
91+
$select = $this->connectionMock->select()->from($this->resource->getTableName('product_alert_stock'))
92+
->where('`product_id` LIKE ?', $productId);
93+
$result = $this->connectionMock->fetchAll($select);
94+
$this->assertCount(1, $result);
95+
}
96+
97+
/**
98+
* @param $productId
99+
*
100+
* @return string
101+
*/
102+
private function getUrlEncodedParameter($productId):string
103+
{
104+
$baseUrl = $this->objectManager->get(StoreManagerInterface::class)->getStore()->getBaseUrl();
105+
$encodedParameterValue = urlencode(
106+
$this->dataUrlHelper->getEncodedUrl($baseUrl . 'productalert/add/stock/product_id/' . $productId)
107+
);
108+
109+
return $encodedParameterValue;
110+
}
111+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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\Controller\Unsubscribe;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Customer\Model\Session;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\TestCase\AbstractController;
17+
18+
/**
19+
* Test for Magento\ProductAlert\Controller\Unsubscribe\Stock class.
20+
*
21+
* @magentoAppIsolation enabled
22+
* @magentoDbIsolation enabled
23+
*/
24+
class StockTest extends AbstractController
25+
{
26+
/**
27+
* @var Session
28+
*/
29+
private $customerSession;
30+
31+
/**
32+
* @var ObjectManagerInterface
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var ResourceConnection
38+
*/
39+
protected $resource;
40+
41+
/**
42+
* Connection adapter
43+
*
44+
* @var AdapterInterface
45+
*/
46+
protected $connectionMock;
47+
48+
/**
49+
* @var ProductRepositoryInterface
50+
*/
51+
private $productRepository;
52+
53+
protected function setUp()
54+
{
55+
parent::setUp();
56+
$this->objectManager = Bootstrap::getObjectManager();
57+
$this->customerSession = $this->objectManager->get(Session::class);
58+
$this->resource = $this->objectManager->get(ResourceConnection::class);
59+
$this->connectionMock = $this->resource->getConnection();
60+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
61+
}
62+
63+
/**
64+
* @magentoAppArea frontend
65+
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
66+
* @magentoDataFixture Magento/Customer/_files/customer.php
67+
* @magentoDataFixture Magento/ProductAlert/_files/customer_unsubscribe_stock.php
68+
*/
69+
public function testUnsubscribeStockNotification()
70+
{
71+
$customerId = 1;
72+
$productId = $this->productRepository->get('simple-out-of-stock')->getId();
73+
74+
$this->customerSession->setCustomerId($customerId);
75+
76+
$this->getRequest()->setPostValue('product', $productId)->setMethod('POST');
77+
$this->dispatch('productalert/unsubscribe/stock');
78+
79+
$select = $this->connectionMock->select()->from($this->resource->getTableName('product_alert_stock'))
80+
->where('`product_id` LIKE ?', $productId);
81+
$result = $this->connectionMock->fetchAll($select);
82+
$this->assertCount(0, $result);
83+
}
84+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
9+
use Magento\ProductAlert\Model\ResourceModel\Stock;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
$resource = $objectManager->get(Stock::class);
14+
15+
/** @var \Magento\Framework\Stdlib\DateTime\DateTime $dateTime */
16+
$dateTime = $objectManager->get(DateTimeFactory::class)->create();
17+
$date = $dateTime->gmtDate(null, ($dateTime->gmtTimestamp() - 3600));
18+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
19+
$productId = $productRepository->get('simple-out-of-stock')->getId();
20+
21+
$resource->getConnection()->insert(
22+
$resource->getMainTable(),
23+
[
24+
'customer_id' => 1,
25+
'product_id' => $productId,
26+
'website_id' => 1,
27+
'store_id' => 1,
28+
'add_date' => $date,
29+
'send_date' => null,
30+
'send_count' => 0,
31+
'status' => 0
32+
]
33+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\ProductAlert\Model\ResourceModel\Stock;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
$objectManager = Bootstrap::getObjectManager();
12+
$resource = $objectManager->get(Stock::class);
13+
14+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
15+
$productId = $productRepository->get('simple-out-of-stock')->getId();
16+
17+
$resource->getConnection()->delete(
18+
$resource->getMainTable(),
19+
['product_id = ?' => $productId]
20+
);

0 commit comments

Comments
 (0)