Skip to content

Commit

Permalink
Integration tests with the Symfony full-stack framework (backport to …
Browse files Browse the repository at this point in the history
…3.x) (#66)

This backports the changes from #54 onto the 3.x branch.
  • Loading branch information
mpdude authored Apr 24, 2024
1 parent 538ed97 commit 8efa14e
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 26 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ on:
- master
pull_request:

env:
SYMFONY_DEPRECATIONS_HELPER: weak

jobs:
PHPUnit:

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tests/Fixtures/var
vendor/
composer.lock
phpunit.xml$
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
"symfony/dependency-injection": "^5.4|^6.4|^7.0",
"symfony/deprecation-contracts": "^2.0|^3.0",
"symfony/event-dispatcher": "^5.4|^6.4|^7.0",
"symfony/framework-bundle": "^5.4|^6.4",
"symfony/http-kernel": "^5.4|^6.4|^7.0"
},

"require-dev": {
"phpunit/phpunit": "^8.5.36|^9.6",
"doctrine/common": "^2.0|^3.1",
"doctrine/doctrine-bundle": "^2.0",
"phpunit/phpunit": "^9.6.18",
"symfony/error-handler": "^5.4|^6.4|^7.0",
"symfony/phpunit-bridge": ">= 7.0",
"webfactory/doctrine-orm-test-infrastructure": "^1.9"
"symfony/yaml": "^5.4|^6.4|^7.0",
"webfactory/doctrine-orm-test-infrastructure": "^1.14"
},

"config": {
Expand Down
20 changes: 8 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">

Expand All @@ -9,17 +9,13 @@
<directory>tests</directory>
</testsuite>
</testsuites>

<!-- Filter for code coverage -->
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory>src/Resources</directory>
</exclude>
</whitelist>
</filter>

<php>
<server name="KERNEL_CLASS" value="Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\TestKernel" />
<server name="KERNEL_DIR" value="tests/Fixtures/" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
<env name="DOCTRINE_DEPRECATIONS" value="trigger" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
Expand Down
2 changes: 1 addition & 1 deletion src/WebfactoryPolyglotBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

final class WebfactoryPolyglotBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new RegisterDoctrineTypePass());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/PersistentTranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use Symfony\Component\ErrorHandler\BufferingLogger;
use Webfactory\Bundle\PolyglotBundle\Doctrine\PersistentTranslatable;
use Webfactory\Bundle\PolyglotBundle\Locale\DefaultLocaleProvider;
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntityTranslation;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntityTranslation;

class PersistentTranslatableTest extends TestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/TranslatableClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Webfactory\Bundle\PolyglotBundle\Doctrine\TranslatableClassMetadata;
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntityTranslation;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntityTranslation;
use Webfactory\Doctrine\ORMTestInfrastructure\ORMInfrastructure;

class TranslatableClassMetadataTest extends TestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

namespace Webfactory\Bundle\PolyglotBundle\Tests;
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

namespace Webfactory\Bundle\PolyglotBundle\Tests;
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class TestKernel extends Kernel
{
public function registerBundles(): iterable
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new \Webfactory\Bundle\PolyglotBundle\WebfactoryPolyglotBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__.'/config/config.yml');
}

public function getProjectDir(): string
{
return __DIR__;
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
framework:
test: true

doctrine:
dbal:
driver: pdo_sqlite
memory: true
orm:
# The following would silence Doctrine bundle deprecation messages, but it's not straightforward
# to do while still being compliant across a range of ORM / DoctrineBundle versions.
#report_fields_where_declared: true
#enable_lazy_ghost_objects: true
mappings:
WebfactoryPolyglotBundle:
type: annotation
dir: ../tests/Fixtures/Entity
prefix: Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity

4 changes: 2 additions & 2 deletions tests/Functional/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Webfactory\Bundle\PolyglotBundle\Tests\Functional;

use Webfactory\Bundle\PolyglotBundle\Tests\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntityTranslation;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntityTranslation;
use Webfactory\Bundle\PolyglotBundle\Translatable;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;

Expand Down
44 changes: 44 additions & 0 deletions tests/Functional/SymfonyIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Webfactory\Bundle\PolyglotBundle\Tests\Functional;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntity;
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntityTranslation;
use Webfactory\Bundle\PolyglotBundle\Translatable;

class SymfonyIntegrationTest extends KernelTestCase
{
/**
* @test
*/
public function persist_and_reload_entity_in_Symfony(): void
{
self::bootKernel();
$container = static::getContainer();

/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get('doctrine.orm.entity_manager');

$schemaTool = new SchemaTool($entityManager);
$schemaTool->createSchema([
$entityManager->getClassMetadata(TestEntity::class),
$entityManager->getClassMetadata(TestEntityTranslation::class),
]);

$value = new Translatable('english', 'en_GB');
$value->setTranslation('deutsch', 'de_DE');
$entity = new TestEntity($value);

$entityManager->persist($entity);
$entityManager->flush();
$entityManager->clear();

$reloadedEntity = $entityManager->find(TestEntity::class, $entity->getId());

self::assertSame('english', $reloadedEntity->getText()->translate('en_GB'));
self::assertSame('deutsch', $reloadedEntity->getText()->translate('de_DE'));
}
}

0 comments on commit 8efa14e

Please sign in to comment.