Skip to content

Commit

Permalink
#11783: Add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver committed Jan 8, 2025
1 parent e3cabad commit 1974214
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilterAndIndexedRelation;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="Category_Master")
*/
class Category
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var int
*/
public $id;

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

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

public function __construct(string $name, string $type)
{
$this->name = $name;
$this->type = $type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilterAndIndexedRelation;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;

use function sprintf;

class CategoryTypeSQLFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
{
if ($targetEntity->getName() === Category::class) {
return sprintf('%s.%s = %s', $targetTableAlias, $targetEntity->fieldMappings['type']['fieldName'], $this->getParameter('type'));
}

return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilterAndIndexedRelation;

use Doctrine\Tests\OrmFunctionalTestCase;

final class ChangeFiltersTest extends OrmFunctionalTestCase
{
private const COMPANY_A = 'A';
private const CAT_BAR = 'bar';

Check failure on line 12 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
private const CAT_FOO = 'foo';

Check failure on line 13 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

public function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
Company::class,
Category::class,
]);
}

private function prepareData(): void
{
$cat1 = new Category('cat1', self::CAT_FOO);

Check failure on line 27 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 2 spaces
$cat2 = new Category('cat2', self::CAT_BAR);

Check failure on line 28 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 2 spaces
$companyA = new Company(self::COMPANY_A, [$cat1, $cat2]);

$this->_em->persist($cat1);
$this->_em->persist($cat2);
$this->_em->persist($companyA);
$this->_em->flush();
$this->_em->clear();
}

public function testIndexAliasUpdatedWithUpdatedFilter(): void
{
$this->prepareData();

$company = $this->_em->getRepository(Company::class)->findOneBy([]);

self::assertCount(2, $company->categories);
self::assertEquals([self::CAT_FOO, self::CAT_BAR], $company->categories->map(function (Category $c): string { return $c->type; })->getValues());

Check failure on line 45 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Closure not using "$this" should be declared static.

Check failure on line 45 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Opening brace must be the last content on the line

Check failure on line 45 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Expected 0 lines before "return", found -1.

Check failure on line 45 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Expected 0 lines after "return", found -1.

Check failure on line 45 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Closing brace must be on a line by itself

$this->_em->clear();
$this->_em->getConfiguration()->addFilter(CategoryTypeSQLFilter::class, CategoryTypeSQLFilter::class);
$this->_em->getFilters()->enable(CategoryTypeSQLFilter::class)->setParameter('type', self::CAT_FOO);

$company = $this->_em->getRepository(Company::class)->findOneBy([]);

self::assertCount(1, $company->categories);
self::assertEquals([self::CAT_FOO], $company->categories->map(function (Category $c): string { return $c->type; })->getValues());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilterAndIndexedRelation;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="Order_Master")
*/
class Company
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var int
*/
public $id;

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

/**
* @ORM\ManyToMany(targetEntity="Category", fetch="EAGER", indexBy="type")
*
* @var Collection<int, Category>
*/
public $categories;

/** @param Category[] $categories */
public function __construct(string $name, array $categories)
{
$this->name = $name;

Check failure on line 43 in tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Company.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
$this->categories = new ArrayCollection($categories);
}
}

0 comments on commit 1974214

Please sign in to comment.