Skip to content

Commit

Permalink
Fix PHP-CS-Fixer cache ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed May 25, 2023
1 parent a351f17 commit bbf51ff
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ vendor/.git
node_modules
vendor
diff.txt
.php_cs.cache
.php-cs-fixer.cache
7 changes: 1 addition & 6 deletions src/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,9 @@ public function contact(Request $request, PDO $db): void
throw new Exception($message, Http::BAD_REQUEST);
}

$clientIP = $request->getClientIP();
$userAgent = $request->getClientUserAgent();

if (
! is_string($data['comment'])
|| ! is_string($clientIP)
|| ! is_string($userAgent)
|| !$this->spamCheckService->isCommentAcceptable($data['comment'], $clientIP, $userAgent)
|| !$this->spamCheckService->isCommentAcceptable($data['comment'], $request->getClientIP(), $request->getClientUserAgent())
) {
throw new Exception("Comment failed spam check", Http::BAD_REQUEST);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Service/NullSpamCheckService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class NullSpamCheckService implements SpamCheckServiceInterface
* Check your comment against the spam check service
*
* @param string $comment
* @param string $userIp
* @param string $userAgent
* @param string|null $userIp
* @param string|null $userAgent
*
* @return bool true if the comment is okay, false if it got rated as spam
*/
public function isCommentAcceptable(string $comment, string $userIp, string $userAgent): bool
public function isCommentAcceptable(string $comment, ?string $userIp, ?string $userAgent): bool
{
return true;
}
Expand Down
13 changes: 9 additions & 4 deletions src/Service/SpamCheckService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ public function __construct(ClientInterface $httpClient, $apiKey, $blog)
/**
* Check your comment against the spam check service
*
* @see https://akismet.crom/development/api/#comment-check
* @see https://akismet.com/development/api/#comment-check
*
* @param string $comment
* @param string $userIp
* @param string $userAgent
* @param string|null $userIp
* @param string|null $userAgent
*
* @return bool true if the comment is okay, false if it got rated as spam
*/
public function isCommentAcceptable(string $comment, string $userIp, string $userAgent)
public function isCommentAcceptable(string $comment, ?string $userIp, ?string $userAgent): bool
{
if (null === $userIp) {
// IP address is a required field: https://akismet.com/comment-check/
return false;
}

if ('' === trim($comment)) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Service/SpamCheckServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ interface SpamCheckServiceInterface
* Check your comment against the spam check service
*
* @param string $comment
* @param string $userIp
* @param string $userAgent
* @param string|null $userIp
* @param string|null $userAgent
*
* @return bool true if the comment is okay, false if it got rated as spam
*/
public function isCommentAcceptable(string $comment, string $userIp, string $userAgent);
public function isCommentAcceptable(string $comment, ?string $userIp, ?string $userAgent): bool;
}
35 changes: 15 additions & 20 deletions tests/Controller/ContactControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ final class ContactControllerTest extends TestCase
*
* @param bool $isClientPermittedPasswordGrant
* @param array $returnValueMap
* @param bool $isCommentAcceptable
* @param null|class-string<\Throwable> $expectedException
* @param null|string $expectedExceptionMessage
* @param bool $spamShouldBeChecked
* @param bool $emailShouldBeSent
* @param bool $isCommentAcceptable
* @param class-string<\Throwable>|null $expectedException
* @param string|null $expectedExceptionMessage
* @param bool $spamShouldBeChecked
* @param bool $emailShouldBeSent
*
* @throws Exception
*/
public function testContactWorksAsExpected(
bool $isClientPermittedPasswordGrant,
array $returnValueMap = [],
$isCommentAcceptable = false,
$expectedException = null,
$expectedExceptionMessage = null,
$spamShouldBeChecked = false,
$emailShouldBeSent = false
bool $isCommentAcceptable = false,
string $expectedException = null,
string $expectedExceptionMessage = null,
bool $spamShouldBeChecked = false,
bool $emailShouldBeSent = false
): void {
$request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock();

Expand Down Expand Up @@ -105,8 +105,7 @@ public function testContactWorksAsExpected(
public function dataProvider(): array
{
return [
//Client cannot use the contactform
[
'Client cannot use the contactform' => [
'isClientPermittedPasswordGrant' => false,
'returnValueMap' => [],
'isCommentAcceptable' => false,
Expand All @@ -115,8 +114,7 @@ public function dataProvider(): array
'emailShouldBeSent' => false,
'spamShouldBeChecked' => false
],
//Not all required fields are set
[
'Not all required fields are set' => [
'isClientPermittedPasswordGrant' => true,
'returnValueMap' => [
['client_id', '', 'client_id'],
Expand All @@ -130,8 +128,7 @@ public function dataProvider(): array
'exceptedException' => Exception::class,
'expectedExceptionMessage' => "The fields 'name', 'email', 'subject', 'comment' are required",
],
//Spamcheck fails
[
'Spamcheck fails' => [
'isClientPermittedPasswordGrant' => true,
'returnValueMap' => [
['client_id', '', 'client_id'],
Expand All @@ -147,8 +144,7 @@ public function dataProvider(): array
'expectedExceptionMessage' => 'Comment failed spam check',
'spamShouldBeChecked' => true,
],
//Email is sent without spamcheck
[
'Email is sent without spamcheck' => [
'isClientPermittedPasswordGrant' => true,
'returnValueMap' => [
['client_id', '', 'client_id'],
Expand All @@ -164,8 +160,7 @@ public function dataProvider(): array
'spamShouldBeChecked' => true,
'emailShouldBeSent' => true
],
//All is good email should be sent
[
'All is good email should be sent' => [
'isClientPermittedPasswordGrant' => true,
'returnValueMap' => [
['client_id', '', 'client_id'],
Expand Down

0 comments on commit bbf51ff

Please sign in to comment.