Added tests

This commit is contained in:
Jan Böhmer 2025-11-12 21:31:44 +01:00
parent 8f2ff50dd0
commit e49048b666
7 changed files with 272 additions and 7 deletions

View file

@ -35,7 +35,7 @@ use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
#[AsEventListener] #[AsEventListener]
readonly class RegisterSynonymsAsTranslationParameters readonly class RegisterSynonymsAsTranslationParametersListener
{ {
private Translator $translator; private Translator $translator;
@ -67,7 +67,7 @@ readonly class RegisterSynonymsAsTranslationParameters
//And we have lowercase versions for both //And we have lowercase versions for both
$placeholders['[' . $elementType->value . ']'] = mb_strtolower($this->typeNameGenerator->typeLabel($elementType)); $placeholders['[' . $elementType->value . ']'] = mb_strtolower($this->typeNameGenerator->typeLabel($elementType));
$placeholders['[' . $elementType->value . ']'] = mb_strtolower($this->typeNameGenerator->typeLabelPlural($elementType)); $placeholders['[[' . $elementType->value . ']]'] = mb_strtolower($this->typeNameGenerator->typeLabelPlural($elementType));
} }
return $placeholders; return $placeholders;

View file

@ -71,7 +71,7 @@ class SynonymSettings
*/ */
public function isSynonymDefinedForType(ElementTypes $type): bool public function isSynonymDefinedForType(ElementTypes $type): bool
{ {
return isset($this->typeSynonyms[$type->value]); return isset($this->typeSynonyms[$type->value]) && count($this->typeSynonyms[$type->value]) > 0;
} }
/** /**
@ -97,4 +97,20 @@ class SynonymSettings
?? $this->typeSynonyms[$type->value][$locale]['singular'] ?? $this->typeSynonyms[$type->value][$locale]['singular']
?? null; ?? null;
} }
/**
* Sets a synonym for the given type and locale.
* @param ElementTypes $type
* @param string $locale
* @param string $singular
* @param string $plural
* @return void
*/
public function setSynonymForType(ElementTypes $type, string $locale, string $singular, string $plural): void
{
$this->typeSynonyms[$type->value][$locale] = [
'singular' => $singular,
'plural' => $plural,
];
}
} }

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\EventListener;
use App\EventListener\RegisterSynonymsAsTranslationParametersListener;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class RegisterSynonymsAsTranslationParametersTest extends KernelTestCase
{
private RegisterSynonymsAsTranslationParametersListener $listener;
public function setUp(): void
{
self::bootKernel();
$this->listener = self::getContainer()->get(RegisterSynonymsAsTranslationParametersListener::class);
}
public function testGetSynonymPlaceholders(): void
{
$placeholders = $this->listener->getSynonymPlaceholders();
$this->assertIsArray($placeholders);
$this->assertSame('Part', $placeholders['{part}']);
$this->assertSame('Parts', $placeholders['{{part}}']);
//Lowercase versions:
$this->assertSame('part', $placeholders['[part]']);
$this->assertSame('parts', $placeholders['[[part]]']);
}
}

View file

@ -30,20 +30,27 @@ use App\Entity\Parts\Category;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Exceptions\EntityNotSupportedException; use App\Exceptions\EntityNotSupportedException;
use App\Services\ElementTypeNameGenerator; use App\Services\ElementTypeNameGenerator;
use App\Services\ElementTypes;
use App\Services\Formatters\AmountFormatter; use App\Services\Formatters\AmountFormatter;
use App\Settings\SynonymSettings;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ElementTypeNameGeneratorTest extends WebTestCase class ElementTypeNameGeneratorTest extends WebTestCase
{ {
/** protected ElementTypeNameGenerator $service;
* @var AmountFormatter private SynonymSettings $synonymSettings;
*/
protected $service;
protected function setUp(): void protected function setUp(): void
{ {
//Get an service instance. //Get an service instance.
$this->service = self::getContainer()->get(ElementTypeNameGenerator::class); $this->service = self::getContainer()->get(ElementTypeNameGenerator::class);
$this->synonymSettings = self::getContainer()->get(SynonymSettings::class);
}
protected function tearDown(): void
{
//Clean up synonym settings
$this->synonymSettings->typeSynonyms = [];
} }
public function testGetLocalizedTypeNameCombination(): void public function testGetLocalizedTypeNameCombination(): void
@ -84,4 +91,30 @@ class ElementTypeNameGeneratorTest extends WebTestCase
} }
}); });
} }
public function testTypeLabel(): void
{
//If no synonym is defined, the default label should be used
$this->assertSame('Part', $this->service->typeLabel(Part::class));
$this->assertSame('Part', $this->service->typeLabel(new Part()));
$this->assertSame('Part', $this->service->typeLabel(ElementTypes::PART));
$this->assertSame('Part', $this->service->typeLabel('part'));
//Define a synonym for parts in english
$this->synonymSettings->setSynonymForType(ElementTypes::PART, 'en', 'Singular', 'Plurals');
$this->assertSame('Singular', $this->service->typeLabel(Part::class));
}
public function testTypeLabelPlural(): void
{
//If no synonym is defined, the default label should be used
$this->assertSame('Parts', $this->service->typeLabelPlural(Part::class));
$this->assertSame('Parts', $this->service->typeLabelPlural(new Part()));
$this->assertSame('Parts', $this->service->typeLabelPlural(ElementTypes::PART));
$this->assertSame('Parts', $this->service->typeLabelPlural('part'));
//Define a synonym for parts in english
$this->synonymSettings->setSynonymForType(ElementTypes::PART, 'en', 'Singular', 'Plurals');
$this->assertSame('Plurals', $this->service->typeLabelPlural(Part::class));
}
} }

View file

@ -0,0 +1,79 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\Parameters\CategoryParameter;
use App\Entity\Parts\Category;
use App\Exceptions\EntityNotSupportedException;
use App\Services\ElementTypes;
use PHPUnit\Framework\TestCase;
class ElementTypesTest extends TestCase
{
public function testFromClass(): void
{
$this->assertSame(ElementTypes::CATEGORY, ElementTypes::fromClass(Category::class));
$this->assertSame(ElementTypes::CATEGORY, ElementTypes::fromClass(new Category()));
//Should also work with subclasses
$this->assertSame(ElementTypes::PARAMETER, ElementTypes::fromClass(CategoryParameter::class));
$this->assertSame(ElementTypes::PARAMETER, ElementTypes::fromClass(new CategoryParameter()));
}
public function testFromClassNotExisting(): void
{
$this->expectException(EntityNotSupportedException::class);
ElementTypes::fromClass(\LogicException::class);
}
public function testFromValue(): void
{
//By enum value
$this->assertSame(ElementTypes::CATEGORY, ElementTypes::fromValue('category'));
$this->assertSame(ElementTypes::ATTACHMENT, ElementTypes::fromValue('attachment'));
//From enum instance
$this->assertSame(ElementTypes::CATEGORY, ElementTypes::fromValue(ElementTypes::CATEGORY));
//From class string
$this->assertSame(ElementTypes::CATEGORY, ElementTypes::fromValue(Category::class));
$this->assertSame(ElementTypes::PARAMETER, ElementTypes::fromValue(CategoryParameter::class));
//From class instance
$this->assertSame(ElementTypes::CATEGORY, ElementTypes::fromValue(new Category()));
$this->assertSame(ElementTypes::PARAMETER, ElementTypes::fromValue(new CategoryParameter()));
}
public function testGetDefaultLabelKey(): void
{
$this->assertSame('category.label', ElementTypes::CATEGORY->getDefaultLabelKey());
$this->assertSame('attachment.label', ElementTypes::ATTACHMENT->getDefaultLabelKey());
}
public function testGetDefaultPluralLabelKey(): void
{
$this->assertSame('category.labelp', ElementTypes::CATEGORY->getDefaultPluralLabelKey());
$this->assertSame('attachment.labelp', ElementTypes::ATTACHMENT->getDefaultPluralLabelKey());
}
}

View file

@ -0,0 +1,76 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 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\Settings;
use App\Services\ElementTypes;
use App\Settings\SynonymSettings;
use App\Tests\SettingsTestHelper;
use PHPUnit\Framework\TestCase;
class SynonymSettingsTest extends TestCase
{
public function testGetSingularSynonymForType(): void
{
$settings = SettingsTestHelper::createSettingsDummy(SynonymSettings::class);
$settings->typeSynonyms['category'] = [
'en' => ['singular' => 'Category', 'plural' => 'Categories'],
'de' => ['singular' => 'Kategorie', 'plural' => 'Kategorien'],
];
$this->assertEquals('Category', $settings->getSingularSynonymForType(ElementTypes::CATEGORY, 'en'));
$this->assertEquals('Kategorie', $settings->getSingularSynonymForType(ElementTypes::CATEGORY, 'de'));
//If no synonym is defined, it should return null
$this->assertNull($settings->getSingularSynonymForType(ElementTypes::MANUFACTURER, 'en'));
}
public function testIsSynonymDefinedForType(): void
{
$settings = SettingsTestHelper::createSettingsDummy(SynonymSettings::class);
$settings->typeSynonyms['category'] = [
'en' => ['singular' => 'Category', 'plural' => 'Categories'],
'de' => ['singular' => 'Kategorie', 'plural' => 'Kategorien'],
];
$settings->typeSynonyms['supplier'] = [];
$this->assertTrue($settings->isSynonymDefinedForType(ElementTypes::CATEGORY));
$this->assertFalse($settings->isSynonymDefinedForType(ElementTypes::FOOTPRINT));
$this->assertFalse($settings->isSynonymDefinedForType(ElementTypes::SUPPLIER));
}
public function testGetPluralSynonymForType(): void
{
$settings = SettingsTestHelper::createSettingsDummy(SynonymSettings::class);
$settings->typeSynonyms['category'] = [
'en' => ['singular' => 'Category', 'plural' => 'Categories'],
'de' => ['singular' => 'Kategorie',],
];
$this->assertEquals('Categories', $settings->getPluralSynonymForType(ElementTypes::CATEGORY, 'en'));
//Fallback to singular if no plural is defined
$this->assertEquals('Kategorie', $settings->getPluralSynonymForType(ElementTypes::CATEGORY, 'de'));
//If no synonym is defined, it should return null
$this->assertNull($settings->getPluralSynonymForType(ElementTypes::MANUFACTURER, 'en'));
}
}

View file

@ -14400,5 +14400,17 @@ Please note that this system is currently experimental, and the synonyms defined
<target>Parts</target> <target>Parts</target>
</segment> </segment>
</unit> </unit>
<unit id="wjcsjzT" name="log.element_edited.changed_fields.part_ipn_prefix">
<segment>
<source>log.element_edited.changed_fields.part_ipn_prefix</source>
<target>IPN prefix</target>
</segment>
</unit>
<unit id="R4hoCqe" name="part.labelp">
<segment>
<source>part.labelp</source>
<target>Parts</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>