Added more tests

This commit is contained in:
Jan Böhmer 2026-05-11 21:50:33 +02:00
parent f3f93a8205
commit 7d27bff062
8 changed files with 756 additions and 1 deletions

View file

@ -0,0 +1,110 @@
<?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\UserSystem\User;
use App\Services\Cache\UserCacheKeyGenerator;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
final class UserCacheKeyGeneratorTest extends TestCase
{
private function makeGenerator(?User $loggedInUser, ?Request $request = null): UserCacheKeyGenerator
{
$security = $this->createMock(Security::class);
$security->method('getUser')->willReturn($loggedInUser);
$requestStack = $this->createMock(RequestStack::class);
$requestStack->method('getCurrentRequest')->willReturn($request);
return new UserCacheKeyGenerator($security, $requestStack);
}
private function makeUserWithId(int $id): User
{
$user = new User();
$ref = new \ReflectionProperty(User::class, 'id');
$ref->setValue($user, $id);
return $user;
}
public function testAnonymousUserKeyContainsAnonymousId(): void
{
$service = $this->makeGenerator(null);
$key = $service->generateKey();
$this->assertStringContainsString((string) User::ID_ANONYMOUS, $key);
}
public function testExplicitAnonymousUserGivesSameKeyAsNull(): void
{
$anonUser = $this->makeUserWithId(User::ID_ANONYMOUS);
$anonUser->setName('anonymous');
$service = $this->makeGenerator(null);
$keyFromNull = $service->generateKey(null);
$keyFromAnon = $service->generateKey($anonUser);
$this->assertSame($keyFromNull, $keyFromAnon);
}
public function testKeyForRealUserContainsUserId(): void
{
$user = $this->makeUserWithId(42);
$service = $this->makeGenerator(null);
$key = $service->generateKey($user);
$this->assertStringContainsString('42', $key);
$this->assertStringNotContainsString((string) User::ID_ANONYMOUS, $key);
}
public function testLocaleFromRequestIsIncludedInKey(): void
{
$request = Request::create('/');
$request->setLocale('de');
$service = $this->makeGenerator(null, $request);
$key = $service->generateKey();
$this->assertStringContainsString('de', $key);
}
public function testDifferentUsersProduceDifferentKeys(): void
{
$service = $this->makeGenerator(null);
$user1 = $this->makeUserWithId(10);
$user2 = $this->makeUserWithId(20);
$this->assertNotSame($service->generateKey($user1), $service->generateKey($user2));
}
public function testCurrentlyLoggedInUserIsUsedWhenNoExplicitUser(): void
{
$loggedIn = $this->makeUserWithId(99);
$service = $this->makeGenerator($loggedIn);
$key = $service->generateKey();
$this->assertStringContainsString('99', $key);
}
}

View file

@ -0,0 +1,113 @@
<?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;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Entity\Parts\StorageLocation;
use App\Entity\Parts\Supplier;
use App\Entity\UserSystem\User;
use App\Exceptions\EntityNotSupportedException;
use App\Services\EntityURLGenerator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class EntityURLGeneratorTest extends WebTestCase
{
private static EntityURLGenerator $service;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$service = self::getContainer()->get(EntityURLGenerator::class);
}
private function entityWithId(string $class, int $id): AbstractDBElement
{
$entity = new $class();
$ref = new \ReflectionProperty(AbstractDBElement::class, 'id');
$ref->setValue($entity, $id);
return $entity;
}
public function testInfoUrlForPartContainsPartPath(): void
{
$part = $this->entityWithId(Part::class, 1);
$url = self::$service->infoURL($part);
$this->assertStringContainsString('part', $url);
$this->assertStringContainsString('1', $url);
}
public function testEditUrlForCategoryContainsCategoryPath(): void
{
$category = $this->entityWithId(Category::class, 5);
$url = self::$service->editURL($category);
$this->assertStringContainsString('category', $url);
$this->assertStringContainsString('5', $url);
}
public function testListPartsUrlForSupplierContainsSupplierPath(): void
{
$supplier = $this->entityWithId(Supplier::class, 7);
$url = self::$service->listPartsURL($supplier);
$this->assertStringContainsString('supplier', $url);
}
public function testGetUrlWithInfoTypeCallsInfoUrl(): void
{
$part = $this->entityWithId(Part::class, 3);
$url = self::$service->getURL($part, 'info');
$this->assertStringContainsString('part', $url);
}
public function testGetUrlWithEditTypeCallsEditUrl(): void
{
$manufacturer = $this->entityWithId(Manufacturer::class, 2);
$url = self::$service->getURL($manufacturer, 'edit');
$this->assertStringContainsString('manufacturer', $url);
}
public function testGetUrlWithUnknownTypeThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$part = $this->entityWithId(Part::class, 1);
self::$service->getURL($part, 'unsupported_type');
}
public function testInfoUrlForUserContainsUserPath(): void
{
$user = $this->entityWithId(User::class, 10);
$url = self::$service->editURL($user);
$this->assertStringContainsString('user', $url);
}
public function testListPartsUrlForStorelocationContainsStorelocationPath(): void
{
$loc = $this->entityWithId(StorageLocation::class, 4);
$url = self::$service->listPartsURL($loc);
$this->assertStringContainsString('store', $url);
}
}

View file

@ -43,7 +43,13 @@ final class MoneyFormatterTest extends WebTestCase
$currency->setIsoCode('USD');
$result = self::$service->format(1.5, $currency);
$this->assertSame('$ 1.50', $result);
// Output format varies by locale, so verify content not exact form
$this->assertNotEmpty($result);
$this->assertStringContainsString('1', $result);
$this->assertTrue(
str_contains($result, '$') || str_contains($result, 'USD'),
"Expected USD indicator in: $result"
);
}
public function testFormatWithNullCurrencyUsesBaseCurrency(): void

View file

@ -0,0 +1,133 @@
<?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\AbstractLogEntry;
use App\Services\LogSystem\LogDataFormatter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class LogDataFormatterTest extends WebTestCase
{
private static LogDataFormatter $service;
private static AbstractLogEntry $dummyLog;
private AbstractLogEntry $dummy;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$service = self::getContainer()->get(LogDataFormatter::class);
}
protected function setUp(): void
{
parent::setUp();
// A mock is fine: $logEntry is only consulted for @id (foreign key) arrays
$this->dummy = $this->createMock(AbstractLogEntry::class);
}
public function testStringIsWrappedInQuoteSpans(): void
{
$result = self::$service->formatData('hello', $this->dummy, 'name');
$this->assertStringContainsString('"', $result);
$this->assertStringContainsString('hello', $result);
}
public function testStringSpecialCharsAreEscaped(): void
{
$result = self::$service->formatData('<script>', $this->dummy, 'name');
$this->assertStringNotContainsString('<script>', $result);
$this->assertStringContainsString('&lt;script&gt;', $result);
}
public function testNewlineInStringRendersAsSpan(): void
{
$result = self::$service->formatData("line1\nline2", $this->dummy, 'name');
$this->assertStringContainsString('\\n', $result);
}
public function testBoolTrueFormatsAsString(): void
{
$result = self::$service->formatData(true, $this->dummy, 'enabled');
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testBoolFalseFormatsAsString(): void
{
$result = self::$service->formatData(false, $this->dummy, 'enabled');
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testBoolTrueAndFalseProduceDifferentOutput(): void
{
$true = self::$service->formatData(true, $this->dummy, 'enabled');
$false = self::$service->formatData(false, $this->dummy, 'enabled');
$this->assertNotSame($true, $false);
}
public function testIntegerFormatsToItsStringRepresentation(): void
{
$result = self::$service->formatData(42, $this->dummy, 'count');
$this->assertSame('42', $result);
}
public function testFloatFormatsToItsStringRepresentation(): void
{
$result = self::$service->formatData(3.14, $this->dummy, 'price');
$this->assertSame('3.14', $result);
}
public function testNullFormatsAsItalicNull(): void
{
$result = self::$service->formatData(null, $this->dummy, 'field');
$this->assertSame('<i>null</i>', $result);
}
public function testDateTimeArrayFormatsToDateString(): void
{
$data = [
'date' => '2024-01-15 10:30:00.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
];
$result = self::$service->formatData($data, $this->dummy, 'created_at');
$this->assertIsString($result);
$this->assertNotEmpty($result);
// Should not be the JSON fallback
$this->assertStringNotContainsString('json-formatter', $result);
}
public function testPlainArrayFormatsAsJsonDiv(): void
{
$result = self::$service->formatData(['key' => 'value', 'num' => 1], $this->dummy, 'tags');
$this->assertStringContainsString('json-formatter', $result);
}
public function testUnsupportedTypeThrowsRuntimeException(): void
{
$this->expectException(\RuntimeException::class);
self::$service->formatData(new \stdClass(), $this->dummy, 'field');
}
}