Skip to content

Commit 7803535

Browse files
committed
ADO-424: Adds phpunit for Meta_Promotions module
1 parent bd7c119 commit 7803535

File tree

7 files changed

+727
-0
lines changed

7 files changed

+727
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Promotions\Test\Unit\Block\Adminhtml\System\Config;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
8+
use Meta\Promotions\Block\Adminhtml\System\Config\PushPromotions;
9+
use Magento\Backend\Block\Template\Context;
10+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
11+
use Magento\Framework\App\RequestInterface;
12+
13+
class PushPromotionsTest extends TestCase
14+
{
15+
private $contextMock;
16+
private $systemConfigMock;
17+
private $requestMock;
18+
private $object;
19+
private $subject;
20+
21+
public function setUp(): void
22+
{
23+
$this->contextMock = $this->getMockBuilder(Context::class)
24+
->disableOriginalConstructor()
25+
->getMock();
26+
$this->systemConfigMock = $this->getMockBuilder(SystemConfig::class)
27+
->onlyMethods(['getCommerceAccountId', 'getPromotionsUrl'])
28+
->disableOriginalConstructor()
29+
->getMock();
30+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
31+
->disableOriginalConstructor()
32+
->getMockForAbstractClass();
33+
34+
$this->contextMock->expects($this->any())
35+
->method('getRequest')
36+
->willReturn($this->requestMock);
37+
38+
$this->object = new ObjectManager($this);
39+
$this->subject = $this->object->getObject(PushPromotions::class, [
40+
'context' => $this->contextMock,
41+
'systemConfig' => $this->systemConfigMock,
42+
'data' => []
43+
]);
44+
}
45+
46+
public function testGetCommerceAccountId()
47+
{
48+
$storeId = 1;
49+
$commerceAccountId = '3456765456';
50+
51+
$this->requestMock->expects($this->once())
52+
->method('getParam')
53+
->with('store')
54+
->willReturn($storeId);
55+
$this->systemConfigMock->expects($this->once())
56+
->method('getCommerceAccountId')
57+
->with($storeId)
58+
->willReturn($commerceAccountId);
59+
60+
$this->subject->getCommerceAccountId();
61+
}
62+
63+
public function testGetCommerceManagerPromotionsUrl()
64+
{
65+
$storeId = 1;
66+
$promotionUrl = 'https://www.facebook.com/commerce/'.$storeId.'/promotions/discounts/';
67+
68+
$this->requestMock->expects($this->once())
69+
->method('getParam')
70+
->with('store')
71+
->willReturn($storeId);
72+
$this->systemConfigMock->expects($this->once())
73+
->method('getPromotionsUrl')
74+
->with($storeId)
75+
->willReturn($promotionUrl);
76+
77+
$this->subject->getCommerceManagerPromotionsUrl();
78+
}
79+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Promotions\Test\Unit\Controller\Adminhtml\Ajax;
5+
6+
use Meta\BusinessExtension\Helper\FBEHelper;
7+
use Meta\Promotions\Model\Promotion\Feed\Uploader;
8+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\Controller\Result\JsonFactory;
11+
use PHPUnit\Framework\TestCase;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Meta\Promotions\Controller\Adminhtml\Ajax\PromotionFeedUpload;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use \Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Framework\App\RequestInterface;
17+
18+
class PromotionFeedUploadTest extends TestCase
19+
{
20+
private $fbeHelperMock;
21+
private $systemConfigMock;
22+
private $uploaderMock;
23+
private $contextMock;
24+
private $resultJsonMock;
25+
private $storeMock;
26+
private $storeManagerMock;
27+
private $requestMock;
28+
private $object;
29+
private $subject;
30+
31+
public function setUp(): void
32+
{
33+
$this->contextMock = $this->getMockBuilder(Context::class)
34+
->disableOriginalConstructor()
35+
->getMock();
36+
$this->resultJsonMock = $this->getMockBuilder(JsonFactory::class)
37+
->disableOriginalConstructor()
38+
->getMock();
39+
$this->fbeHelperMock = $this->getMockBuilder(FBEHelper::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$this->systemConfigMock = $this->getMockBuilder(SystemConfig::class)
43+
->disableOriginalConstructor()
44+
->getMock();
45+
$this->uploaderMock = $this->getMockBuilder(Uploader::class)
46+
->disableOriginalConstructor()
47+
->getMock();
48+
49+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
50+
->disableOriginalConstructor()
51+
->getMockForAbstractClass();
52+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
53+
->disableOriginalConstructor()
54+
->getMockForAbstractClass();
55+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
56+
->disableOriginalConstructor()
57+
->getMockForAbstractClass();
58+
59+
$this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
60+
61+
$this->object = new ObjectManager($this);
62+
$this->subject = $this->object->getObject(PromotionFeedUpload::class, [
63+
'context' => $this->contextMock,
64+
'resultJsonFactory' => $this->resultJsonMock,
65+
'fbeHelper' => $this->fbeHelperMock,
66+
'systemConfig' => $this->systemConfigMock,
67+
'uploader' => $this->uploaderMock
68+
]);
69+
}
70+
71+
public function testExecuteForJsonNoStoreParam()
72+
{
73+
$storeId = 1;
74+
$storeName = 'Default Store';
75+
76+
$this->fbeHelperMock->expects($this->exactly(2))
77+
->method('getStore')
78+
->willReturn($this->storeMock);
79+
$this->storeMock->expects($this->once())
80+
->method('getId')
81+
->willReturn($storeId);
82+
$this->storeMock->expects($this->once())
83+
->method('getName')
84+
->willReturn($storeName);
85+
86+
$this->subject->executeForJson();
87+
}
88+
89+
public function testExecuteForJsonStoreParam()
90+
{
91+
$storeId = 1;
92+
$storeName = 'Default Store';
93+
94+
$this->fbeHelperMock->expects($this->exactly(2))
95+
->method('getStore')
96+
->willReturn($this->storeMock);
97+
$this->storeMock->expects($this->once())
98+
->method('getId')
99+
->willReturn($storeId);
100+
$this->storeMock->expects($this->exactly(2))
101+
->method('getName')
102+
->willReturn($storeName);
103+
$this->requestMock->expects($this->once())
104+
->method('getParam')
105+
->with('store')
106+
->willReturn($storeId);
107+
$this->systemConfigMock->expects($this->once())
108+
->method('getStoreManager')
109+
->willReturn($this->storeManagerMock);
110+
$this->storeManagerMock->expects($this->once())
111+
->method('getStore')
112+
->with($storeId)
113+
->willReturn($this->storeMock);
114+
115+
$this->subject->executeForJson();
116+
}
117+
118+
public function testExecuteForJsonWithAccessToken()
119+
{
120+
$storeId = 1;
121+
$storeName = 'Default Store';
122+
$accessToken = 'akjfbkhab-afjhavfj-ahfhvgja';
123+
$pushFeedResponse = ['Success' => true];
124+
125+
$this->fbeHelperMock->expects($this->exactly(2))
126+
->method('getStore')
127+
->willReturn($this->storeMock);
128+
$this->storeMock->expects($this->once())
129+
->method('getId')
130+
->willReturn($storeId);
131+
$this->storeMock->expects($this->exactly(2))
132+
->method('getName')
133+
->willReturn($storeName);
134+
$this->requestMock->expects($this->once())
135+
->method('getParam')
136+
->with('store')
137+
->willReturn($storeId);
138+
$this->systemConfigMock->expects($this->once())
139+
->method('getStoreManager')
140+
->willReturn($this->storeManagerMock);
141+
$this->storeManagerMock->expects($this->once())
142+
->method('getStore')
143+
->with($storeId)
144+
->willReturn($this->storeMock);
145+
146+
$this->uploaderMock->expects($this->once())
147+
->method('uploadPromotions')
148+
->with($storeId)
149+
->willReturn($pushFeedResponse);
150+
$this->systemConfigMock->expects($this->once())
151+
->method('getAccessToken')
152+
->with($storeId)
153+
->willReturn($accessToken);
154+
155+
$this->subject->executeForJson();
156+
}
157+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Meta\Promotions\Test\Unit\Cron;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
8+
use Meta\Promotions\Cron\PromotionSyncCron;
9+
use Meta\BusinessExtension\Helper\FBEHelper;
10+
use Meta\Promotions\Model\Promotion\Feed\Uploader;
11+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
14+
class PromotionSyncCronTest extends TestCase
15+
{
16+
private $fbeHelperMock;
17+
private $systemConfigMock;
18+
private $uploaderMock;
19+
private $object;
20+
private $subject;
21+
22+
public function setUp(): void
23+
{
24+
$this->fbeHelperMock = $this->getMockBuilder(FBEHelper::class)
25+
->disableOriginalConstructor()
26+
->getMock();
27+
$this->systemConfigMock = $this->getMockBuilder(SystemConfig::class)
28+
->disableOriginalConstructor()
29+
->getMock();
30+
$this->uploaderMock = $this->getMockBuilder(Uploader::class)
31+
->disableOriginalConstructor()
32+
->getMock();
33+
34+
$this->object = new ObjectManager($this);
35+
$this->subject = $this->object->getObject(PromotionSyncCron::class, [
36+
'systemConfig' => $this->systemConfigMock,
37+
'uploader' => $this->uploaderMock,
38+
'fbeHelper' => $this->fbeHelperMock
39+
]);
40+
}
41+
42+
public function testExecute(): void
43+
{
44+
$storeId = 1;
45+
$uploadResponse = ['Success' => true];
46+
47+
$storeMock = $this->getMockBuilder(StoreInterface::class)
48+
->disableOriginalConstructor()
49+
->getMockForAbstractClass();
50+
51+
$this->systemConfigMock->expects($this->once())
52+
->method('getAllOnsiteFBEInstalledStores')
53+
->willReturn([$storeMock]);
54+
55+
$storeMock->expects($this->exactly(2))
56+
->method('getId')
57+
->willReturn($storeId);
58+
$this->systemConfigMock->expects($this->once())
59+
->method('isPromotionsSyncEnabled')
60+
->with($storeId)
61+
->willReturn(true);
62+
$this->uploaderMock->expects($this->once())
63+
->method('uploadPromotions')
64+
->with($storeId)
65+
->willReturn($uploadResponse);
66+
67+
$this->subject->execute();
68+
}
69+
}

0 commit comments

Comments
 (0)