Completed
Push — master ( ac8ec4...1a57ac )
by Kévin
04:37 queued 11s
created

src/Test/DoctrineMongoDbOdmFilterTestCase.php (2 issues)

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\MongoDbOdm\Filter\FilterInterface;
17
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy;
18
use Doctrine\Bundle\MongoDBBundle\Tests\TestCase;
19
use Doctrine\Common\Persistence\ManagerRegistry;
20
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
21
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
22
23
/**
24
 * @internal
25
 *
26
 * @author Alan Poulain <[email protected]>
27
 */
28
abstract class DoctrineMongoDbOdmFilterTestCase extends KernelTestCase
29
{
30
    /**
31
     * @var ManagerRegistry
32
     */
33
    protected $managerRegistry;
34
35
    /**
36
     * @var DocumentRepository
37
     */
38
    protected $repository;
39
40
    /**
41
     * @var string
42
     */
43
    protected $resourceClass = Dummy::class;
44
45
    /**
46
     * @var string
47
     */
48
    protected $filterClass;
49
50
    protected function setUp(): void
51
    {
52
        self::bootKernel();
53
54
        $manager = TestCase::createTestDocumentManager();
55
        $this->managerRegistry = self::$kernel->getContainer()->get('doctrine_mongodb');
56
        $this->repository = $manager->getRepository(Dummy::class);
57
    }
58
59
    /**
60
     * @dataProvider provideApplyTestData
61
     */
62
    public function testApply(?array $properties, array $filterParameters, array $expectedPipeline, callable $factory = null)
63
    {
64
        $this->doTestApply($properties, $filterParameters, $expectedPipeline, $factory);
65
    }
66
67
    protected function doTestApply(?array $properties, array $filterParameters, array $expectedPipeline, callable $filterFactory = null)
68
    {
69
        if (null === $filterFactory) {
70
            $filterFactory = function (ManagerRegistry $managerRegistry, array $properties = null): FilterInterface {
71
                $filterClass = $this->filterClass;
72
73
                return new $filterClass($managerRegistry, null, $properties);
74
            };
75
        }
76
77
        $aggregationBuilder = $this->repository->createAggregationBuilder();
78
        $filterCallable = $filterFactory($this->managerRegistry, $properties);
79
        $context = ['filters' => $filterParameters];
80
        $filterCallable->apply($aggregationBuilder, $this->resourceClass, null, $context);
81
        $pipeline = [];
0 ignored issues
show
The assignment to $pipeline is dead and can be removed.
Loading history...
82
        try {
83
            $pipeline = $aggregationBuilder->getPipeline();
84
        } catch (\OutOfRangeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
85
        }
86
87
        $this->assertEquals($expectedPipeline, $pipeline);
88
    }
89
90
    protected function buildFilter(?array $properties = null)
91
    {
92
        return new $this->filterClass($this->managerRegistry, null, $properties);
93
    }
94
95
    abstract public function provideApplyTestData(): array;
96
}
97