Completed
Push — master ( d4cfd0...1c3910 )
by Antoine
18s queued 10s
created

DoctrineOrmFilterTestCase::doTestApply()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 12
nop 6
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Test;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\FilterInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
18
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
19
use Doctrine\Common\Persistence\ManagerRegistry;
20
use Doctrine\ORM\EntityRepository;
21
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
22
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
26
/**
27
 * @author Kévin Dunglas <[email protected]>
28
 */
29
abstract class DoctrineOrmFilterTestCase extends KernelTestCase
30
{
31
    /**
32
     * @var ManagerRegistry
33
     */
34
    protected $managerRegistry;
35
36
    /**
37
     * @var EntityRepository
38
     */
39
    protected $repository;
40
41
    /**
42
     * @var string
43
     */
44
    protected $resourceClass = Dummy::class;
45
46
    /**
47
     * @var string
48
     */
49
    protected $alias = 'o';
50
51
    /**
52
     * @var string
53
     */
54
    protected $filterClass;
55
56
    protected function setUp()
57
    {
58
        self::bootKernel();
59
60
        $manager = DoctrineTestHelper::createTestEntityManager();
61
        $this->managerRegistry = self::$kernel->getContainer()->get('doctrine');
62
        $this->repository = $manager->getRepository(Dummy::class);
63
    }
64
65
    /**
66
     * @dataProvider provideApplyTestData
67
     */
68
    public function testApply(array $properties = null, array $filterParameters, string $expectedDql, array $expectedParameters = null, callable $factory = null)
69
    {
70
        $this->doTestApply(false, $properties, $filterParameters, $expectedDql, $expectedParameters, $factory);
71
    }
72
73
    /**
74
     * @group legacy
75
     * @dataProvider provideApplyTestData
76
     */
77
    public function testApplyRequest(array $properties = null, array $filterParameters, string $expectedDql, array $expectedParameters = null, callable $factory = null)
78
    {
79
        $this->doTestApply(true, $properties, $filterParameters, $expectedDql, $expectedParameters, $factory);
80
    }
81
82
    protected function doTestApply(bool $request, array $properties = null, array $filterParameters, string $expectedDql, array $expectedParameters = null, callable $filterFactory = null)
83
    {
84
        if (null === $filterFactory) {
85
            $filterFactory = function (ManagerRegistry $managerRegistry, RequestStack $requestStack = null, array $properties = null): FilterInterface {
86
                $filterClass = $this->filterClass;
87
88
                return new $filterClass($managerRegistry, $requestStack, null, $properties);
89
            };
90
        }
91
92
        $requestStack = null;
93
        if ($request) {
94
            $requestStack = new RequestStack();
95
            $requestStack->push(Request::create('/api/dummies', 'GET', $filterParameters));
96
        }
97
98
        $queryBuilder = $this->repository->createQueryBuilder($this->alias);
99
        $filterCallable = $filterFactory($this->managerRegistry, $requestStack, $properties);
100
        $filterCallable->apply($queryBuilder, new QueryNameGenerator(), $this->resourceClass, null, $request ? [] : ['filters' => $filterParameters]);
101
102
        $this->assertEquals($expectedDql, $queryBuilder->getQuery()->getDQL());
103
104
        if (null === $expectedParameters) {
105
            return;
106
        }
107
108
        foreach ($expectedParameters as $parameterName => $expectedParameterValue) {
109
            $queryParameter = $queryBuilder->getQuery()->getParameter($parameterName);
110
111
            $this->assertNotNull($queryParameter, sprintf('Expected query parameter "%s" to be set', $parameterName));
112
            $this->assertEquals($expectedParameterValue, $queryParameter->getValue(), sprintf('Expected query parameter "%s" to be "%s"', $parameterName, var_export($expectedParameterValue, true)));
113
        }
114
    }
115
116
    abstract public function provideApplyTestData(): array;
117
}
118