Skip to content

Commit b089cc2

Browse files
committed
initial commit for the issue
1 parent c6427d1 commit b089cc2

File tree

3 files changed

+209
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)