Skip to content

Commit

Permalink
tt
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Mar 1, 2024
1 parent e6e8c22 commit ae45214
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 238 deletions.
2 changes: 1 addition & 1 deletion src/Doctrine/Orm/AbstractPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(DoctrinePaginator $paginator)
{
$query = $paginator->getQuery();

if (null === ($firstResult = $query->getFirstResult()) || null === $maxResults = $query->getMaxResults()) { // @phpstan-ignore-line
if (null === ($firstResult = $query->getFirstResult()) || $firstResult < 0 || null === $maxResults = $query->getMaxResults()) { // @phpstan-ignore-line
throw new InvalidArgumentException(sprintf('"%1$s::setFirstResult()" or/and "%1$s::setMaxResults()" was/were not applied to the query.', Query::class));
}

Expand Down
3 changes: 2 additions & 1 deletion src/Doctrine/Orm/Filter/ExistsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
Expand Down Expand Up @@ -223,7 +224,7 @@ protected function isNullableField(string $property, string $resourceClass): boo
*
* @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
*/
private function isAssociationNullable(array $associationMapping): bool
private function isAssociationNullable(AssociationMapping|array $associationMapping): bool
{
if (!empty($associationMapping['id'])) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Doctrine\Persistence\ManagerRegistry;

/**
Expand Down Expand Up @@ -70,7 +71,11 @@ public function create(string $resourceClass, string $property, array $options =
if ($doctrineClassMetadata instanceof ClassMetadata && \in_array($property, $doctrineClassMetadata->getFieldNames(), true)) {
/** @var mixed[] */
$fieldMapping = $doctrineClassMetadata->getFieldMapping($property);
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault());
if (class_exists(FieldMapping::class) && $fieldMapping instanceof FieldMapping) {
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping->default ?? $propertyMetadata->getDefault());
} else {
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault());
}
}

return $propertyMetadata;
Expand Down
96 changes: 50 additions & 46 deletions tests/Doctrine/Orm/Extension/EagerLoadingExtensionTest.php

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions tests/Doctrine/Orm/Extension/OrderExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testApplyToCollectionWithValidOrder(): void
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);

$queryBuilderProphecy->getDQLPart('orderBy')->shouldBeCalled()->willReturn([]);
$queryBuilderProphecy->addOrderBy('o.name', 'asc')->shouldBeCalled();
$queryBuilderProphecy->addOrderBy('o.name', 'asc')->shouldBeCalled()->willReturn($queryBuilderProphecy);

$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
Expand Down Expand Up @@ -79,7 +79,7 @@ public function testApplyToCollectionWithOrderOverridden(): void
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);

$queryBuilderProphecy->getDQLPart('orderBy')->shouldBeCalled()->willReturn([]);
$queryBuilderProphecy->addOrderBy('o.foo', 'DESC')->shouldBeCalled();
$queryBuilderProphecy->addOrderBy('o.foo', 'DESC')->shouldBeCalled()->willReturn($queryBuilderProphecy);

$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
Expand All @@ -100,8 +100,8 @@ public function testApplyToCollectionWithOrderOverriddenWithNoDirection(): void
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);

$queryBuilderProphecy->getDQLPart('orderBy')->shouldBeCalled()->willReturn([]);
$queryBuilderProphecy->addOrderBy('o.foo', 'ASC')->shouldBeCalled();
$queryBuilderProphecy->addOrderBy('o.bar', 'DESC')->shouldBeCalled();
$queryBuilderProphecy->addOrderBy('o.foo', 'ASC')->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilderProphecy->addOrderBy('o.bar', 'DESC')->shouldBeCalled()->willReturn($queryBuilderProphecy);

$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
Expand All @@ -123,8 +123,8 @@ public function testApplyToCollectionWithOrderOverriddenWithAssociation(): void

$queryBuilderProphecy->getDQLPart('orderBy')->shouldBeCalled()->willReturn([]);
$queryBuilderProphecy->getDQLPart('join')->willReturn(['o' => []])->shouldBeCalled();
$queryBuilderProphecy->innerJoin('o.author', 'author_a1', null, null)->shouldBeCalled();
$queryBuilderProphecy->addOrderBy('author_a1.name', 'ASC')->shouldBeCalled();
$queryBuilderProphecy->innerJoin('o.author', 'author_a1', null, null)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilderProphecy->addOrderBy('author_a1.name', 'ASC')->shouldBeCalled()->willReturn($queryBuilderProphecy);

$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
Expand All @@ -145,7 +145,7 @@ public function testApplyToCollectionWithOrderOverriddenWithEmbeddedAssociation(
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->getDQLPart('orderBy')->shouldBeCalled()->willReturn([]);
$queryBuilderProphecy->getRootAliases()->willReturn(['o']);
$queryBuilderProphecy->addOrderBy('o.embeddedDummy.dummyName', 'DESC')->shouldBeCalled();
$queryBuilderProphecy->addOrderBy('o.embeddedDummy.dummyName', 'DESC')->shouldBeCalled()->willReturn($queryBuilderProphecy);

$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['id']);
Expand Down
16 changes: 8 additions & 8 deletions tests/Doctrine/Orm/Extension/PaginationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testApplyToCollection(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(40)->willReturn($queryBuilderProphecy)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(40)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(40)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand All @@ -79,7 +79,7 @@ public function testApplyToCollectionWithItemPerPageZero(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(0)->willReturn($queryBuilderProphecy)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(0)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(0)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand All @@ -101,7 +101,7 @@ public function testApplyToCollectionWithItemPerPageZeroAndPage2(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(0)->willReturn($queryBuilderProphecy)->shouldNotBeCalled();
$queryBuilderProphecy->setMaxResults(0)->shouldNotBeCalled();
$queryBuilderProphecy->setMaxResults(0)->shouldNotBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand All @@ -123,7 +123,7 @@ public function testApplyToCollectionWithItemPerPageLessThan0(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(40)->willReturn($queryBuilderProphecy)->shouldNotBeCalled();
$queryBuilderProphecy->setMaxResults(40)->shouldNotBeCalled();
$queryBuilderProphecy->setMaxResults(40)->shouldNotBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand All @@ -142,7 +142,7 @@ public function testApplyToCollectionWithItemPerPageTooHigh(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(300)->willReturn($queryBuilderProphecy)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(300)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(300)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand All @@ -158,7 +158,7 @@ public function testApplyToCollectionWithGraphql(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(10)->willReturn($queryBuilderProphecy)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(5)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(5)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand All @@ -174,7 +174,7 @@ public function testApplyToCollectionNofilters(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(0)->willReturn($queryBuilderProphecy)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(30)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(30)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand Down Expand Up @@ -230,7 +230,7 @@ public function testApplyToCollectionWithMaximumItemsPerPage(): void

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->setFirstResult(0)->willReturn($queryBuilderProphecy)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(80)->shouldBeCalled();
$queryBuilderProphecy->setMaxResults(80)->shouldBeCalled()->willReturn($queryBuilderProphecy);
$queryBuilder = $queryBuilderProphecy->reveal();

$extension = new PaginationExtension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyPropertyWithDefaultValue;
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function testCreateIsWritable(): void
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$classMetadata = $this->prophesize(ClassMetadata::class);
$classMetadata = $this->prophesize(ORMClassMetadata::class);
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
$classMetadata->getFieldNames()->shouldBeCalled()->willReturn([]);

Expand All @@ -100,11 +101,20 @@ public function testCreateWithDefaultOption(): void
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(DummyPropertyWithDefaultValue::class, 'dummyDefaultOption', [])->shouldBeCalled()->willReturn($propertyMetadata);

$classMetadata = new ORMClassMetadata(DummyPropertyWithDefaultValue::class);
// @phpstan-ignore-next-line
$classMetadata->fieldMappings = [
'dummyDefaultOption' => ['options' => ['default' => 'default value']],
];
$classMetadata = $this->prophesize(ORMClassMetadata::class);
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
$classMetadata->getFieldNames()->shouldBeCalled()->willReturn(['dummyDefaultOption']);
if (class_exists(FieldMapping::class)) {
$fieldMapping = new FieldMapping('string', 'dummyDefaultOption', 'dummyDefaultOption');
$fieldMapping->default = 'default value';
$classMetadata->fieldMappings = [$fieldMapping];
$classMetadata->getFieldMapping('dummyDefaultOption')->willReturn($fieldMapping);
} else {
// @phpstan-ignore-next-line
$classMetadata->fieldMappings = [
'dummyDefaultOption' => ['options' => ['default' => 'default value']],
];
}

$objectManager = $this->prophesize(ObjectManager::class);
$objectManager->getClassMetadata(DummyPropertyWithDefaultValue::class)->shouldBeCalled()->willReturn($classMetadata);
Expand All @@ -126,7 +136,7 @@ public function testCreateClassMetadataInfo(): void
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactory->create(Dummy::class, 'id', [])->shouldBeCalled()->willReturn($propertyMetadata);

$classMetadata = $this->prophesize(ClassMetadata::class);
$classMetadata = $this->prophesize(ORMClassMetadata::class);
$classMetadata->getIdentifier()->shouldBeCalled()->willReturn(['id']);
$classMetadata->isIdentifierNatural()->shouldBeCalled()->willReturn(true);
$classMetadata->getFieldNames()->shouldBeCalled()->willReturn([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Model\Car;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\InverseSideMapping;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Doctrine\ORM\Mapping\InverseSideMapping;

final class DoctrineOrmLinkFactoryTest extends TestCase
{
Expand All @@ -53,6 +54,12 @@ public function testCreateLinksFromRelations(): void
$classMetadataProphecy->getAssociationMappedByTargetField('relatedNonResource')->willReturn('dummies');
$classMetadataProphecy->getAssociationMappedByTargetField('relatedDummy')->willReturn(null);
$classMetadataProphecy->getAssociationMappedByTargetField('relatedDummies')->willReturn('dummies');
if (class_exists(InverseSideMapping::class)) {
$classMetadataProphecy->getAssociationMapping('relatedNonResource')->willReturn(new class('a', 'a', 'a') extends AssociationMapping {});
$classMetadataProphecy->getAssociationMapping('relatedDummy')->willReturn(new class('a', 'a', 'a') extends AssociationMapping {});
$classMetadataProphecy->getAssociationMapping('relatedDummies')->willReturn(new class('a', 'a', 'a') extends InverseSideMapping {});
}

$entityManagerProphecy = $this->prophesize(EntityManagerInterface::class);
$entityManagerProphecy->getClassMetadata($class)->willReturn($classMetadataProphecy->reveal());
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
Expand All @@ -74,34 +81,6 @@ public function testCreateLinksFromRelations(): void
),
], $doctrineOrmLinkFactory->createLinksFromRelations($operation));
}

public function testCreateLinksFromRelationsDoctrine3(): void
{
if (!class_exists(InverseSideMapping::class)) {
$this->markTestSkipped();
}

$class = Dummy::class;
$operation = (new Get())->withClass($class);

$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
$classMetadataProphecy->hasAssociation('noMappedBy')->willReturn(true);
$classMetadataProphecy->getAssociationTargetClass('noMappedBy')->willReturn('NoMappedByClass');
$classMetadataProphecy->getAssociationMappedByTargetField('noMappedBy')->shouldNotBeCalled();
$entityManagerProphecy = $this->prophesize(EntityManagerInterface::class);
$entityManagerProphecy->getClassMetadata($class)->willReturn($classMetadataProphecy->reveal());
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
$managerRegistryProphecy->getManagerForClass($class)->willReturn($entityManagerProphecy->reveal());
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create($class)->willReturn(new PropertyNameCollection(['noMappedBy']));
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->isResourceClass(Car::class)->shouldNotBeCalled();

$doctrineOrmLinkFactory = new DoctrineOrmLinkFactory($managerRegistryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $resourceClassResolverProphecy->reveal(), new LinkFactoryStub());

self::assertEquals([
], $doctrineOrmLinkFactory->createLinksFromRelations($operation));
}
}

class LinkFactoryStub implements LinkFactoryInterface, PropertyLinkFactoryInterface
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Orm/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use ApiPlatform\Doctrine\Orm\Paginator;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Tests\Fixtures\Query;
use Doctrine\ORM\Query;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
Expand Down Expand Up @@ -78,7 +78,7 @@ private function getPaginator(int $firstResult = 1, int $maxResults = 15, int $t
private function getPaginatorWithMalformedQuery(bool $maxResults = false): void
{
$query = $this->prophesize(Query::class);
$query->getFirstResult()->willReturn($maxResults ? 42 : null)->shouldBeCalled();
$query->getFirstResult()->willReturn($maxResults ? 42 : -1)->shouldBeCalled();

if ($maxResults) {
$query->getMaxResults()->willReturn(null)->shouldBeCalled();
Expand Down

0 comments on commit ae45214

Please sign in to comment.