Completed
Push — ezp_31107 ( 816f32...9177e0 )
by
unknown
220:54 queued 209:22
created

QueryRenderControllerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testRenderQueryWithMinOptions() 0 14 1
A testRenderQueryWithAllOptions() 0 17 1
A configureMocks() 0 21 1
A assertRenderQueryResult() 0 13 1
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\Controller\Tests;
10
11
use eZ\Publish\API\Repository\Values\Content\Query;
12
use eZ\Publish\Core\MVC\Symfony\Controller\QueryRenderController;
13
use eZ\Publish\Core\MVC\Symfony\View\QueryView;
14
use eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory\SearchHitAdapterFactoryInterface;
15
use eZ\Publish\Core\Query\QueryFactoryInterface;
16
use Pagerfanta\Adapter\AdapterInterface;
17
use Pagerfanta\Pagerfanta;
18
use PHPUnit\Framework\TestCase;
19
use Symfony\Component\HttpFoundation\Request;
20
21
final class QueryRenderControllerTest extends TestCase
22
{
23
    private const EXAMPLE_CURRENT_PAGE = 3;
24
    private const EXAMPLE_MAX_PER_PAGE = 100;
25
26
    private const MIN_OPTIONS = [
27
        'query' => [
28
            'query_type' => 'ExampleQuery',
29
        ],
30
        'template' => 'example.html.twig',
31
    ];
32
33
    private const ALL_OPTIONS = [
34
        'query' => [
35
            'query_type' => 'ExampleQuery',
36
            'parameters' => [
37
                'foo' => 'foo',
38
                'bar' => 'bar',
39
                'baz' => 'baz',
40
            ],
41
            'assign_results_to' => 'results',
42
        ],
43
        'template' => 'example.html.twig',
44
        'pagination' => [
45
            'enabled' => true,
46
            'limit' => self::EXAMPLE_MAX_PER_PAGE,
47
            'page_param' => 'p',
48
        ],
49
    ];
50
51
    /** @var \eZ\Publish\Core\Query\QueryFactoryInterface|\PHPUnit\Framework\MockObject\MockObject */
52
    private $queryFactory;
53
54
    /** @var \eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory\SearchHitAdapterFactoryInterface|\PHPUnit\Framework\MockObject\MockObject */
55
    private $searchHitAdapterFactory;
56
57
    /** @var \eZ\Publish\Core\MVC\Symfony\Controller\QueryRenderController */
58
    private $controller;
59
60
    protected function setUp(): void
61
    {
62
        $this->queryFactory = $this->createMock(QueryFactoryInterface::class);
63
        $this->searchHitAdapterFactory = $this->createMock(SearchHitAdapterFactoryInterface::class);
64
65
        $this->controller = new QueryRenderController(
66
            $this->queryFactory,
67
            $this->searchHitAdapterFactory
68
        );
69
    }
70
71
    public function testRenderQueryWithMinOptions(): void
72
    {
73
        $adapter = $this->configureMocks(self::MIN_OPTIONS);
74
75
        $items = new Pagerfanta($adapter);
76
        $items->setAllowOutOfRangePages(true);
77
78
        $this->assertRenderQueryResult(
79
            new QueryView('example.html.twig', [
80
                'items' => $items,
81
            ]),
82
            self::MIN_OPTIONS
83
        );
84
    }
85
86
    public function testRenderQueryWithAllOptions(): void
87
    {
88
        $adapter = $this->configureMocks(self::ALL_OPTIONS);
89
90
        $items = new Pagerfanta($adapter);
91
        $items->setAllowOutOfRangePages(true);
92
        $items->setCurrentPage(self::EXAMPLE_CURRENT_PAGE);
93
        $items->setMaxPerPage(self::EXAMPLE_MAX_PER_PAGE);
94
95
        $this->assertRenderQueryResult(
96
            new QueryView('example.html.twig', [
97
                'results' => $items,
98
            ]),
99
            self::ALL_OPTIONS,
100
            new Request(['p' => self::EXAMPLE_CURRENT_PAGE])
101
        );
102
    }
103
104
    private function configureMocks(array $options): AdapterInterface
105
    {
106
        $query = new Query();
107
108
        $this->queryFactory
109
            ->method('create')
110
            ->with(
111
                $options['query']['query_type'],
112
                $options['query']['parameters'] ?? []
113
            )
114
            ->willReturn($query);
115
116
        $adapter = $this->createMock(AdapterInterface::class);
117
118
        $this->searchHitAdapterFactory
119
            ->method('createAdapter')
120
            ->with($query)
121
            ->willReturn($adapter);
122
123
        return $adapter;
124
    }
125
126
    private function assertRenderQueryResult(
127
        QueryView $expectedView,
128
        array $options,
129
        Request $request = null
130
    ): void {
131
        $this->assertEquals(
132
            $expectedView,
133
            $this->controller->renderQuery(
134
                $request ?? new Request(),
135
                $options
136
            )
137
        );
138
    }
139
}
140