Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectable should compare field values, not call getters for initialized collections #11160

Open
wants to merge 1 commit into
base: 2.20.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 229 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH3591Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;
use Generator;

class GH3591Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
GH3591Entity::class,
GH3591CollectionEntry::class,
]);
}

/**
* @dataProvider provideFieldNamesAndInitializationState
*/
public function testMatchingApiOnUninitializedCollection(string $fieldName): void
{
$criteria = $this->createComparisonCriteria($fieldName, 'test value');
$entity = $this->createAndLoadEntityWithCollectionEntry('test value');

$matches = $entity->entries->matching($criteria);

self::assertCount(1, $matches);
}

/**
* @dataProvider provideFieldNamesAndInitializationState
*/
public function testMatchingApiOnInitializedCollection(string $fieldName): void
{
$criteria = $this->createComparisonCriteria($fieldName, 'test value');
$entity = $this->createAndLoadEntityWithCollectionEntry('test value');
$entity->entries->initialize();

$matches = $entity->entries->matching($criteria);

self::assertCount(1, $matches);
}

/**
* @dataProvider provideFieldNamesAndInitializationState
*/
public function testMatchingApiOnUnpersistedCollection(string $fieldName): void
{
$criteria = $this->createComparisonCriteria($fieldName, 'test value');
$entity = new GH3591Entity();

Check failure on line 60 in tests/Tests/ORM/Functional/Ticket/GH3591Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$entry = new GH3591CollectionEntry('test value');

Check failure on line 61 in tests/Tests/ORM/Functional/Ticket/GH3591Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 2 spaces
$entity->addEntry($entry);

$matches = $entity->entries->matching($criteria);

self::assertCount(1, $matches);
}

/**
* @return Generator<string>
*/
public function provideFieldNamesAndInitializationState(): Generator
{
yield ['privateField'];
yield ['protectedField'];
yield ['publicField'];
yield ['fieldWithAwkwardGetter'];
yield ['duplicatePrivateField'];
}

private function createAndLoadEntityWithCollectionEntry(string $entryFieldValue): GH3591Entity
{
$entity = new GH3591Entity();
$entry = new GH3591CollectionEntry($entryFieldValue);

$entity->addEntry($entry);
$this->_em->persist($entity);
$this->_em->flush();
$this->_em->clear();

return $this->_em->find(GH3591Entity::class, $entity->id);
}

private function createComparisonCriteria(string $fieldName, string $expectedValue): Criteria
{
$expr = new Comparison($fieldName, '=', $expectedValue);
$criteria = new Criteria();
$criteria->where($expr);

return $criteria;
}
}

/**
* @ORM\Entity()
*/
class GH3591Entity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
public $id;

/**
* @ORM\OneToMany(targetEntity="GH3591CollectionEntry", mappedBy="entity", cascade={"persist"})
*
* @var Collection<int, GH3591CollectionEntry>
*/
public $entries;

public function __construct()
{
$this->entries = new ArrayCollection();
}

public function addEntry(GH3591CollectionEntry $child): void
{
if (! $this->entries->contains($child)) {
$this->entries->add($child);
$child->setEntity($this);
}
}
}

/**
* @ORM\MappedSuperclass()
*/
class GH3591CollectionEntrySuperclass
{
/**
* @ORM\Column(type="string")
*
* @var string
*/
private $duplicatePrivateField;

public function __construct(string $duplicatePrivateField)
{
$this->duplicatePrivateField = $duplicatePrivateField;
}
}

/**
* @ORM\Entity()
*/
class GH3591CollectionEntry extends GH3591CollectionEntrySuperclass
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="GH3591Entity", inversedBy="entries")
*
* @var GH3591Entity
*/
private $entity;

/**
* @ORM\Column(type="string")
*
* @var string
*/
private $privateField;

/**
* @ORM\Column(type="string")
*
* @var string
*/
protected $protectedField;

/**
* @ORM\Column(type="string")
*
* @var string
*/
public $publicField;

/**
* @ORM\Column(type="string")
*
* @var string
*/
public $fieldWithAwkwardGetter;

private $duplicatePrivateField;

Check failure on line 206 in tests/Tests/ORM/Functional/Ticket/GH3591Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Property \Doctrine\Tests\ORM\Functional\Ticket\GH3591CollectionEntry::$duplicatePrivateField does not have @var annotation for its value.

public function __construct($value)
{
$this->privateField = $value;
$this->protectedField = $value;
$this->publicField = $value;
$this->fieldWithAwkwardGetter = $value;

parent::__construct($value);

Check failure on line 215 in tests/Tests/ORM/Functional/Ticket/GH3591Test.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.3)

Expected 1 line after "parent", found 0.
$this->duplicatePrivateField = 'this is a transient field';
}

public function setEntity(GH3591Entity $entity): void
{
$this->entity = $entity;
$entity->addEntry($this);
}

public function fieldWithAwkwardGetter(): string
{
return 'not what you expect' . $this->fieldWithAwkwardGetter;
}
}
Loading