Skip to content

Commit

Permalink
minor #2946 Fixed some failing tests because of recent Doctrine chang…
Browse files Browse the repository at this point in the history
…es (javiereguiluz)

This PR was squashed before being merged into the 2.0.x-dev branch (closes #2946).

Discussion
----------

Fixed some failing tests because of recent Doctrine changes

Tries to fix some failing tests caused by this Doctrine change: doctrine/orm#7528

Commits
-------

5b9212e Fixed some failing tests because of recent Doctrine changes
  • Loading branch information
javiereguiluz committed Sep 30, 2019
2 parents daf1bf2 + 5b9212e commit d35a122
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 15 deletions.
14 changes: 7 additions & 7 deletions tests/Form/Filter/Type/ArrayFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testSubmitAndFilter($submittedData, $data, array $options, strin
$filter->filter($this->qb, $form, ['property' => 'foo', 'dataType' => 'array']);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

public function getDataProvider(): iterable
Expand All @@ -36,7 +36,7 @@ public function getDataProvider(): iterable
['comparison' => 'like', 'value' => ['bar']],
[],
'SELECT o FROM Object o WHERE o.foo like :foo_1',
[new Parameter('foo_1', '%"bar"%', \PDO::PARAM_STR)],
[new Parameter('foo_1', '%"bar"%')],
];

yield [
Expand All @@ -45,8 +45,8 @@ public function getDataProvider(): iterable
[],
'SELECT o FROM Object o WHERE o.foo not like :foo_1 OR o.foo not like :foo_2 OR o.foo IS NULL',
[
new Parameter('foo_1', '%"foo"%', \PDO::PARAM_STR),
new Parameter('foo_2', '%"bar"%', \PDO::PARAM_STR),
new Parameter('foo_1', '%"foo"%'),
new Parameter('foo_2', '%"bar"%'),
],
];

Expand Down Expand Up @@ -79,7 +79,7 @@ public function getDataProvider(): iterable
],
],
'SELECT o FROM Object o WHERE o.foo like :foo_1',
[new Parameter('foo_1', '%"b"%', \PDO::PARAM_STR)],
[new Parameter('foo_1', '%"b"%')],
];

yield [
Expand All @@ -93,8 +93,8 @@ public function getDataProvider(): iterable
],
'SELECT o FROM Object o WHERE o.foo not like :foo_1 OR o.foo not like :foo_2 OR o.foo IS NULL',
[
new Parameter('foo_1', '%"a"%', \PDO::PARAM_STR),
new Parameter('foo_2', '%"c"%', \PDO::PARAM_STR),
new Parameter('foo_1', '%"a"%'),
new Parameter('foo_2', '%"c"%'),
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Filter/Type/BooleanFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testSubmitAndFilter($submittedData, $data, string $dql, array $p
$filter->filter($this->qb, $form, ['property' => 'foo']);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

public function getDataProvider(): iterable
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Filter/Type/ChoiceFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testSubmitAndFilter($submittedData, $data, array $options, strin
$filter->filter($this->qb, $form, ['property' => 'foo']);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

public function getDataProvider(): iterable
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Filter/Type/ComparisonFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testSubmitAndFilter($submittedData, $data, $options, string $dql
$filter->filter($this->qb, $form, ['property' => 'foo']);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

public function getDataProvider(): iterable
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Filter/Type/DateTimeFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testSubmitAndFilter($submittedData, $data, $options, string $dql
$filter->filter($this->qb, $form, ['property' => 'foo']);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
} else {
$this->assertSame($expectedError, $form->getTransformationFailure()->getMessage());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Form/Filter/Type/EntityFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testSubmitAndFilterToOneAssociationType($submittedData, $data, a
$filter->filter($this->qb, $form, ['property' => 'foo', 'dataType' => 'association', 'associationType' => ClassMetadata::TO_ONE]);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

/**
Expand All @@ -95,7 +95,7 @@ public function testFilterToManyAssociationType($submittedData, $data, array $op
$filter->filter($this->qb, $form, ['property' => 'foo', 'dataType' => 'association', 'associationType' => ClassMetadata::TO_MANY]);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

public function getDataProviderToOneAssoc(): iterable
Expand Down
40 changes: 40 additions & 0 deletions tests/Form/Filter/Type/FilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Form\Filter\FilterRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Form\Filter\Type\ComparisonFilterType;
use PHPUnit\Framework\AssertionFailedError;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Component\Form\Test\TypeTestCase;

Expand Down Expand Up @@ -38,4 +39,43 @@ protected function createQueryBuilder(): QueryBuilder

return $qb;
}

// this is needed because some Doctrine versions changed some internals
// and we can't use this code: $this->assertEquals($params, $this->qb->getParameters()->toArray());
// (see https://github.com/doctrine/orm/pull/7528)
protected function assertSameDoctrineParams(array $expectedParams, array $actualParams)
{
for ($i = 0; $i < \count($expectedParams); ++$i) {
$expectedParam = $expectedParams[$i];
$actualParam = $actualParams[$i];

$namesAreDifferent = $expectedParam->getName() !== $actualParam->getName();
$typesAreDifferent = $expectedParam->getType() !== $actualParam->getType();

$expectedValue = $expectedParam->getValue();
$actualValue = $actualParam->getValue();
if ($expectedValue instanceof \DateInterval) {
$valuesAreDifferent = !$this->dateIntervalsAreEqual($expectedValue, $actualValue);
} elseif (\is_object($expectedValue)) {
$valuesAreDifferent = $expectedParam->getValue() != $actualParam->getValue();
} else {
$valuesAreDifferent = $expectedParam->getValue() !== $actualParam->getValue();
}

if ($namesAreDifferent || $valuesAreDifferent || $typesAreDifferent) {
throw new AssertionFailedError(sprintf('The "%s" and "%s" Doctrine parameters are not the same.', $expectedParam->getName(), $actualParam->getName()));
}
}
}

private function dateIntervalsAreEqual(\DateInterval $a, \DateInterval $b): bool
{
$startDate1 = date_create();
$startDate2 = clone $startDate1;

$endDate1 = $startDate1->add($a);
$endDate2 = $startDate2->add($b);

return $endDate1 == $endDate2;
}
}
2 changes: 1 addition & 1 deletion tests/Form/Filter/Type/NumericFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testSubmitAndFilter($submittedData, $data, $options, string $dql
$filter->filter($this->qb, $form, ['property' => 'foo']);
$this->assertSame(static::FILTER_TYPE, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
} else {
$this->assertSame($expectedError, $form->getTransformationFailure()->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Filter/Type/TextFilterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testSubmitAndFilter($submittedData, $data, string $dql, array $p
$filter->filter($this->qb, $form, ['property' => 'foo']);
$this->assertSame(ComparisonFilterType::class, \get_class($filter));
$this->assertSame($dql, $this->qb->getDQL());
$this->assertEquals($params, $this->qb->getParameters()->toArray());
$this->assertSameDoctrineParams($params, $this->qb->getParameters()->toArray());
}

public function getDataProvider(): iterable
Expand Down

0 comments on commit d35a122

Please sign in to comment.