Skip to content

Commit 850ca3f

Browse files
authored
Run functional tests without the doctrine-orm-infrastructure dependency (#85)
I am unsure about the future of the [Doctrine-ORM-Test-infrastructure package](https://github.com/webfactory/doctrine-orm-test-infrastructure/): Recent deprecations in ORM 3 and especially DBAL 3 make it seem harder and harder to come up with something that closely resembles a working ORM setup "out of nothing". So, this PR gets rid of that dependency. It builds upon Symfonys functional testing infrastructure, and configures a copy of the default entity manager in the DIC. This is good enough to have separate channels for importing/loading data and still having a pristine entity manager afterwards (altough, admittedly, a `clear()` call would probably have the same effect?). Also, setting up the schema in memory is not all too complicated, leveraging the ORM's schema tool.
1 parent 31db2dc commit 850ca3f

34 files changed

+857
-646
lines changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"doctrine/event-manager": "^1.1|^2.0",
2727
"doctrine/orm": "^2.13|^3.0",
2828
"doctrine/persistence": "^2.4|^3.1",
29-
"psr/log": "^1.0|^2.0|^3.0",
29+
"psr/log": "^2.0|^3.0",
3030
"symfony/config": "^6.4|^7.0",
3131
"symfony/dependency-injection": "^6.4|^7.0",
3232
"symfony/deprecation-contracts": "^2.0|^3.0",
@@ -39,8 +39,7 @@
3939
"doctrine/doctrine-bundle": "^2.12",
4040
"phpunit/phpunit": "^10.5.58",
4141
"symfony/framework-bundle": "^6.4|^7.0",
42-
"symfony/yaml": "^6.4|^7.0",
43-
"webfactory/doctrine-orm-test-infrastructure": "^1.14"
42+
"symfony/yaml": "^6.4|^7.0"
4443
},
4544

4645
"config": {

tests/Doctrine/TranslatableClassMetadataTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace Webfactory\Bundle\PolyglotBundle\Tests\Doctrine;
44

5-
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
65
use Doctrine\Persistence\Mapping\RuntimeReflectionService;
7-
use PHPUnit\Framework\TestCase;
86
use Webfactory\Bundle\PolyglotBundle\Doctrine\TranslatableClassMetadata;
97
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntity;
10-
use Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\TestEntityTranslation;
11-
use Webfactory\Doctrine\ORMTestInfrastructure\ORMInfrastructure;
8+
use Webfactory\Bundle\PolyglotBundle\Tests\Functional\DatabaseFunctionalTestCase;
129

13-
class TranslatableClassMetadataTest extends TestCase
10+
class TranslatableClassMetadataTest extends DatabaseFunctionalTestCase
1411
{
1512
/**
1613
* @test
@@ -27,11 +24,7 @@ public function can_be_serialized_and_retrieved(): void
2724

2825
private function createMetadata(): TranslatableClassMetadata
2926
{
30-
$infrastructure = new ORMInfrastructure([
31-
TestEntity::class,
32-
TestEntityTranslation::class,
33-
], mappingDriver: new AttributeDriver([], true));
34-
$entityManager = $infrastructure->getEntityManager();
27+
$entityManager = $this->entityManager;
3528

3629
return TranslatableClassMetadata::parseFromClass(TestEntity::class, $entityManager->getMetadataFactory());
3730
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\CascadePersist;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
9+
use Webfactory\Bundle\PolyglotBundle\Translatable;
10+
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
11+
12+
#[Polyglot\Locale(primary: 'en_GB')]
13+
#[ORM\Entity]
14+
class CascadePersistTranslationsTest_Entity
15+
{
16+
#[ORM\Column]
17+
#[ORM\Id]
18+
#[ORM\GeneratedValue]
19+
private ?int $id = null;
20+
21+
/**
22+
* (!) There is *not* cascade="persist" configuration here.
23+
*/
24+
#[Polyglot\TranslationCollection]
25+
#[ORM\OneToMany(targetEntity: CascadePersistTranslationsTest_Translation::class, mappedBy: 'entity')]
26+
protected Collection $translations;
27+
28+
#[Polyglot\Translatable]
29+
#[ORM\Column(type: 'string')]
30+
protected string|TranslatableInterface $text;
31+
32+
public function __construct()
33+
{
34+
$this->text = new Translatable('test en_GB');
35+
$this->translations = new ArrayCollection();
36+
}
37+
38+
public function addTranslation(string $locale, mixed $text): void
39+
{
40+
$this->text->setTranslation($text, $locale);
41+
}
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\CascadePersist;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
7+
8+
#[ORM\Entity]
9+
class CascadePersistTranslationsTest_Translation
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column]
14+
private ?int $id = null;
15+
16+
#[Polyglot\Locale]
17+
#[ORM\Column]
18+
private string $locale;
19+
20+
#[ORM\ManyToOne(inversedBy: 'translations')]
21+
private CascadePersistTranslationsTest_Entity $entity;
22+
23+
#[ORM\Column]
24+
private string $text;
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
9+
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
10+
11+
#[Polyglot\Locale(primary: 'en_GB')]
12+
#[ORM\Entity]
13+
#[ORM\InheritanceType(value: 'SINGLE_TABLE')]
14+
#[ORM\DiscriminatorMap(['base' => 'EntityInheritance_BaseEntityClass', 'child' => 'EntityInheritance_ChildEntityClass'])]
15+
#[ORM\DiscriminatorColumn(name: 'discriminator', type: 'string')]
16+
class EntityInheritance_BaseEntityClass
17+
{
18+
#[ORM\Column(type: 'integer')]
19+
#[ORM\Id]
20+
#[ORM\GeneratedValue]
21+
private ?int $id = null;
22+
23+
private string $discriminator;
24+
25+
#[Polyglot\TranslationCollection]
26+
#[ORM\OneToMany(targetEntity: EntityInheritance_BaseEntityClassTranslation::class, mappedBy: 'entity')]
27+
private Collection $translations;
28+
29+
#[Polyglot\Translatable]
30+
#[ORM\Column(type: 'string')]
31+
private TranslatableInterface|string|null $text = null;
32+
33+
public function __construct()
34+
{
35+
$this->translations = new ArrayCollection();
36+
}
37+
38+
public function getId(): int
39+
{
40+
return $this->id;
41+
}
42+
43+
public function setText(TranslatableInterface $text): void
44+
{
45+
$this->text = $text;
46+
}
47+
48+
public function getText(): TranslatableInterface
49+
{
50+
return $this->text;
51+
}
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
7+
8+
#[ORM\Entity]
9+
class EntityInheritance_BaseEntityClassTranslation
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column(type: 'integer')]
14+
private ?int $id = null;
15+
16+
#[Polyglot\Locale]
17+
#[ORM\Column]
18+
private string $locale;
19+
20+
#[ORM\ManyToOne(inversedBy: 'translations')]
21+
private EntityInheritance_BaseEntityClass $entity;
22+
23+
#[ORM\Column]
24+
private string $text;
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
9+
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
10+
11+
#[ORM\Entity]
12+
class EntityInheritance_ChildEntityClass extends EntityInheritance_BaseEntityClass
13+
{
14+
#[Polyglot\Translatable]
15+
#[ORM\Column(type: 'string')]
16+
private TranslatableInterface|string|null $extraText = null;
17+
18+
#[Polyglot\TranslationCollection]
19+
#[ORM\OneToMany(targetEntity: EntityInheritance_ChildEntityClassTranslation::class, mappedBy: 'entity')]
20+
private Collection $extraTranslations;
21+
22+
public function __construct()
23+
{
24+
parent::__construct();
25+
$this->extraTranslations = new ArrayCollection();
26+
}
27+
28+
public function setExtra(TranslatableInterface $extraText): void
29+
{
30+
$this->extraText = $extraText;
31+
}
32+
33+
public function getExtraText(): TranslatableInterface
34+
{
35+
return $this->extraText;
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\EntityInheritance;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
7+
8+
#[ORM\Entity]
9+
class EntityInheritance_ChildEntityClassTranslation
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column]
14+
private ?int $id = null;
15+
16+
#[Polyglot\Locale]
17+
#[ORM\Column]
18+
private string $locale;
19+
20+
#[ORM\ManyToOne(inversedBy: 'extraTranslations')]
21+
private EntityInheritance_ChildEntityClass $entity;
22+
23+
#[ORM\Column]
24+
private string $extraText;
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\StronglyTyped;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
9+
use Webfactory\Bundle\PolyglotBundle\Translatable;
10+
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
11+
12+
#[Polyglot\Locale(primary: 'en_GB')]
13+
#[ORM\Entity]
14+
class StronglyTypedTranslationsTest_Entity
15+
{
16+
#[ORM\Column(type: 'integer')]
17+
#[ORM\Id]
18+
#[ORM\GeneratedValue]
19+
public ?int $id = null;
20+
21+
#[Polyglot\TranslationCollection]
22+
#[ORM\OneToMany(targetEntity: StronglyTypedTranslationsTest_Translation::class, mappedBy: 'entity')]
23+
public Collection $translations;
24+
25+
/**
26+
* @var TranslatableInterface<string>
27+
*/
28+
#[Polyglot\Translatable]
29+
#[ORM\Column(type: 'translatable_string', options: ['use_text_column' => true])]
30+
public TranslatableInterface $text;
31+
32+
public function __construct()
33+
{
34+
$this->text = new Translatable();
35+
$this->translations = new ArrayCollection();
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity\StronglyTyped;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
7+
8+
#[ORM\Entity]
9+
class StronglyTypedTranslationsTest_Translation
10+
{
11+
#[ORM\Id]
12+
#[ORM\GeneratedValue]
13+
#[ORM\Column(type: 'integer')]
14+
public ?int $id = null;
15+
16+
#[Polyglot\Locale]
17+
#[ORM\Column]
18+
public string $locale;
19+
20+
#[ORM\ManyToOne(targetEntity: StronglyTypedTranslationsTest_Entity::class, inversedBy: 'translations')]
21+
public StronglyTypedTranslationsTest_Entity $entity;
22+
23+
#[ORM\Column]
24+
public string $text;
25+
}

0 commit comments

Comments
 (0)