Skip to content

Commit 28243cb

Browse files
author
Amrouche Hamza
committed
feat: add test to doctrine extention namespace
1 parent 00b2c8f commit 28243cb

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Api\FilterCollection;
14+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\FilterExtension;
15+
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\FilterInterface;
16+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
19+
use Doctrine\ORM\QueryBuilder;
20+
21+
/**
22+
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
23+
*/
24+
class FilterExtensionTest extends \PHPUnit_Framework_TestCase
25+
{
26+
public function testApplyToCollectionWithValidFilters()
27+
{
28+
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
29+
30+
$dummyMetadata = new ResourceMetadata('dummy', 'dummy', '#dummy', ['get' => ['method' => 'GET'], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET', 'filters' => ['dummyFilter']], 'post' => ['method' => 'POST'], 'custom' => ['method' => 'GET', 'path' => '/foo'], 'custom2' => ['method' => 'POST', 'path' => '/foo']], []);
31+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
32+
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
33+
34+
$queryBuilder = $queryBuilderProphecy->reveal();
35+
36+
$filterProphecy = $this->prophesize(FilterInterface::class);
37+
$filterProphecy->apply($queryBuilder, Dummy::class, 'get')->shouldBeCalled();
38+
39+
$orderExtensionTest = new FilterExtension($resourceMetadataFactoryProphecy->reveal(), new FilterCollection(['dummyFilter' => $filterProphecy->reveal()]));
40+
$orderExtensionTest->applyToCollection($queryBuilder, Dummy::class, 'get');
41+
}
42+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\OrderExtension;
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 OrderExtensionTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testApplyToCollectionWithValidOrder()
25+
{
26+
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
27+
28+
$queryBuilderProphecy->addOrderBy('o.name', 'asc')->shouldBeCalled();
29+
30+
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
31+
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
32+
33+
$emProphecy = $this->prophesize(EntityManager::class);
34+
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
35+
36+
$em = $queryBuilderProphecy->getEntityManager()->shouldBeCalled()->willReturn($emProphecy->reveal());
37+
38+
$queryBuilder = $queryBuilderProphecy->reveal();
39+
$orderExtensionTest = new OrderExtension('asc');
40+
$orderExtensionTest->applyToCollection($queryBuilder, Dummy::class);
41+
}
42+
43+
public function testApplyToCollectionWithWrongOrder()
44+
{
45+
$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
46+
47+
$queryBuilderProphecy->addOrderBy('o.name', 'asc')->shouldNotBeCalled();
48+
49+
$classMetadataProphecy = $this->prophesize(ClassMetadata::class);
50+
$classMetadataProphecy->getIdentifier()->shouldBeCalled()->willReturn(['name']);
51+
52+
$emProphecy = $this->prophesize(EntityManager::class);
53+
$emProphecy->getClassMetadata(Dummy::class)->shouldBeCalled()->willReturn($classMetadataProphecy->reveal());
54+
55+
$em = $queryBuilderProphecy->getEntityManager()->shouldBeCalled()->willReturn($emProphecy->reveal());
56+
57+
$queryBuilder = $queryBuilderProphecy->reveal();
58+
$orderExtensionTest = new OrderExtension();
59+
$orderExtensionTest->applyToCollection($queryBuilder, Dummy::class);
60+
}
61+
}

0 commit comments

Comments
 (0)