|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Tests\ORM\Functional\Ticket; |
| 6 | + |
| 7 | +use Doctrine\Common\Collections\ArrayCollection; |
| 8 | +use Doctrine\Common\Collections\Collection; |
| 9 | +use Doctrine\Common\Collections\Criteria; |
| 10 | +use Doctrine\Common\Collections\Expr\Comparison; |
| 11 | +use Doctrine\ORM\Mapping as ORM; |
| 12 | +use Doctrine\Tests\OrmFunctionalTestCase; |
| 13 | +use Generator; |
| 14 | + |
| 15 | +class GH3591Test extends OrmFunctionalTestCase |
| 16 | +{ |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + $this->setUpEntitySchema([ |
| 22 | + GH3591Entity::class, |
| 23 | + GH3591CollectionEntry::class, |
| 24 | + ]); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @dataProvider provideFieldNamesAndInitializationState |
| 29 | + */ |
| 30 | + public function testMatchingApiOnField(string $fieldName, bool $collectionInitialized): void |
| 31 | + { |
| 32 | + $criteria = $this->createComparisonCriteria($fieldName, 'test value'); |
| 33 | + $entity = $this->createAndLoadEntityWithCollectionEntry('test value'); |
| 34 | + if ($collectionInitialized) { |
| 35 | + $entity->entries->initialize(); |
| 36 | + } |
| 37 | + |
| 38 | + $matches = $entity->entries->matching($criteria); |
| 39 | + |
| 40 | + self::assertCount(1, $matches); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return Generator<string> |
| 45 | + */ |
| 46 | + public function provideFieldNamesAndInitializationState(): Generator |
| 47 | + { |
| 48 | + yield ['privateField', false]; |
| 49 | + yield ['privateField', true]; |
| 50 | + yield ['protectedField', false]; |
| 51 | + yield ['protectedField', true]; |
| 52 | + yield ['publicField', false]; |
| 53 | + yield ['publicField', true]; |
| 54 | + yield ['fieldWithAwkwardGetter', false]; |
| 55 | + yield ['fieldWithAwkwardGetter', true]; |
| 56 | + } |
| 57 | + |
| 58 | + private function createAndLoadEntityWithCollectionEntry(string $entryFieldValue): GH3591Entity |
| 59 | + { |
| 60 | + $entity = new GH3591Entity(); |
| 61 | + $entry = new GH3591CollectionEntry($entryFieldValue); |
| 62 | + |
| 63 | + $entity->addEntry($entry); |
| 64 | + $this->_em->persist($entity); |
| 65 | + $this->_em->flush(); |
| 66 | + $this->_em->clear(); |
| 67 | + |
| 68 | + return $this->_em->find(GH3591Entity::class, $entity->id); |
| 69 | + } |
| 70 | + |
| 71 | + private function createComparisonCriteria(string $fieldName, string $expectedValue): Criteria |
| 72 | + { |
| 73 | + $expr = new Comparison($fieldName, '=', $expectedValue); |
| 74 | + $criteria = new Criteria(); |
| 75 | + $criteria->where($expr); |
| 76 | + |
| 77 | + return $criteria; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * @ORM\Entity() |
| 83 | + */ |
| 84 | +class GH3591Entity |
| 85 | +{ |
| 86 | + /** |
| 87 | + * @ORM\Id |
| 88 | + * @ORM\Column(type="integer") |
| 89 | + * @ORM\GeneratedValue |
| 90 | + * |
| 91 | + * @var int |
| 92 | + */ |
| 93 | + public $id; |
| 94 | + |
| 95 | + /** |
| 96 | + * @ORM\OneToMany(targetEntity="GH3591CollectionEntry", mappedBy="entity", cascade={"persist"}) |
| 97 | + * |
| 98 | + * @var Collection<int, GH3591CollectionEntry> |
| 99 | + */ |
| 100 | + public $entries; |
| 101 | + |
| 102 | + public function __construct() |
| 103 | + { |
| 104 | + $this->entries = new ArrayCollection(); |
| 105 | + } |
| 106 | + |
| 107 | + public function addEntry(GH3591CollectionEntry $child): void |
| 108 | + { |
| 109 | + if (! $this->entries->contains($child)) { |
| 110 | + $this->entries->add($child); |
| 111 | + $child->setEntity($this); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @ORM\Entity() |
| 118 | + */ |
| 119 | +class GH3591CollectionEntry |
| 120 | +{ |
| 121 | + /** |
| 122 | + * @ORM\Id |
| 123 | + * @ORM\Column(type="integer") |
| 124 | + * @ORM\GeneratedValue |
| 125 | + * |
| 126 | + * @var int |
| 127 | + */ |
| 128 | + private $id; |
| 129 | + |
| 130 | + /** |
| 131 | + * @ORM\ManyToOne(targetEntity="GH3591Entity", inversedBy="entries") |
| 132 | + * |
| 133 | + * @var GH3591Entity |
| 134 | + */ |
| 135 | + private $entity; |
| 136 | + |
| 137 | + /** |
| 138 | + * @ORM\Column(type="string") |
| 139 | + * |
| 140 | + * @var string |
| 141 | + */ |
| 142 | + private $privateField; |
| 143 | + |
| 144 | + /** |
| 145 | + * @ORM\Column(type="string") |
| 146 | + * |
| 147 | + * @var string |
| 148 | + */ |
| 149 | + protected $protectedField; |
| 150 | + |
| 151 | + /** |
| 152 | + * @ORM\Column(type="string") |
| 153 | + * |
| 154 | + * @var string |
| 155 | + */ |
| 156 | + public $publicField; |
| 157 | + |
| 158 | + /** |
| 159 | + * @ORM\Column(type="string") |
| 160 | + * |
| 161 | + * @var string |
| 162 | + */ |
| 163 | + public $fieldWithAwkwardGetter; |
| 164 | + |
| 165 | + public function __construct($value) |
| 166 | + { |
| 167 | + $this->privateField = $value; |
| 168 | + $this->protectedField = $value; |
| 169 | + $this->publicField = $value; |
| 170 | + $this->fieldWithAwkwardGetter = $value; |
| 171 | + } |
| 172 | + |
| 173 | + public function setEntity(GH3591Entity $entity): void |
| 174 | + { |
| 175 | + $this->entity = $entity; |
| 176 | + $entity->addEntry($this); |
| 177 | + } |
| 178 | + |
| 179 | + public function fieldWithAwkwardGetter(): string |
| 180 | + { |
| 181 | + return 'not what you expect' . $this->fieldWithAwkwardGetter; |
| 182 | + } |
| 183 | +} |
0 commit comments