Skip to content

Commit

Permalink
Added unit tests for Captcha and HTML.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 11, 2024
1 parent cf439d8 commit 05ad25a
Show file tree
Hide file tree
Showing 24 changed files with 937 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Ce programme est distribué avec une [licence propriétaire](LICENSE.md) et une
[![PHP](https://img.shields.io/badge/PHP-8.2.19-informational?logo=php)](https://www.php.net)
[![MySQL](https://img.shields.io/badge/MySQL-5.7.32-informational?logo=mysql)](https://www.mysql.com)
[![Apache](https://img.shields.io/badge/Apache-2.4.51-informational?logo=apache)](https://httpd.apache.org)
[![PhpStorm](https://img.shields.io/badge/PhpStorm-2024.1.2-informational?logo=phpstorm)](https://www.jetbrains.com/phpstorm)
[![PhpStorm](https://img.shields.io/badge/PhpStorm-2024.1.3-informational?logo=phpstorm)](https://www.jetbrains.com/phpstorm)

## Qualité du code

Expand Down
8 changes: 8 additions & 0 deletions src/Captcha/AbstractAlphaCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ protected function getRandomWord(): string
{
return $this->dictionary->getRandomWord();
}

/**
* Translates the given message with the 'captcha' domain.
*/
protected function transCaptcha(string $id, array $parameters = []): string
{
return $this->trans($id, $parameters, 'captcha');
}
}
5 changes: 0 additions & 5 deletions src/Captcha/AlphaCaptchaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,4 @@ public function checkAnswer(string $givenAnswer, string $expectedAnswer): bool;
* @return string[] the question and the answer
*/
public function getChallenge(): array;

/**
* Gets the default index name.
*/
public static function getDefaultIndexName(): string;
}
16 changes: 4 additions & 12 deletions src/Captcha/ConsonantCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ class ConsonantCaptcha extends AbstractAlphaCaptcha
'-1' => 'last',
];

/**
* Gets the default index name.
*/
public static function getDefaultIndexName(): string
{
return 'ConsonantCaptcha';
}

protected function getAnswer(string $word, int $letterIndex): string
{
return $this->findAnswer($word, $letterIndex, self::CONSONANT);
Expand All @@ -46,12 +38,12 @@ protected function getLetterIndex(): int

protected function getQuestion(string $word, int $letterIndex): string
{
$params = [
'%index%' => $this->trans(self::INDEX_MAPPING[$letterIndex], [], 'captcha'),
'%letter%' => $this->trans('consonant', [], 'captcha'),
$parameters = [
'%index%' => $this->transCaptcha(self::INDEX_MAPPING[$letterIndex]),
'%letter%' => $this->transCaptcha('consonant'),
'%word%' => $word,
];

return $this->trans('sentence', $params, 'captcha');
return $this->transCaptcha('sentence', $parameters);
}
}
16 changes: 4 additions & 12 deletions src/Captcha/LetterCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ class LetterCaptcha extends AbstractAlphaCaptcha
'-1' => 'last',
];

/**
* Gets the default index name.
*/
public static function getDefaultIndexName(): string
{
return 'LetterCaptcha';
}

protected function getAnswer(string $word, int $letterIndex): string
{
if (0 > $letterIndex) {
Expand All @@ -51,12 +43,12 @@ protected function getLetterIndex(): int

protected function getQuestion(string $word, int $letterIndex): string
{
$params = [
'%index%' => $this->trans(self::INDEX_MAPPING[$letterIndex], [], 'captcha'),
'%letter%' => $this->trans('letter', [], 'captcha'),
$parameters = [
'%index%' => $this->transCaptcha(self::INDEX_MAPPING[$letterIndex]),
'%letter%' => $this->transCaptcha('letter'),
'%word%' => $word,
];

return $this->trans('sentence', $params, 'captcha');
return $this->transCaptcha('sentence', $parameters);
}
}
16 changes: 4 additions & 12 deletions src/Captcha/VowelCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ class VowelCaptcha extends AbstractAlphaCaptcha

private const VOWEL = 'AEIOUY';

/**
* Gets the default index name.
*/
public static function getDefaultIndexName(): string
{
return 'VowelCaptcha';
}

protected function getAnswer(string $word, int $letterIndex): string
{
return $this->findAnswer($word, $letterIndex, self::VOWEL);
Expand All @@ -46,12 +38,12 @@ protected function getLetterIndex(): int

protected function getQuestion(string $word, int $letterIndex): string
{
$params = [
'%index%' => $this->trans(self::INDEX_MAPPING[$letterIndex], [], 'captcha'),
'%letter%' => $this->trans('vowel', [], 'captcha'),
$parameters = [
'%index%' => $this->transCaptcha(self::INDEX_MAPPING[$letterIndex]),
'%letter%' => $this->transCaptcha('vowel'),
'%word%' => $word,
];

return $this->trans('sentence', $params, 'captcha');
return $this->transCaptcha('sentence', $parameters);
}
}
2 changes: 2 additions & 0 deletions src/Pdf/Html/HtmlOlChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function getType(): HtmlListType

/**
* Sets the start counting (must be positive).
*
* @psalm-param positive-int $start
*/
public function setStart(int $start): self
{
Expand Down
1 change: 1 addition & 0 deletions src/Pdf/Html/HtmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private function createLiChunk(string $name, HtmlParentChunk $parent, ?string $c
private function createOlChunk(string $name, HtmlParentChunk $parent, ?string $className, \DOMNode $node): HtmlOlChunk
{
$type = HtmlAttribute::LIST_TYPE->getEnumValue($node, HtmlListType::NUMBER);
/** @psalm-var positive-int $start */
$start = HtmlAttribute::LIST_START->getIntValue($node, 1);
$chunk = new HtmlOlChunk($name, $parent, $className);

Expand Down
19 changes: 19 additions & 0 deletions src/Pdf/Html/HtmlStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ public function __construct()
$this->setBorder(PdfBorder::none());
}

/**
* Gets the default style.
*
* The style has the following properties:
*
* - Font: Arial 9 points Regular.
* - Line width: 0.2 mm.
* - Fill color: White.
* - Draw color: Black.
* - Text color: Black.
* - Border: None.
* - Text Alignment: Left
* - Margins: 0.0
*/
public static function default(): self
{
return new self();
}

/**
* Gets the alignment.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Pdf/Traits/PdfColorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ public function asInt(): int

/**
* @return array{0: int, 1: int, 2: int}
*
* @psalm-return array{0: int<0, 255>, 1: int<0, 255>, 2: int<0, 255>}
*/
public function asRGB(): array
{
$hex = $this->getPhpOfficeColor();

/** @psalm-var array{0: int<0, 255>, 1: int<0, 255>, 2: int<0, 255>} */
return [
$this->hex2Dec(\substr($hex, 0, 2)),
$this->hex2Dec(\substr($hex, 2, 2)),
Expand Down
61 changes: 61 additions & 0 deletions tests/Captcha/AlphaCaptchaTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Captcha;

use App\Captcha\AbstractAlphaCaptcha;
use App\Service\DictionaryService;
use App\Tests\TranslatorMockTrait;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @template TCaptcha as AbstractAlphaCaptcha
*/
abstract class AlphaCaptchaTestCase extends TestCase
{
use TranslatorMockTrait;

protected MockObject&DictionaryService $service;
protected MockObject&TranslatorInterface $translator;

/**
* @throws Exception
*/
protected function setUp(): void
{
$letters = \implode('', \range('A', 'Z'));
$this->translator = $this->createTranslator();
$this->service = $this->createMock(DictionaryService::class);
$this->service->expects(self::any())
->method('getRandomWord')
->willReturn($letters);
}

public function testCheckAnswer(): void
{
$captcha = $this->createCaptcha($this->service);
$captcha->setTranslator($this->translator);

$challenge = $captcha->getChallenge();
self::assertCount(2, $challenge);
$actual = $captcha->checkAnswer($challenge[1], $challenge[1]);
self::assertTrue($actual);
}

/**
* @psalm-return TCaptcha
*/
abstract protected function createCaptcha(DictionaryService $service): AbstractAlphaCaptcha;
}
31 changes: 31 additions & 0 deletions tests/Captcha/ConsonantCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Captcha;

use App\Captcha\AbstractAlphaCaptcha;
use App\Captcha\ConsonantCaptcha;
use App\Service\DictionaryService;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends AlphaCaptchaTestCase<ConsonantCaptcha>
*/
#[CoversClass(ConsonantCaptcha::class)]
#[CoversClass(AbstractAlphaCaptcha::class)]
class ConsonantCaptchaTest extends AlphaCaptchaTestCase
{
protected function createCaptcha(DictionaryService $service): ConsonantCaptcha
{
return new ConsonantCaptcha($service);
}
}
31 changes: 31 additions & 0 deletions tests/Captcha/LetterCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Captcha;

use App\Captcha\AbstractAlphaCaptcha;
use App\Captcha\LetterCaptcha;
use App\Service\DictionaryService;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends AlphaCaptchaTestCase<LetterCaptcha>
*/
#[CoversClass(LetterCaptcha::class)]
#[CoversClass(AbstractAlphaCaptcha::class)]
class LetterCaptchaTest extends AlphaCaptchaTestCase
{
protected function createCaptcha(DictionaryService $service): LetterCaptcha
{
return new LetterCaptcha($service);
}
}
31 changes: 31 additions & 0 deletions tests/Captcha/VowelCaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Calculation package.
*
* (c) bibi.nu <bibi@bibi.nu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Tests\Captcha;

use App\Captcha\AbstractAlphaCaptcha;
use App\Captcha\VowelCaptcha;
use App\Service\DictionaryService;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends AlphaCaptchaTestCase<VowelCaptcha>
*/
#[CoversClass(VowelCaptcha::class)]
#[CoversClass(AbstractAlphaCaptcha::class)]
class VowelCaptchaTest extends AlphaCaptchaTestCase
{
protected function createCaptcha(DictionaryService $service): VowelCaptcha
{
return new VowelCaptcha($service);
}
}

0 comments on commit 05ad25a

Please sign in to comment.