forked from doctrine-extensions/DoctrineExtensions
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for doctrine-extensions#2701
- Loading branch information
Showing
3 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gedmo\Tests\Tree\Fixture\Issue2701; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Gedmo\Mapping\Annotation as Gedmo; | ||
use Gedmo\Tree\Entity\Repository\NestedTreeRepository; | ||
use Symfony\Bridge\Doctrine\Types\UlidType; | ||
use Symfony\Component\Uid\Ulid; | ||
|
||
/** | ||
* @Gedmo\Tree(type="nested") | ||
* | ||
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository") | ||
* @ORM\Table(indexes={@ORM\Index(columns={"lft", "rgt"}, name="idx_tree")}) | ||
*/ | ||
#[Gedmo\Tree(type: 'nested')] | ||
#[ORM\Entity(repositoryClass: NestedTreeRepository::class)] | ||
#[ORM\Index(columns: ['lft', 'rgt'], name: 'idx_tree')] | ||
class Category | ||
{ | ||
/** | ||
* @ORM\Column(type="ulid", unique=true) | ||
* @ORM\Id | ||
* @ORM\GeneratedValue(strategy="CUSTOM") | ||
* @ORM\CustomIdGenerator(class="doctrine.ulid_generator") | ||
*/ | ||
#[ORM\Id] | ||
#[ORM\Column(type: UlidType::NAME, unique: true)] | ||
#[ORM\GeneratedValue(strategy: 'CUSTOM')] | ||
#[ORM\CustomIdGenerator(class: 'doctrine.ulid_generator')] | ||
private ?Ulid $id = null; | ||
|
||
/** | ||
* @ORM\Column(length=255) | ||
*/ | ||
#[ORM\Column(length: 255)] | ||
private ?string $name = null; | ||
|
||
/** | ||
* @Gedmo\TreeLeft | ||
* | ||
* @ORM\Column(name="lft", type="integer") | ||
*/ | ||
#[Gedmo\TreeLeft] | ||
#[ORM\Column(name: 'lft', type: Types::INTEGER)] | ||
public ?int $lft; | ||
|
||
/** | ||
* @Gedmo\TreeLevel | ||
* | ||
* @ORM\Column(name="lvl", type="integer") | ||
*/ | ||
#[Gedmo\TreeLevel] | ||
#[ORM\Column(name: 'lvl', type: Types::INTEGER)] | ||
private ?int $lvl; | ||
|
||
/** | ||
* @Gedmo\TreeRight | ||
* | ||
* @ORM\Column(name="rgt", type="integer") | ||
*/ | ||
#[Gedmo\TreeRight] | ||
#[ORM\Column(name: 'rgt', type: Types::INTEGER)] | ||
public ?int $rgt; | ||
|
||
/** | ||
* @Gedmo\TreeRoot | ||
* | ||
* @ORM\ManyToOne(targetEntity="Gedmo\Tests\Tree\Fixture\Issue2701\Category") | ||
* @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE") | ||
*/ | ||
#[Gedmo\TreeRoot] | ||
#[ORM\ManyToOne(targetEntity: self::class)] | ||
#[ORM\JoinColumn(name: 'tree_root', referencedColumnName: 'id', onDelete: 'CASCADE')] | ||
private ?self $root; | ||
|
||
/** | ||
* @Gedmo\TreeParent | ||
* | ||
* @ORM\ManyToOne(targetEntity="Gedmo\Tests\Tree\Fixture\Issue2701\Category", inversedBy="children") | ||
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") | ||
*/ | ||
#[Gedmo\TreeParent] | ||
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] | ||
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', onDelete: 'CASCADE')] | ||
private ?self $parent; | ||
|
||
/** | ||
* @var Collection<int, self>|self[] | ||
* | ||
* @ORM\OneToMany(targetEntity="Gedmo\Tests\Tree\Fixture\Issue2701\Category", mappedBy="parent") | ||
* @ORM\OrderBy({"lft" = "ASC"}) | ||
*/ | ||
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] | ||
#[ORM\OrderBy(['lft' => 'ASC'])] | ||
private Collection $children; | ||
|
||
public function __construct() | ||
{ | ||
$this->children = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?Ulid | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): static | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function setTranslatableLocale(string $locale): void | ||
{ | ||
$this->locale = $locale; | ||
} | ||
|
||
public function getRoot(): ?self | ||
{ | ||
return $this->root; | ||
} | ||
|
||
public function setParent(?self $parent = null): void | ||
{ | ||
$this->parent = $parent; | ||
} | ||
|
||
public function getParent(): ?self | ||
{ | ||
return $this->parent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gedmo\Tests\Tree\Issue; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Mapping\ClassMetadataFactory; | ||
use Doctrine\Bundle\DoctrineBundle\Mapping\MappingDriver; | ||
use Doctrine\Common\EventManager; | ||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\Persistence\Mapping\Driver\MappingDriver as MappingDriverInterface; | ||
use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
use Gedmo\Tests\Tree\Fixture\Issue2701\Category; | ||
use Gedmo\Tree\TreeListener; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator; | ||
use Symfony\Bridge\Doctrine\Types\UlidType; | ||
use Symfony\Component\Uid\Factory\UlidFactory; | ||
|
||
final class Issue2701Test extends BaseTestCaseORM | ||
{ | ||
private TreeListener $listener; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->listener = new TreeListener(); | ||
|
||
$evm = new EventManager(); | ||
$evm->addEventSubscriber($this->listener); | ||
|
||
$config = $this->getDefaultConfiguration(); | ||
$config->setClassMetadataFactoryName(ClassMetadataFactory::class); | ||
|
||
$mappingDriver = $config->getMetadataDriverImpl(); | ||
|
||
assert($mappingDriver instanceof MappingDriverInterface); | ||
|
||
$config->setMetadataDriverImpl(new MappingDriver($mappingDriver, $this->createIdGeneratorLocator())); | ||
|
||
Type::addType(UlidType::NAME, UlidType::class); | ||
|
||
$this->getDefaultMockSqliteEntityManager($evm, $config); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
// To avoid state bleedout between test cases, we will clear the type registry using brute force | ||
$refl = (new \ReflectionClass(Type::class))->getProperty('typeRegistry'); | ||
$refl->setAccessible(true); | ||
$refl->setValue(Type::class, null); | ||
} | ||
|
||
public function testGetNextSiblingsWithoutIdentifierMethod(): void | ||
{ | ||
$food = new Category(); | ||
$food->setName('Food'); | ||
|
||
$fruits = new Category(); | ||
$fruits->setName('Fruits'); | ||
$fruits->setParent($food); | ||
|
||
$vegetables = new Category(); | ||
$vegetables->setName('Vegetables'); | ||
$vegetables->setParent($food); | ||
|
||
$carrots = new Category(); | ||
$carrots->setName('Carrots'); | ||
$carrots->setParent($vegetables); | ||
|
||
$this->em->persist($food); | ||
$this->em->persist($fruits); | ||
$this->em->persist($vegetables); | ||
$this->em->persist($carrots); | ||
$this->em->flush(); | ||
|
||
$categoryRepository = $this->em->getRepository(Category::class); | ||
|
||
static::assertTrue($categoryRepository->verify()); | ||
} | ||
|
||
protected function createIdGeneratorLocator(): ContainerInterface | ||
{ | ||
return new class(new UlidGenerator(new UlidFactory())) implements ContainerInterface { | ||
private UlidGenerator $ulidGenerator; | ||
|
||
public function __construct(UlidGenerator $ulidGenerator) | ||
{ | ||
$this->ulidGenerator = $ulidGenerator; | ||
} | ||
|
||
public function get(string $id) | ||
{ | ||
if ('doctrine.ulid_generator' === $id) { | ||
return $this->ulidGenerator; | ||
} | ||
|
||
throw new class(sprintf('Service ID "%s" not found.', $id)) extends \InvalidArgumentException implements NotFoundExceptionInterface { | ||
}; | ||
} | ||
|
||
public function has(string $id): bool | ||
{ | ||
return 'doctrine.ulid_generator' === $id; | ||
} | ||
}; | ||
} | ||
|
||
protected function getUsedEntityFixtures(): array | ||
{ | ||
return [Category::class]; | ||
} | ||
} |