Skip to content

Commit f97e22b

Browse files
authored
Merge pull request #18 from mage-one-com/develop
Fixes #17 and adds a test for emailService
2 parents 59054db + cacbec6 commit f97e22b

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/app/code/community/Mageone/Qps/Model/Cron.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ class Mageone_Qps_Model_Cron
1717

1818
public function __construct(array $args = [])
1919
{
20+
$this->client = Mage::getModel('qps/http_client_curl');
2021
if (isset($args['client'])) {
2122
$this->client = $args['client'];
2223
}
23-
$this->helper = Mage::helper('qps');
24+
2425
$this->emailService = Mage::getModel('qps/emailService');
26+
if (isset($args['emailService'])) {
27+
$this->emailService = $args['emailService'];
28+
}
29+
30+
$this->helper = Mage::helper('qps');
31+
2532
}
2633

2734
/**
@@ -50,7 +57,7 @@ public function getRules(): void
5057
'message' => $message,
5158
]
5259
);
53-
if ($client->getStatus() !== 200) {
60+
if ($client->getStatus() >= 300) {
5461
Mage::log(
5562
sprintf(
5663
'Something went wrong while trying to update security rules, response: %s',
@@ -82,7 +89,7 @@ public function getRules(): void
8289
$collection->walk('delete');
8390
Mage::app()->cleanCache([Mageone_Qps_Model_Observer::QPS_CACHE_TAG]);
8491
if ($sendNotification === true) {
85-
$this->emailService->sendNotificationEmail($this->helper);
92+
$this->emailService->sendNotificationEmail();
8693
}
8794
}
8895
} catch (Exception $exception) {
@@ -96,7 +103,7 @@ public function getRules(): void
96103
*/
97104
private function getClient()
98105
{
99-
return $this->client ?: new Mageone_Qps_Model_HTTP_Client_Curl();
106+
return $this->client;
100107
}
101108

102109
/**

src/app/code/community/Mageone/Qps/Model/EmailService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

3-
declare(strict_types = 1);
3+
declare(strict_types=1);
44

55
class Mageone_Qps_Model_EmailService
66
{
77

8-
public function sendNotificationEmail(Mageone_Qps_Helper_Data $helper): void
8+
public function sendNotificationEmail(): void
99
{
10+
$helper = Mage::helper('qps');
1011
if ($helper->isNotificationEnabled() === false) {
1112
return;
1213
}

tests/Integration/RuleUpdateTest.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ class RuleUpdateTest extends AbstractTest
4545
* @var Mageone_Qps_Model_SecService
4646
*/
4747
private $secService;
48+
/**
49+
* @var \Mageone_Qps_Model_EmailService|MockObject
50+
*/
51+
private $emailServiceMock;
4852

4953
protected function setUp(): void
5054
{
5155
parent::setUp();
52-
$this->clientMock = $this->createMock(Mage_HTTP_IClient::class);
53-
$this->cron = Mage::getModel('qps/cron', ['client' => $this->clientMock]);
54-
$this->secService = Mage::getModel('qps/secService');
56+
$this->clientMock = $this->createMock(\Mage_HTTP_IClient::class);
57+
$this->emailServiceMock = $this->createMock(\Mageone_Qps_Model_EmailService::class);
58+
$this->cron = Mage::getModel(
59+
'qps/cron',
60+
['client' => $this->clientMock, 'emailService' => $this->emailServiceMock]
61+
);
62+
$this->secService = Mage::getModel('qps/secService');
5563

5664
$this->helperMock->method('getUsername')->willReturn(self::EXAMPLE_USER);
5765
$this->helperMock->method('getResourceUrl')->willReturn(self::RESOURCE_URL);
@@ -113,6 +121,37 @@ public function testClientReturns200(): void
113121
$this->cron->getRules();
114122
}
115123

124+
public function testClientReturns100(): void
125+
{
126+
$secService = $this->secService;
127+
$validatePostData = (static function ($postData) use ($secService) {
128+
if ($postData['user'] !== self::EXAMPLE_USER) {
129+
return false;
130+
}
131+
$decrypt = $secService->decryptMessage($postData['message']);
132+
try {
133+
$message = json_decode($decrypt, true, 512, JSON_THROW_ON_ERROR);
134+
135+
return $message['magento_version'] === Mage::getVersion()
136+
&& $message['patches_list'] === ['test-patch' => 'TEST patch'];
137+
} catch (Exception $e) {
138+
return false;
139+
}
140+
});
141+
142+
$this->clientMock
143+
->expects($this->once())
144+
->method('post')
145+
->with(self::RESOURCE_URL, $this->callback($validatePostData));
146+
147+
$this->clientMock
148+
->expects($this->once())
149+
->method('getStatus')
150+
->willReturn(100);
151+
152+
$this->cron->getRules();
153+
}
154+
116155
public function testClientReturnsNo200(): void
117156
{
118157
$this->clientMock
@@ -184,8 +223,10 @@ public function testRulesAreAutoEnabled($rules): void
184223
*
185224
* @param string[]
186225
*/
187-
public function testWriteNewRules($rules): void
226+
public function testWriteNewRulesAndCallEmailService($rules): void
188227
{
228+
$this->emailServiceMock->expects($this->once())->method('sendNotificationEmail');
229+
189230
$this->clientMock
190231
->method('getStatus')
191232
->willReturn(200);

0 commit comments

Comments
 (0)