Last commit july 5th

This commit is contained in:
2024-07-05 13:46:23 +02:00
parent dad0d86e8c
commit b0e4dfbb76
24982 changed files with 2621219 additions and 413 deletions

54
tests/SpamCheckerTest.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
namespace App\Tests;
use App\Entity\Comment;
use App\SpamChecker;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\ResponseInterface;
class SpamCheckerTest extends TestCase
{
public function testSpamScoreWithInvalidRequest(): void
{
$comment = new Comment();
$comment->setCreatedAtValue();
$context = [];
$client = new MockHttpClient([new MockResponse('invalid', ['response_headers' => ['x-akismet-debug-help: Invalid key']])]);
$checker = new SpamChecker($client, 'abcde');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to check for spam: invalid (Invalid key).');
$checker->getSpamScore($comment, $context);
}
/**
* @dataProvider provideComments
*/
public function testSpamScore(int $expectedScore, ResponseInterface $response, Comment $comment, array $context)
{
$client = new MockHttpClient([$response]);
$checker = new SpamChecker($client, 'abcde');
$score = $checker->getSpamScore($comment, $context);
$this->assertSame($expectedScore, $score);
}
public static function provideComments(): iterable
{
$comment = new Comment();
$comment->setCreatedAtValue();
$context = [];
$response = new MockResponse('', ['response_headers' => ['x-akismet-pro-tip: discard']]);
yield 'blatant_spam' => [2, $response, $comment, $context];
$response = new MockResponse('true');
yield 'spam' => [1, $response, $comment, $context];
$response = new MockResponse('false');
yield 'ham' => [0, $response, $comment, $context];
}
}