|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the API Platform project. |
| 4 | + * |
| 5 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace ApiPlatform\Core\Tests\Doctrine\Orm\Extension; |
| 12 | + |
| 13 | +use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension; |
| 14 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 15 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 16 | +use Doctrine\ORM\QueryBuilder; |
| 17 | +use Doctrine\ORM\EntityManager; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Amrouche Hamza <hamza.simperfit@gmail.com> |
| 21 | + */ |
| 22 | +class EagerLoadingExtensionTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + public function testApplyToCollection() |
| 25 | + { |
| 26 | + $queryBuilderProphecy = $this->prophesize(QueryBuilder::class); |
| 27 | + |
| 28 | + $classMetadataProphecy = $this->prophesize(ClassMetadata::class); |
| 29 | + $classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([0 => 'dummyRelation']); |
| 30 | + $classMetadataProphecy->associationMappings = ['dummyRelation' => ['fetch' => 3]]; |
| 31 | + $emProphecy = $this->prophesize(EntityManager::class); |
| 32 | + $emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal()); |
| 33 | + $queryBuilderProphecy->leftJoin('o.dummyRelation', 'a0')->shouldBeCalled(1); |
| 34 | + $queryBuilderProphecy->addSelect('a0')->shouldBeCalled(1); |
| 35 | + |
| 36 | + $em = $queryBuilderProphecy->getEntityManager()->shouldBeCalled(1)->willReturn($emProphecy->reveal()); |
| 37 | + |
| 38 | + $queryBuilder = $queryBuilderProphecy->reveal(); |
| 39 | + $orderExtensionTest = new EagerLoadingExtension(); |
| 40 | + $orderExtensionTest->applyToCollection($queryBuilder, Dummy::class); |
| 41 | + } |
| 42 | + |
| 43 | + public function testApplyToItem() |
| 44 | + { |
| 45 | + $queryBuilderProphecy = $this->prophesize(QueryBuilder::class); |
| 46 | + |
| 47 | + $classMetadataProphecy = $this->prophesize(ClassMetadata::class); |
| 48 | + $classMetadataProphecy->getAssociationNames()->shouldBeCalled()->willReturn([0 => 'dummyRelation']); |
| 49 | + $classMetadataProphecy->associationMappings = ['dummyRelation' => ['fetch' => 3]]; |
| 50 | + $emProphecy = $this->prophesize(EntityManager::class); |
| 51 | + $emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal()); |
| 52 | + $queryBuilderProphecy->leftJoin('o.dummyRelation', 'a0')->shouldBeCalled(1); |
| 53 | + $queryBuilderProphecy->addSelect('a0')->shouldBeCalled(1); |
| 54 | + |
| 55 | + $em = $queryBuilderProphecy->getEntityManager()->shouldBeCalled(1)->willReturn($emProphecy->reveal()); |
| 56 | + |
| 57 | + $queryBuilder = $queryBuilderProphecy->reveal(); |
| 58 | + $orderExtensionTest = new EagerLoadingExtension(); |
| 59 | + $orderExtensionTest->applyToItem($queryBuilder, Dummy::class, []); |
| 60 | + } |
| 61 | +} |
0 commit comments