Skip to content

Commit

Permalink
Update tests to use only attributes to configure Doctrine ORM mappings (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude authored Apr 4, 2024
1 parent 5583c9b commit f2fc6cb
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 270 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
},

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

"config": {
Expand Down
3 changes: 2 additions & 1 deletion tests/Doctrine/TranslatableClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Webfactory\Bundle\PolyglotBundle\Tests\Doctrine;

use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use PHPUnit\Framework\TestCase;
use Webfactory\Bundle\PolyglotBundle\Doctrine\TranslatableClassMetadata;
use Webfactory\Bundle\PolyglotBundle\Tests\TestEntity;
Expand All @@ -28,7 +29,7 @@ private function createMetadata(): TranslatableClassMetadata
$infrastructure = new ORMInfrastructure([
TestEntity::class,
TestEntityTranslation::class,
]);
], mappingDriver: new AttributeDriver([], true));
$entityManager = $infrastructure->getEntityManager();

return TranslatableClassMetadata::parseFromClass(TestEntity::class, $entityManager->getMetadataFactory());
Expand Down
47 changes: 13 additions & 34 deletions tests/Functional/CascadePersistTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,24 @@ public function adding_and_persisting_translations(): void
}
}

/**
* @ORM\Entity
*/
#[Polyglot\Locale(primary: 'en_GB')]
#[ORM\Entity]
class CascadePersistTranslationsTest_Entity
{
/**
* @ORM\Column(type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue]
private ?int $id = null;

/**
* (!) There is *not* cascade="persist" configuration here.
*
* @ORM\OneToMany(targetEntity="CascadePersistTranslationsTest_Translation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \CascadePersistTranslationsTest_Translation::class, mappedBy: 'entity')]
protected Collection $translations;

/**
* @ORM\Column(type="string")
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'string')]
protected string|TranslatableInterface $text;

public function __construct()
Expand All @@ -85,33 +76,21 @@ public function addTranslation(string $locale, mixed $text): void
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class CascadePersistTranslationsTest_Translation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
private string $locale;

/**
* @ORM\ManyToOne(targetEntity="CascadePersistTranslationsTest_Entity", inversedBy="translations")
*/
#[ORM\ManyToOne(targetEntity: \CascadePersistTranslationsTest_Entity::class, inversedBy: 'translations')]
private CascadePersistTranslationsTest_Entity $entity;

/**
* @ORM\Column
*/
#[ORM\Column]
private string $text;
}
95 changes: 26 additions & 69 deletions tests/Functional/EntityInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,39 +101,26 @@ public function testUpdateTranslations(): void
}
}

/**
* @ORM\Entity()
*
* @ORM\InheritanceType(value="SINGLE_TABLE")
*
* @ORM\DiscriminatorMap({"base"="EntityInheritance_BaseEntityClass", "child"="EntityInheritance_ChildEntityClass"})
*
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
*/
#[Polyglot\Locale(primary: 'en_GB')]
#[ORM\Entity]
#[ORM\InheritanceType(value: 'SINGLE_TABLE')]
#[ORM\DiscriminatorMap(['base' => 'EntityInheritance_BaseEntityClass', 'child' => 'EntityInheritance_ChildEntityClass'])]
#[ORM\DiscriminatorColumn(name: 'discriminator', type: 'string')]
class EntityInheritance_BaseEntityClass
{
/**
* @ORM\Column(type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue]
private ?int $id = null;

private string $discriminator;

/**
* @ORM\OneToMany(targetEntity="EntityInheritance_BaseEntityClassTranslation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \EntityInheritance_BaseEntityClassTranslation::class, mappedBy: 'entity')]
private Collection $translations;

/**
* @ORM\Column(type="string")
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'string')]
private TranslatableInterface|string|null $text = null;

public function __construct()
Expand All @@ -157,52 +144,34 @@ public function getText(): TranslatableInterface
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class EntityInheritance_BaseEntityClassTranslation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
private string $locale;

/**
* @ORM\ManyToOne(targetEntity="EntityInheritance_BaseEntityClass", inversedBy="translations")
*/
#[ORM\ManyToOne(targetEntity: \EntityInheritance_BaseEntityClass::class, inversedBy: 'translations')]
private EntityInheritance_BaseEntityClass $entity;

/**
* @ORM\Column()
*/
#[ORM\Column]
private string $text;
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class EntityInheritance_ChildEntityClass extends EntityInheritance_BaseEntityClass
{
/**
* @ORM\Column(type="string")
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'string')]
private TranslatableInterface|string|null $extraText = null;

/**
* @ORM\OneToMany(targetEntity="EntityInheritance_ChildEntityClassTranslation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \EntityInheritance_ChildEntityClassTranslation::class, mappedBy: 'entity')]
private Collection $extraTranslations;

public function __construct()
Expand All @@ -222,33 +191,21 @@ public function getExtraText(): TranslatableInterface
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class EntityInheritance_ChildEntityClassTranslation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
private string $locale;

/**
* @ORM\ManyToOne(targetEntity="EntityInheritance_ChildEntityClass", inversedBy="extraTranslations")
*/
#[ORM\ManyToOne(targetEntity: \EntityInheritance_ChildEntityClass::class, inversedBy: 'extraTranslations')]
private EntityInheritance_ChildEntityClass $entity;

/**
* @ORM\Column()
*/
#[ORM\Column]
private string $extraText;
}
3 changes: 2 additions & 1 deletion tests/Functional/FunctionalTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use PHPUnit\Framework\TestCase;
use Webfactory\Bundle\PolyglotBundle\Doctrine\PolyglotListener;
use Webfactory\Bundle\PolyglotBundle\Doctrine\TranslatableStringType;
Expand All @@ -22,7 +23,7 @@ protected function setupOrmInfrastructure(array $classes): void
if (!Type::hasType(TranslatableStringType::NAME)) {
Type::addType(TranslatableStringType::NAME, TranslatableStringType::class);
}
$this->infrastructure = ORMInfrastructure::createOnlyFor($classes);
$this->infrastructure = ORMInfrastructure::createOnlyFor($classes, mappingDriver: new AttributeDriver([], true));
$this->entityManager = $this->infrastructure->getEntityManager();
$this->defaultLocaleProvider = new DefaultLocaleProvider('en_GB');

Expand Down
47 changes: 13 additions & 34 deletions tests/Functional/StronglyTypedTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,24 @@ public function flush_updated_entity_two_times_does_not_update(): void
}
}

/**
* @ORM\Entity
*/
#[Polyglot\Locale(primary: 'en_GB')]
#[ORM\Entity]
class StronglyTypedTranslationsTest_Entity
{
/**
* @ORM\Column(type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue]
public ?int $id = null;

/**
* @ORM\OneToMany(targetEntity="StronglyTypedTranslationsTest_Translation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \StronglyTypedTranslationsTest_Translation::class, mappedBy: 'entity')]
public Collection $translations;

/**
* @var TranslatableInterface<string>
*
* @ORM\Column(type="translatable_string", options={"use_text_column": true})
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'translatable_string', options: ['use_text_column' => true])]
public TranslatableInterface $text;

public function __construct()
Expand All @@ -224,33 +215,21 @@ public function __construct()
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class StronglyTypedTranslationsTest_Translation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
public string $locale;

/**
* @ORM\ManyToOne(targetEntity="StronglyTypedTranslationsTest_Entity", inversedBy="translations")
*/
#[ORM\ManyToOne(targetEntity: \StronglyTypedTranslationsTest_Entity::class, inversedBy: 'translations')]
public StronglyTypedTranslationsTest_Entity $entity;

/**
* @ORM\Column
*/
#[ORM\Column]
public string $text;
}
Loading

0 comments on commit f2fc6cb

Please sign in to comment.