Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
"doctrine/orm": "^3.0",
"doctrine/doctrine-bundle": "^2.13 || ^3.0",
"symfony/translation": "^6.4 || ^7.0 || ^8.0",
"symfony/dependency-injection": "^6.4 || ^7.0 || ^8.0"
"symfony/dependency-injection": "^6.4 || ^7.0 || ^8.0",
"symfony/yaml": "^6.4 || ^7.0 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"phpstan/phpstan": "^2.2",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-symfony": "^2.0",
"phpstan/phpstan-doctrine": "^2.0",
"friendsofphp/php-cs-fixer": "^3.95"
"friendsofphp/php-cs-fixer": "^3.95",
"symfony/framework-bundle": "^6.4 || ^7.0 || ^8.0",
"symfony/validator": "^6.4 || ^7.0 || ^8.0",
"api-platform/core": "^3.4 || ^4.0"
},
"prefer-stable": true,
"autoload": {
Expand Down
76 changes: 76 additions & 0 deletions tests/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Locastic\ApiPlatformTranslationBundle\Tests\Fixtures;

use ApiPlatform\Symfony\Bundle\ApiPlatformBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Locastic\ApiPlatformTranslationBundle\ApiPlatformTranslationBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel;

final class TestKernel extends Kernel
{
use MicroKernelTrait;

public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
new ApiPlatformBundle(),
new ApiPlatformTranslationBundle(),
];
}

public function getCacheDir(): string
{
return sys_get_temp_dir().'/aptb-tests/cache/'.$this->environment;
}

public function getLogDir(): string
{
return sys_get_temp_dir().'/aptb-tests/log';
}

protected function build(ContainerBuilder $container): void
{
// Keep the bundle services visible to the test; private unused
// services are otherwise inlined or removed during compilation.
$container->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
foreach ($container->getDefinitions() as $id => $definition) {
if (str_starts_with($id, 'locastic_api_platform_translation.')) {
$definition->setPublic(true);
}
}
}
});
}

protected function configureContainer(ContainerConfigurator $container): void
{
$container->extension('framework', [
'secret' => 'test',
'test' => true,
'http_method_override' => false,
'validation' => ['enabled' => true],
]);

$container->extension('doctrine', [
'dbal' => ['url' => 'sqlite:///:memory:'],
]);

$container->extension('api_platform', [
'title' => 'Test',
'mapping' => ['paths' => []],
'doctrine' => false,
]);
}
}
44 changes: 44 additions & 0 deletions tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Locastic\ApiPlatformTranslationBundle\Tests\Functional;

use ApiPlatform\Serializer\Filter\GroupFilter;
use Locastic\ApiPlatformTranslationBundle\EventListener\AssignLocaleListener;
use Locastic\ApiPlatformTranslationBundle\Tests\Fixtures\TestKernel;
use Locastic\ApiPlatformTranslationBundle\Translation\Translator;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* Boots a real kernel with FrameworkBundle, DoctrineBundle and ApiPlatformBundle
* to prove the container compiles and the bundle services are wired, in
* particular the translation.groups filter, which inherits from an
* api_platform service definition the unit tests never see.
*/
class BundleInitializationTest extends KernelTestCase
{
protected static function getKernelClass(): string
{
return TestKernel::class;
}

public function testContainerCompilesAndRegistersServices(): void
{
self::bootKernel();
$container = static::getContainer();

$this->assertInstanceOf(
Translator::class,
$container->get('locastic_api_platform_translation.translation.translator')
);
$this->assertInstanceOf(
AssignLocaleListener::class,
$container->get('locastic_api_platform_translation.listener.assign_locale')
);
$this->assertInstanceOf(
GroupFilter::class,
$container->get('locastic_api_platform_translation.filter.translation_groups')
);
}
}
75 changes: 75 additions & 0 deletions tests/Model/TranslatableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,81 @@ public function testGetTranslationWithoutLocales(): void
$dummyTranslatable->getTranslation();
}

/**
* @test getTranslation
*/
public function testGetTranslationCreatesAndAddsMissingTranslation(): void
{
$dummyTranslatable = $this->setTranslatable('es', 'en');
$this->assertCount(0, $dummyTranslatable->getTranslations());

$translation = $dummyTranslatable->getTranslation('fr');

$this->assertSame('fr', $translation->getLocale());
$this->assertSame($dummyTranslatable, $translation->getTranslatable());
$this->assertCount(1, $dummyTranslatable->getTranslations());
}

/**
* @test hasTranslation
*/
public function testHasTranslation(): void
{
$dummyTranslatable = $this->setTranslatable('es', 'en');
$english = $this->setTranslation('en', 'english', $dummyTranslatable);

$notAdded = new DummyTranslation();
$notAdded->setLocale('fr');

$this->assertTrue($dummyTranslatable->hasTranslation($english));
$this->assertFalse($dummyTranslatable->hasTranslation($notAdded));
}

/**
* @test addTranslation
*/
public function testAddTranslationIgnoresTranslationWithoutLocale(): void
{
$dummyTranslatable = $this->setTranslatable('es', 'en');

$translation = new DummyTranslation();
$dummyTranslatable->addTranslation($translation);

$this->assertCount(0, $dummyTranslatable->getTranslations());
$this->assertNull($translation->getTranslatable());
}

/**
* @test addTranslation
*/
public function testAddTranslationIgnoresDuplicateLocale(): void
{
$dummyTranslatable = $this->setTranslatable('es', 'en');
$this->setTranslation('en', 'english', $dummyTranslatable);

$duplicate = new DummyTranslation();
$duplicate->setLocale('en');
$duplicate->setTranslation('english again');
$dummyTranslatable->addTranslation($duplicate);

$this->assertCount(1, $dummyTranslatable->getTranslations());
$this->assertSame('english', $dummyTranslatable->getTranslation('en')->getTranslation());
}

/**
* @test removeTranslationWithLocale
*/
public function testRemoveTranslationWithLocale(): void
{
$dummyTranslatable = $this->setTranslatable('es', 'en');
$this->setTranslation('en', 'english', $dummyTranslatable);
$this->setTranslation('es', 'espanol', $dummyTranslatable);

$dummyTranslatable->removeTranslationWithLocale('en');

$this->assertSame(['es'], array_values($dummyTranslatable->getTranslationLocales()));
}

/**
* @param TranslatableInterface<DummyTranslation> $translatable
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,7 @@ public function provideLocalesWithAcceptLanguage(): \Generator
yield [null, null, 'en'];
yield [null, 'fr_FR', 'fr'];
yield [null, 'es', 'en']; // Accept-Language locale not enabled
yield [null, 'it;q=0.4, fr;q=0.9', 'fr']; // Quality values respected
yield [null, 'fr;q=0.3, it;q=0.9', 'it'];
}
}
Loading