Skip to content

Commit

Permalink
Add integration tests with the Symfony full-stack framework (#54)
Browse files Browse the repository at this point in the history
This fixes #43. It would have helped to spot the broken wiring of
Doctrine lifecycle subscribers fixed in #51, or the declaration of
compatibility with Symfony 7 in #20 that missed the `annotation_reader`
depencency.
  • Loading branch information
mpdude authored Apr 7, 2024
1 parent 79c0c2f commit d3b54a8
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 27 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ on:
- master
pull_request:

env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0
DOCTRINE_DEPRECATIONS: trigger

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
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
},

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

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="max[self]=0" />
<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 @@ -6,8 +6,8 @@
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
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,11 +7,10 @@
* 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;
use TestEntityTranslation;
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;

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 All @@ -21,7 +21,7 @@ class TestEntityTranslation extends BaseTranslation
/**
* @var TestEntity
*/
#[ORM\ManyToOne(targetEntity: \TestEntity::class, inversedBy: 'translations')]
#[ORM\ManyToOne(targetEntity: TestEntity::class, inversedBy: 'translations')]
protected $entity;

/**
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__;
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
framework:
test: true
annotations: false


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: attribute
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 d3b54a8

Please sign in to comment.