Skip to content

Commit d9c64a9

Browse files
committed
use contructor injection for email and spam services
1 parent ee0f17b commit d9c64a9

File tree

2 files changed

+43
-74
lines changed

2 files changed

+43
-74
lines changed

src/controllers/ContactController.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,27 @@ class ContactController extends BaseApiController
1515
*/
1616
private $spamCheckService;
1717

18-
public function handle(Request $request, PDO $db)
19-
{
20-
// really need to not require this to be declared
21-
}
22-
2318
/**
24-
* @param ContactEmailService $emailService
19+
* ContactController constructor.
2520
*
26-
* @return $this
21+
* @param ContactEmailService $emailService
22+
* @param SpamCheckServiceInterface $spamCheckService
23+
* @param array $config
2724
*/
28-
public function setEmailService(ContactEmailService $emailService)
29-
{
25+
public function __construct(
26+
ContactEmailService $emailService,
27+
SpamCheckServiceInterface $spamCheckService,
28+
array $config = []
29+
) {
3030
$this->emailService = $emailService;
31+
$this->spamCheckService = $spamCheckService;
3132

32-
return $this;
33+
parent::__construct($config);
3334
}
3435

35-
/**
36-
* @param SpamCheckService $spamCheckService
37-
*
38-
* @return $this
39-
*/
40-
public function setSpamCheckService(SpamCheckService $spamCheckService = null)
36+
public function handle(Request $request, PDO $db)
4137
{
42-
$this->spamCheckService = $spamCheckService;
43-
44-
return $this;
38+
// really need to not require this to be declared
4539
}
4640

4741
/**
@@ -95,17 +89,14 @@ public function contact(Request $request, PDO $db)
9589
throw new Exception($message, 400);
9690
}
9791

98-
// run it by akismet if we have it
99-
if (isset($this->spamCheckService)) {
100-
$isValid = $this->spamCheckService->isCommentAcceptable(
101-
$data,
102-
$request->getClientIP(),
103-
$request->getClientUserAgent()
104-
);
92+
$isValid = $this->spamCheckService->isCommentAcceptable(
93+
$data,
94+
$request->getClientIP(),
95+
$request->getClientUserAgent()
96+
);
10597

106-
if (!$isValid) {
107-
throw new Exception("Comment failed spam check", 400);
108-
}
98+
if (!$isValid) {
99+
throw new Exception("Comment failed spam check", 400);
109100
}
110101

111102
$this->emailService->sendEmail($data);

tests/controllers/ContactControllerTest.php

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,27 @@ class ContactControllerTest extends TestCase
1010
* @dataProvider dataProvider
1111
* @test
1212
*
13+
* @param bool $isClientPermittedPasswordGrant
14+
* @param array $returnValueMap
15+
* @param bool $isCommentAcceptable
16+
* @param null $expectedException
17+
* @param null $expectedExceptionMessage
18+
* @param bool $spamShouldBeChecked
19+
* @param bool $emailShouldBeSent
20+
*
21+
* @throws \Exception
1322
*/
1423
public function contactWorksAsExpected(
1524
$isClientPermittedPasswordGrant,
1625
array $returnValueMap = [],
1726
$isCommentAcceptable = false,
18-
$spamCheckServiceIsSet = false,
19-
$emailServiceIsSet = false,
2027
$expectedException = null,
2128
$expectedExceptionMessage = null,
29+
$spamShouldBeChecked = false,
2230
$emailShouldBeSent = false
2331
) {
2432
$request = $this->getMockBuilder('\Request')->disableOriginalConstructor()->getMock();
2533

26-
$contactController = new \ContactController();
27-
2834
$oauthModel = $this->getMockBuilder('\OAuthModel')->disableOriginalConstructor()->getMock();
2935
$oauthModel->expects($this->once())->method('isClientPermittedPasswordGrant')->willReturn($isClientPermittedPasswordGrant);
3036
$request->expects($this->once())->method('getOauthModel')->willReturn($oauthModel);
@@ -37,26 +43,22 @@ public function contactWorksAsExpected(
3743

3844
$db = $this->getMockBuilder('\PDO')->disableOriginalConstructor()->getMock();
3945

40-
if ($spamCheckServiceIsSet) {
41-
$spamCheckService = $this->getMockBuilder(\SpamCheckService::class)
42-
->disableOriginalConstructor()
43-
->getMock();
46+
$spamCheckService = $this->getMockBuilder(\SpamCheckServiceInterface::class)
47+
->disableOriginalConstructor()
48+
->getMock();
4449

50+
if ($spamShouldBeChecked) {
4551
$spamCheckService
4652
->expects($this->once())
4753
->method('isCommentAcceptable')
4854
->willReturn($isCommentAcceptable);
49-
50-
$contactController->setSpamCheckService($spamCheckService);
5155
}
5256

53-
if ($emailServiceIsSet) {
54-
$emailService = $this->getMockBuilder(\ContactEmailService::class)
55-
->disableOriginalConstructor()
56-
->getMock();
57+
$emailService = $this->getMockBuilder(\ContactEmailService::class)
58+
->disableOriginalConstructor()
59+
->getMock();
5760

58-
$contactController->setEmailService($emailService);
59-
}
61+
$contactController = new \ContactController($emailService, $spamCheckService);
6062

6163
if (null !== $expectedException) {
6264
$this->expectException($expectedException);
@@ -102,11 +104,10 @@ public function dataProvider()
102104
'isClientPermittedPasswordGrant' => false,
103105
'returnValueMap' => [],
104106
'isCommentAcceptable' => false,
105-
'spamCheckServiceIsSet' => false,
106-
'emailCheckServiceIsSet' => false,
107107
'exceptedException' => 'Exception',
108108
'expectedExceptionMessage' => 'This client cannot perform this action',
109-
'emailShouldBeSent' => false
109+
'emailShouldBeSent' => false,
110+
'spamShouldBeChecked' => false
110111
],
111112
//Not all required fields are set
112113
[
@@ -120,8 +121,6 @@ public function dataProvider()
120121
['comment', '', '']
121122
],
122123
'isCommentAcceptable' => false,
123-
'spamCheckServiceIsSet' => false,
124-
'emailCheckServiceIsSet' => true,
125124
'exceptedException' => \Exception::class,
126125
'expectedExceptionMessage' => "The fields 'name', 'email', 'subject', 'comment' are required",
127126
],
@@ -138,28 +137,9 @@ public function dataProvider()
138137
],
139138

140139
'isCommentAcceptable' => false,
141-
'spamCheckServiceIsSet' => true,
142-
'emailCheckServiceIsSet' => true,
143140
'exceptedException' => \Exception::class,
144141
'expectedExceptionMessage' => 'Comment failed spam check',
145-
],
146-
//EmailService check throws an exception
147-
[
148-
'isClientPermittedPasswordGrant' => true,
149-
'returnValueMap' => [
150-
['client_id', '', 'client_id'],
151-
['client_secret', '', 'client_secret'],
152-
['name', '', 'name'],
153-
['email', '', 'email'],
154-
['subject', '', 'subject'],
155-
['comment', '', 'comment']
156-
],
157-
'isCommentAcceptable' => true,
158-
'spamCheckServiceIsSet' => false,
159-
'emailCheckServiceIsSet' => false,
160-
'exceptedException' => \RuntimeException::class,
161-
'expectedExceptionMessage' => 'The emailservice has not been set',
162-
'emailShouldBeSent' => false
142+
'spamShouldBeChecked' => true,
163143
],
164144
//Email is sent without spamcheck
165145
[
@@ -173,10 +153,9 @@ public function dataProvider()
173153
['comment', '', 'comment']
174154
],
175155
'isCommentAcceptable' => true,
176-
'spamCheckServiceIsSet' => false,
177-
'emailCheckServiceIsSet' => true,
178156
'exceptedException' => null,
179157
'expectedExceptionMessage' => null,
158+
'spamShouldBeChecked' => true,
180159
'emailShouldBeSent' => true
181160
],
182161
//All is good email should be sent
@@ -191,10 +170,9 @@ public function dataProvider()
191170
['comment', '', 'comment']
192171
],
193172
'isCommentAcceptable' => true,
194-
'spamCheckServiceIsSet' => true,
195-
'emailCheckServiceIsSet' => true,
196173
'exceptedException' => null,
197174
'expectedExceptionMessage' => null,
175+
'spamShouldBeChecked' => true,
198176
'emailShouldBeSent' => true
199177
]
200178
];

0 commit comments

Comments
 (0)