Added additional tests

This commit is contained in:
Jan Böhmer 2026-05-11 20:28:40 +02:00
parent cb669ad4ec
commit 65a6f46369
12 changed files with 2392 additions and 1441 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\Cache;
use App\Entity\Parts\Part;
use App\Services\Cache\ElementCacheTagGenerator;
use PHPUnit\Framework\TestCase;
final class ElementCacheTagGeneratorTest extends TestCase
{
private ElementCacheTagGenerator $service;
protected function setUp(): void
{
$this->service = new ElementCacheTagGenerator();
}
public function testClassNameIsConvertedToTag(): void
{
$tag = $this->service->getElementTypeCacheTag(Part::class);
// Backslashes must be replaced by underscores
$this->assertStringNotContainsString('\\', $tag);
$this->assertSame(str_replace('\\', '_', Part::class), $tag);
}
public function testObjectInputGivesSameResultAsClassName(): void
{
$part = new Part();
$tagFromObject = $this->service->getElementTypeCacheTag($part);
$tagFromClass = $this->service->getElementTypeCacheTag(Part::class);
$this->assertSame($tagFromClass, $tagFromObject);
}
public function testResultIsCached(): void
{
$tag1 = $this->service->getElementTypeCacheTag(Part::class);
$tag2 = $this->service->getElementTypeCacheTag(Part::class);
$this->assertSame($tag1, $tag2);
}
public function testNonExistentClassThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->service->getElementTypeCacheTag('App\\NonExistent\\Foo');
}
}

View file

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\Formatters;
use App\Services\Formatters\MarkdownParser;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Translation\TranslatorInterface;
final class MarkdownParserTest extends TestCase
{
private MarkdownParser $service;
protected function setUp(): void
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->method('trans')->willReturn('Loading...');
$this->service = new MarkdownParser($translator);
}
public function testOutputContainsDataMarkdownAttribute(): void
{
$result = $this->service->markForRendering('**hello**');
$this->assertStringContainsString('data-markdown=', $result);
$this->assertStringContainsString('data-controller="common--markdown"', $result);
}
public function testMarkdownContentIsHtmlescapedInAttribute(): void
{
$result = $this->service->markForRendering('<script>alert(1)</script>');
// The raw < should not appear unescaped inside the attribute
$this->assertStringNotContainsString('<script>', $result);
$this->assertStringContainsString('&lt;script&gt;', $result);
}
public function testInlineModeAddsInlineClass(): void
{
$result = $this->service->markForRendering('text', true);
$this->assertStringContainsString('markdown-inline', $result);
}
public function testNonInlineModeDoesNotAddInlineClass(): void
{
$result = $this->service->markForRendering('text', false);
$this->assertStringNotContainsString('markdown-inline', $result);
}
public function testOutputIsWrappedInDiv(): void
{
$result = $this->service->markForRendering('test');
$this->assertStringStartsWith('<div', $result);
$this->assertStringEndsWith('</div>', $result);
}
public function testTranslatorIsCalledForLoadingText(): void
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->once())
->method('trans')
->with('markdown.loading')
->willReturn('Loading...');
$service = new MarkdownParser($translator);
$service->markForRendering('test');
}
}

View file

@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\Formatters;
use App\Entity\PriceInformations\Currency;
use App\Services\Formatters\MoneyFormatter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class MoneyFormatterTest extends WebTestCase
{
private static MoneyFormatter $service;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$service = self::getContainer()->get(MoneyFormatter::class);
}
public function testFormatWithFloatInput(): void
{
$currency = new Currency();
$currency->setIsoCode('USD');
$result = self::$service->format(1.5, $currency);
$this->assertSame('$ 1.50', $result);
}
public function testFormatWithNullCurrencyUsesBaseCurrency(): void
{
$result = self::$service->format(1.5);
// Should return a non-empty formatted string
$this->assertNotEmpty($result);
$this->assertIsString($result);
}
public function testFormatWithExplicitCurrencyUsesThatCurrency(): void
{
$currency = new Currency();
$currency->setIsoCode('USD');
$result = self::$service->format(10.0, $currency);
$this->assertNotEmpty($result);
$this->assertStringContainsString('10', $result);
}
public function testFormatStringInputWorksSameAsFloat(): void
{
$resultFloat = self::$service->format(1.5);
$resultString = self::$service->format('1.5');
$this->assertSame($resultFloat, $resultString);
}
public function testShowAllDigitsRespectsFractionCount(): void
{
// With show_all_digits = true and decimals = 3, we expect exactly 3 decimal places
$result = self::$service->format(1.5, null, 3, true);
// The number should contain exactly 3 decimal digits
$this->assertMatchesRegularExpression('/\d{3}(?!\d)/', $result);
}
public function testZeroIsFormattedCorrectly(): void
{
$result = self::$service->format(0.0);
$this->assertNotEmpty($result);
$this->assertStringContainsString('0', $result);
}
public function testCurrencyWithEmptyIsoCodeFallsBackToBaseCurrency(): void
{
$currency = new Currency();
// Empty ISO code → should fall back to base currency
$resultWithEmpty = self::$service->format(1.0, $currency);
$resultWithNull = self::$service->format(1.0, null);
$this->assertSame($resultWithNull, $resultWithEmpty);
}
}

View file

@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LogSystem;
use App\Services\LogSystem\LogDiffFormatter;
use PHPUnit\Framework\TestCase;
final class LogDiffFormatterTest extends TestCase
{
private LogDiffFormatter $service;
protected function setUp(): void
{
$this->service = new LogDiffFormatter();
}
public function testPositiveNumericDiff(): void
{
$result = $this->service->formatDiff(1, 6);
$this->assertStringContainsString('text-success', $result);
$this->assertStringContainsString('+5', $result);
}
public function testNegativeNumericDiff(): void
{
$result = $this->service->formatDiff(10, 3);
$this->assertStringContainsString('text-danger', $result);
$this->assertStringContainsString('-7', $result);
}
public function testZeroNumericDiff(): void
{
$result = $this->service->formatDiff(5, 5);
$this->assertStringContainsString('text-muted', $result);
$this->assertStringContainsString('0', $result);
}
public function testStringDiffReturnsNonEmptyHtml(): void
{
$result = $this->service->formatDiff('hello world', 'hello PHP');
$this->assertNotEmpty($result);
// DiffHelper returns HTML
$this->assertStringContainsString('<', $result);
}
public function testUnsupportedTypesReturnEmptyString(): void
{
// booleans are neither string nor numeric → empty
$result = $this->service->formatDiff(true, false);
$this->assertSame('', $result);
}
public function testFloatDiff(): void
{
$result = $this->service->formatDiff(1.5, 3.0);
$this->assertStringContainsString('text-success', $result);
}
}

View file

@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LogSystem;
use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\LogSystem\UserLogoutLogEntry;
use App\Services\LogSystem\LogEntryExtraFormatter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class LogEntryExtraFormatterTest extends WebTestCase
{
private static LogEntryExtraFormatter $service;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$service = self::getContainer()->get(LogEntryExtraFormatter::class);
}
public function testFormatUserLoginLogEntryContainsIp(): void
{
$entry = new UserLoginLogEntry('127.0.0.1', anonymize: false);
$result = self::$service->format($entry);
$this->assertNotEmpty($result);
$this->assertStringContainsString('127.0.0.1', $result);
}
public function testFormatDatabaseUpdatedLogEntryContainsVersions(): void
{
$entry = new DatabaseUpdatedLogEntry('1.0.0', '2.0.0');
$result = self::$service->format($entry);
$this->assertStringContainsString('1.0.0', $result);
$this->assertStringContainsString('2.0.0', $result);
}
public function testFormatUserLogoutContainsIp(): void
{
$entry = new UserLogoutLogEntry('10.0.0.1', anonymize: false);
$result = self::$service->format($entry);
$this->assertNotEmpty($result);
$this->assertStringContainsString('10.0.0.1', $result);
}
public function testFormatConsoleReplacesHtmlTags(): void
{
$entry = new DatabaseUpdatedLogEntry('1.0', '2.0');
$result = self::$service->formatConsole($entry);
// Console format replaces the arrow icon with →
$this->assertStringContainsString('→', $result);
// No raw HTML tags should remain from the arrow icon
$this->assertStringNotContainsString('<i class="fas fa-long-arrow-alt-right"></i>', $result);
}
public function testFormatConsoleReturnsString(): void
{
$entry = new UserLoginLogEntry('192.168.1.1', anonymize: false);
$result = self::$service->formatConsole($entry);
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testIpAddressIsHtmlEscapedInFormat(): void
{
// Verify that the IP embedded in the result is safe (htmlspecialchars is applied)
$entry = new UserLoginLogEntry('192.168.0.1', anonymize: false);
$result = self::$service->format($entry);
// The result must not contain unescaped HTML even from a crafted IP
$this->assertStringNotContainsString('<script>', $result);
}
}

View file

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\LogSystem;
use App\Services\LogSystem\LogLevelHelper;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
final class LogLevelHelperTest extends TestCase
{
private LogLevelHelper $service;
protected function setUp(): void
{
$this->service = new LogLevelHelper();
}
public static function iconClassProvider(): \Generator
{
yield [LogLevel::DEBUG, 'fa-bug'];
yield [LogLevel::INFO, 'fa-info'];
yield [LogLevel::NOTICE, 'fa-flag'];
yield [LogLevel::WARNING, 'fa-exclamation-circle'];
yield [LogLevel::ERROR, 'fa-exclamation-triangle'];
yield [LogLevel::CRITICAL, 'fa-bolt'];
yield [LogLevel::ALERT, 'fa-radiation'];
yield [LogLevel::EMERGENCY, 'fa-skull-crossbones'];
}
#[DataProvider('iconClassProvider')]
public function testLogLevelToIconClass(string $logLevel, string $expectedIcon): void
{
$this->assertSame($expectedIcon, $this->service->logLevelToIconClass($logLevel));
}
public function testUnknownLogLevelReturnsDefaultIcon(): void
{
$this->assertSame('fa-question-circle', $this->service->logLevelToIconClass('unknown_level'));
}
public static function tableColorProvider(): \Generator
{
yield [LogLevel::EMERGENCY, 'table-danger'];
yield [LogLevel::ALERT, 'table-danger'];
yield [LogLevel::CRITICAL, 'table-danger'];
yield [LogLevel::ERROR, 'table-danger'];
yield [LogLevel::WARNING, 'table-warning'];
yield [LogLevel::NOTICE, 'table-info'];
yield [LogLevel::INFO, ''];
yield [LogLevel::DEBUG, ''];
}
#[DataProvider('tableColorProvider')]
public function testLogLevelToTableColorClass(string $logLevel, string $expectedClass): void
{
$this->assertSame($expectedClass, $this->service->logLevelToTableColorClass($logLevel));
}
public function testUnknownLogLevelReturnsEmptyColor(): void
{
$this->assertSame('', $this->service->logLevelToTableColorClass('unknown_level'));
}
}

View file

@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Services\UserSystem;
use App\Entity\UserSystem\User;
use App\Services\UserSystem\PermissionManager;
use App\Services\UserSystem\PermissionPresetsHelper;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class PermissionPresetsHelperTest extends WebTestCase
{
private static PermissionPresetsHelper $service;
private static PermissionManager $permissionManager;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$service = self::getContainer()->get(PermissionPresetsHelper::class);
self::$permissionManager = self::getContainer()->get(PermissionManager::class);
}
private function createUser(): User
{
return new User();
}
public function testAllInheritPresetLeavesAllPermissionsInherit(): void
{
$user = $this->createUser();
self::$service->applyPreset($user, PermissionPresetsHelper::PRESET_ALL_INHERIT);
// After all-inherit preset, 'parts' read should be null (inherit)
$this->assertNull(self::$permissionManager->dontInherit($user, 'parts', 'read'));
}
public function testAllForbidPresetSetsAllPermissionsToFalse(): void
{
$user = $this->createUser();
self::$service->applyPreset($user, PermissionPresetsHelper::PRESET_ALL_FORBID);
// After all-forbid, 'parts' read should be false (disallowed)
$this->assertFalse(self::$permissionManager->dontInherit($user, 'parts', 'read'));
}
public function testAllAllowPresetSetsAllPermissionsToTrue(): void
{
$user = $this->createUser();
self::$service->applyPreset($user, PermissionPresetsHelper::PRESET_ALL_ALLOW);
// After all-allow, 'parts' read should be true (allowed)
$this->assertTrue(self::$permissionManager->dontInherit($user, 'parts', 'read'));
}
public function testReadOnlyPresetAllowsPartsRead(): void
{
$user = $this->createUser();
self::$service->applyPreset($user, PermissionPresetsHelper::PRESET_READ_ONLY);
$this->assertTrue(self::$permissionManager->dontInherit($user, 'parts', 'read'));
}
public function testReadOnlyPresetDoesNotAllowPartsCreate(): void
{
$user = $this->createUser();
self::$service->applyPreset($user, PermissionPresetsHelper::PRESET_READ_ONLY);
// create should remain null (inherit) or false — not explicitly allowed
$createValue = self::$permissionManager->dontInherit($user, 'parts', 'create');
$this->assertNotTrue($createValue);
}
public function testUnknownPresetThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
self::$service->applyPreset($this->createUser(), 'non_existent_preset');
}
public function testApplyPresetReturnsTheSameUser(): void
{
$user = $this->createUser();
$returned = self::$service->applyPreset($user, PermissionPresetsHelper::PRESET_ALL_INHERIT);
$this->assertSame($user, $returned);
}
}

View file

@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Validator\Constraints\BigDecimal;
use App\Validator\Constraints\BigDecimal\BigDecimalGreaterThanValidator;
use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
use Brick\Math\BigDecimal;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* Tests BigDecimalGreaterThanValidator via the BigDecimalPositive constraint (value > 0).
*/
final class BigDecimalGreaterThanValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
return new BigDecimalGreaterThanValidator();
}
public function testNullIsValid(): void
{
$this->validator->validate(null, new BigDecimalPositive());
$this->assertNoViolation();
}
public function testPositiveIntegerIsValid(): void
{
$this->validator->validate(1, new BigDecimalPositive());
$this->assertNoViolation();
}
public function testPositiveStringIsValid(): void
{
$this->validator->validate('0.01', new BigDecimalPositive());
$this->assertNoViolation();
}
public function testPositiveBigDecimalIsValid(): void
{
$this->validator->validate(BigDecimal::of('1.5'), new BigDecimalPositive());
$this->assertNoViolation();
}
public function testZeroIsInvalid(): void
{
$constraint = new BigDecimalPositive();
$this->validator->validate(0, $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '0', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThan::TOO_LOW_ERROR)
->assertRaised();
}
public function testZeroBigDecimalIsInvalid(): void
{
$constraint = new BigDecimalPositive();
$this->validator->validate(BigDecimal::of('0.00'), $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '0.00', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThan::TOO_LOW_ERROR)
->assertRaised();
}
public function testNegativeIsInvalid(): void
{
$constraint = new BigDecimalPositive();
$this->validator->validate(-1, $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '-1', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThan::TOO_LOW_ERROR)
->assertRaised();
}
}

View file

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Validator\Constraints\BigDecimal;
use App\Validator\Constraints\BigDecimal\BigDecimalGreaterThenOrEqualValidator;
use App\Validator\Constraints\BigDecimal\BigDecimalPositiveOrZero;
use Brick\Math\BigDecimal;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* Tests BigDecimalGreaterThenOrEqualValidator via the BigDecimalPositiveOrZero constraint (value >= 0).
*/
final class BigDecimalGreaterThenOrEqualValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
return new BigDecimalGreaterThenOrEqualValidator();
}
public function testNullIsValid(): void
{
$this->validator->validate(null, new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testPositiveIntegerIsValid(): void
{
$this->validator->validate(1, new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testZeroIsValid(): void
{
$this->validator->validate(0, new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testZeroBigDecimalIsValid(): void
{
$this->validator->validate(BigDecimal::of('0.00'), new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testPositiveBigDecimalIsValid(): void
{
$this->validator->validate(BigDecimal::of('3.14'), new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testNegativeIsInvalid(): void
{
$constraint = new BigDecimalPositiveOrZero();
$this->validator->validate(-1, $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '-1', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThanOrEqual::TOO_LOW_ERROR)
->assertRaised();
}
public function testNegativeBigDecimalIsInvalid(): void
{
$constraint = new BigDecimalPositiveOrZero();
$this->validator->validate(BigDecimal::of('-0.01'), $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '-0.01', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThanOrEqual::TOO_LOW_ERROR)
->assertRaised();
}
}

View file

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Validator\Constraints;
use App\Validator\Constraints\ValidFileFilter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class ValidFileFilterValidatorTest extends WebTestCase
{
private static ValidatorInterface $validator;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$validator = self::getContainer()->get('validator');
}
public function testNullIsValid(): void
{
$violations = self::$validator->validate(null, new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testEmptyStringIsValid(): void
{
$violations = self::$validator->validate('', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testValidExtensionFilterIsValid(): void
{
$violations = self::$validator->validate('.jpg,.png', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testValidMimeTypeFilterIsValid(): void
{
$violations = self::$validator->validate('image/*', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testMixedValidFilterIsValid(): void
{
$violations = self::$validator->validate('image/*, .pdf, video/mp4', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testInvalidFilterRaisesViolation(): void
{
$violations = self::$validator->validate('*.notvalid', new ValidFileFilter());
$this->assertCount(1, $violations);
}
public function testFullFilenameRaisesViolation(): void
{
$violations = self::$validator->validate('test.png', new ValidFileFilter());
$this->assertCount(1, $violations);
}
}

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Tests\Validator\Constraints;
use App\Validator\Constraints\Year2038BugWorkaround;
use App\Validator\Constraints\Year2038BugWorkaroundValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
final class Year2038BugWorkaroundValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
// Disable validation by default so tests run on both 32- and 64-bit systems
return new Year2038BugWorkaroundValidator(disable_validation: true);
}
public function testIsNotActivatedWhenDisabled(): void
{
$validator = new Year2038BugWorkaroundValidator(disable_validation: true);
$this->assertFalse($validator->isActivated());
}
public function testIsNotActivatedOn64Bit(): void
{
// On any normal 64-bit CI/dev system PHP_INT_SIZE === 8, so activation requires 32-bit
if (PHP_INT_SIZE !== 8) {
$this->markTestSkipped('This test is only meaningful on 64-bit systems.');
}
$validator = new Year2038BugWorkaroundValidator(disable_validation: false);
$this->assertFalse($validator->isActivated());
}
public function testNullValueProducesNoViolation(): void
{
$this->validator->validate(null, new Year2038BugWorkaround());
$this->assertNoViolation();
}
public function testDateBefore2038ProducesNoViolationWhenDisabled(): void
{
$this->validator->validate(new \DateTime('2037-01-01'), new Year2038BugWorkaround());
$this->assertNoViolation();
}
public function testDateAfter2038ProducesNoViolationWhenDisabled(): void
{
// Validation disabled → even a "bad" date causes no violation
$this->validator->validate(new \DateTime('2039-01-01'), new Year2038BugWorkaround());
$this->assertNoViolation();
}
}