Skip to content

Commit

Permalink
Updated unit tests for services.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 17, 2024
1 parent 0f440a7 commit b10131b
Show file tree
Hide file tree
Showing 35 changed files with 1,396 additions and 57 deletions.
10 changes: 5 additions & 5 deletions src/Service/AkismetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(
CacheInterface $cache,
LoggerInterface $logger,
private readonly Security $security,
private readonly RequestStack $stack,
private readonly RequestStack $requestStack,
private readonly TranslatorInterface $translator
) {
parent::__construct($key, $cache, $logger);
Expand Down Expand Up @@ -142,7 +142,7 @@ public function verifyComment(string $content, array $options = []): bool
self::BODY => $body,
]);
if (Response::HTTP_OK !== $response->getStatusCode() || !$this->checkError($response)) {
return true;
return false;
}

return match ($response->getContent()) {
Expand Down Expand Up @@ -176,11 +176,11 @@ protected function getDefaultOptions(): array
private function checkError(ResponseInterface $response): bool
{
$headers = $response->getHeaders();
$code = (int) ($headers['X-akismet-alert-code'][0] ?? 0);
$code = (int) ($headers['x-akismet-alert-code'][0] ?? 0);
if (0 !== $code) {
$message = $this->trans((string) $code);
if ($message === (string) $code) {
$message = $headers['X-akismet-alert-msg'][0] ?? $this->trans('unknown');
$message = $headers['x-akismet-alert-msg'][0] ?? $this->trans('unknown');
}

return $this->setLastError($code, $message);
Expand Down Expand Up @@ -214,7 +214,7 @@ private function doVerifyKey(): bool

private function getCurrentRequest(): ?Request
{
return $this->stack->getCurrentRequest();
return $this->requestStack->getCurrentRequest();
}

private function getVerifyParameters(Request $request, string $content, array $options): array
Expand Down
4 changes: 2 additions & 2 deletions src/Service/AssetVersionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function __construct(
*
* @throws InvalidArgumentException
*/
public function deleteCache(): void
public function deleteCache(): bool
{
$this->cache->delete(self::KEY_IMAGES);
return $this->cache->delete(self::KEY_IMAGES);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Service/CalculationArchiveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private function getDateMin(array $sources): ?\DateTime
*/
private function getSources(bool $useSession): array
{
/** @var CalculationState[] $sources */
/** @psalm-var CalculationState[] $sources */
$sources = $this->stateRepository
->getEditableQueryBuilder()
->getQuery()
Expand All @@ -266,7 +266,7 @@ private function getSources(bool $useSession): array
return $sources;
}

/** @var int[] $ids */
/** @psalm-var int[] $ids */
$ids = $this->getSessionValue(self::KEY_SOURCES, []);
if ([] === $ids) {
return $sources;
Expand Down
1 change: 0 additions & 1 deletion src/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public function count(?string $search, ?string $entity = null): int
}
$result = $this->getArrayResult($search, $entity);

// count
return \count($result);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Calendar/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function testGetToday(): void

$expected = new \DateTime('today');
$actual = $calendar->getToday();
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($expected, $actual->getDate());
}

public function testGetWeek(): void
Expand Down
3 changes: 3 additions & 0 deletions tests/Calendar/CalendarTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
use App\Calendar\Day;
use App\Calendar\Month;
use App\Calendar\Week;
use App\Tests\DateAssertTrait;
use PHPUnit\Framework\TestCase;

abstract class CalendarTestCase extends TestCase
{
use DateAssertTrait;

protected function createCalendar(int $year = 2024): Calendar
{
try {
Expand Down
4 changes: 2 additions & 2 deletions tests/Calendar/DayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetDate(): void
$expected = new \DateTime('2024-01-01');
$day = $this->createDay();
$actual = $day->getDate();
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($expected, $actual);
}

public function testGetDayOfYear(): void
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testGetTimestamp(): void
$day = $this->createDay();
$actual = $day->getTimestamp();
$expected = new \DateTime('2024-01-01');
self::assertSame($expected->getTimestamp(), $actual);
self::assertSameDate($expected, $actual);
}

public function testGetWeek(): void
Expand Down
4 changes: 4 additions & 0 deletions tests/Data/log_invalid_csv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Line 1
Line 2
Line 3
|request|INFO|Matched route "homepage".|{"route":"homepage","route_parameters":{"_route":"homepage","_controller":"App\\Controller\\IndexController::invoke"},"request_uri":"http://127.0.0.1:8000/","method":"GET"}|[]
1 change: 1 addition & 0 deletions tests/Data/log_invalid_json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23.02.2023 16:47:53.967|request|INFO|Matched route "homepage".|"route":"homepage","route_parameters":{"_route":"homepage","_controller":"App\\Controller\\IndexController::invoke"},"request_uri":"http://127.0.0.1:8000/","method":"GET"}|[]
Binary file added tests/Data/public/images/users/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions tests/DateAssertTrait.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;

use PHPUnit\Framework\TestCase;

/**
* Trait to compare dates.
*
* @psalm-require-extends TestCase
*/
trait DateAssertTrait
{
protected static function assertSameDate(\DateTimeInterface $expected, mixed $actual, string $message = ''): void
{
if ($actual instanceof \DateTimeInterface) {
$actual = $actual->getTimestamp();
}
self::assertSame($expected->getTimestamp(), $actual, $message);
}
}
6 changes: 4 additions & 2 deletions tests/Entity/AbstractPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use App\Entity\Category;
use App\Enums\EntityPermission;
use App\Enums\Theme;
use App\Tests\DateAssertTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(AbstractProperty::class)]
class AbstractPropertyTest extends TestCase
{
use DateAssertTrait;
use IdTrait;

/**
Expand Down Expand Up @@ -85,7 +87,7 @@ public function testDate(): void
$actual = $entity->getDate();
// @phpstan-ignore staticMethod.impossibleType
self::assertNotNull($actual);
self::assertSame($date->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($date, $actual);
}

/**
Expand Down Expand Up @@ -133,7 +135,7 @@ public function testValue(): void
$entity->setValue($date);
$actual = $entity->getDate();
self::assertNotNull($actual);
self::assertSame($date->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($date, $actual);

$category = new Category();
self::setId($category, 10);
Expand Down
10 changes: 6 additions & 4 deletions tests/Entity/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace App\Tests\Entity;

use App\Entity\Log;
use App\Tests\DateAssertTrait;
use App\Utils\DateUtils;
use App\Utils\FormatUtils;
use Doctrine\SqlFormatter\NullHighlighter;
Expand All @@ -25,6 +26,8 @@
#[CoversClass(Log::class)]
class LogTest extends TestCase
{
use DateAssertTrait;

public static function getChannelIcons(): \Generator
{
yield ['application', 'fa-fw fa-solid fa-laptop-code'];
Expand Down Expand Up @@ -226,12 +229,11 @@ public function testLevelIcon(string $level, string $expected): void

public function testTimestamp(): void
{
$date = new \DateTime();
$expected = new \DateTime();
$log = new Log();
$log->setCreatedAt($date);
$expected = $date->getTimestamp();
$log->setCreatedAt($expected);
$actual = $log->getTimestamp();
self::assertSame($expected, $actual);
self::assertSameDate($expected, $actual);
}

public function testUser(): void
Expand Down
4 changes: 0 additions & 4 deletions tests/Generator/GeneratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use App\Tests\KernelServiceTestCase;
use App\Utils\StringUtils;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -35,9 +34,6 @@ abstract class GeneratorTestCase extends KernelServiceTestCase
protected EntityManagerInterface $manager;
protected TranslatorInterface $translator;

/**
* @throws Exception
*/
protected function setUp(): void
{
parent::setUp();
Expand Down
6 changes: 4 additions & 2 deletions tests/Model/CalculationArchiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Entity\CalculationState;
use App\Model\AbstractSimulateQuery;
use App\Model\CalculationArchiveQuery;
use App\Tests\DateAssertTrait;
use App\Tests\Entity\IdTrait;
use App\Utils\DateUtils;
use App\Utils\FormatUtils;
Expand All @@ -25,6 +26,7 @@
#[CoversClass(CalculationArchiveQuery::class)]
class CalculationArchiveQueryTest extends TestCase
{
use DateAssertTrait;
use IdTrait;

public function testConstruct(): void
Expand All @@ -45,11 +47,11 @@ public function testDate(): void
{
$date = DateUtils::sub(DateUtils::removeTime(), 'P6M');
$query = new CalculationArchiveQuery();
self::assertSame($date->getTimestamp(), $query->getDate()->getTimestamp());
self::assertSameDate($date, $query->getDate());

$date = DateUtils::sub(DateUtils::removeTime(), 'P6D');
$query->setDate($date);
self::assertSame($date->getTimestamp(), $query->getDate()->getTimestamp());
self::assertSame($date, $query->getDate());

$expected = FormatUtils::formatDate($date);
self::assertSame($expected, $query->getDateFormatted());
Expand Down
10 changes: 6 additions & 4 deletions tests/Model/CalculationUpdateQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use App\Entity\CalculationState;
use App\Model\CalculationUpdateQuery;
use App\Tests\DateAssertTrait;
use App\Tests\Entity\IdTrait;
use App\Utils\DateUtils;
use App\Utils\FormatUtils;
Expand All @@ -26,6 +27,7 @@
#[CoversClass(CalculationUpdateQuery::class)]
class CalculationUpdateQueryTest extends TestCase
{
use DateAssertTrait;
use IdTrait;

/**
Expand All @@ -37,15 +39,15 @@ public function testConstruct(): void

$expected = $this->getDateFrom();
$actual = $query->getDateFrom();
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($expected, $actual);

$expected = FormatUtils::formatDate($actual);
$actual = $query->getDateFromFormatted();
self::assertSame($expected, $actual);

$expected = $this->getDateTo();
$actual = $query->getDateTo();
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($expected, $actual);

$expected = FormatUtils::formatDate($actual);
$actual = $query->getDateToFormatted();
Expand All @@ -62,12 +64,12 @@ public function testSetDates(): void
$expected = $this->getDateFrom();
$query->setDateFrom($expected);
$actual = $query->getDateFrom();
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($expected, $actual);

$expected = $this->getDateTo();
$query->setDateTo($expected);
$actual = $query->getDateTo();
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSameDate($expected, $actual);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Model/SwissPostUpdateResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
namespace App\Tests\Model;

use App\Model\SwissPostUpdateResult;
use App\Tests\DateAssertTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(SwissPostUpdateResult::class)]
class SwissPostUpdateResultTest extends TestCase
{
use DateAssertTrait;

public function testAdd(): void
{
$result = new SwissPostUpdateResult();
Expand Down Expand Up @@ -104,6 +107,6 @@ public function testValidity(): void
$expected = new \DateTime();
$result = new SwissPostUpdateResult();
$result->setValidity($expected);
self::assertSame($expected->getTimestamp(), $result->getValidity()?->getTimestamp());
self::assertSameDate($expected, $result->getValidity());
}
}
8 changes: 5 additions & 3 deletions tests/Repository/CalculationRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Repository\AbstractRepository;
use App\Repository\CalculationRepository;
use App\Tests\DatabaseTrait;
use App\Tests\DateAssertTrait;
use App\Tests\Entity\IdTrait;
use App\Tests\EntityTrait\CalculationTrait;
use App\Tests\EntityTrait\ProductTrait;
Expand All @@ -31,6 +32,7 @@ class CalculationRepositoryTest extends KernelServiceTestCase
{
use CalculationTrait;
use DatabaseTrait;
use DateAssertTrait;
use IdTrait;
use ProductTrait;

Expand Down Expand Up @@ -252,12 +254,12 @@ public function testGetMinMaxDates(): void
$calculation->setDate($date);
$this->addEntity($calculation);

$expected = $calculation->getDate()->getTimestamp();
$expected = $calculation->getDate();

$actual = $this->repository->getMinMaxDates();
self::assertCount(2, $actual);
self::assertSame($expected, $actual[0]?->getTimestamp());
self::assertSame($expected, $actual[1]?->getTimestamp());
self::assertSameDate($expected, $actual[0]);
self::assertSameDate($expected, $actual[1]);
}

public function testGetPivot(): void
Expand Down
Loading

0 comments on commit b10131b

Please sign in to comment.