Completed
Push — ezp-30882-thumbnail-strategy-i... ( de6b29...42b8c0 )
by
unknown
288:43 queued 276:39
created

SearchHitAdapterFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateAdapterForContentQuery() 0 13 1
A testCreateAdapterForLocationQuery() 0 13 1
A testCreateFixedAdapter() 0 24 1
A dataProviderForCreateFixedAdapter() 0 12 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\Pagination\Pagerfanta\Tests\AdapterFactory;
10
11
use eZ\Publish\API\Repository\SearchService;
12
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
13
use eZ\Publish\API\Repository\Values\Content\Query;
14
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
15
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
16
use eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory\SearchHitAdapterFactory;
17
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchHitAdapter;
18
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchHitAdapter;
19
use Pagerfanta\Adapter\FixedAdapter;
20
use PHPUnit\Framework\TestCase;
21
22
final class SearchHitAdapterFactoryTest extends TestCase
23
{
24
    private const EXAMPLE_LANGUAGE_FILTER = [
25
        'language' => 'eng-GB',
26
    ];
27
28
    /** @var \eZ\Publish\API\Repository\SearchService|\PHPUnit\Framework\MockObject\MockObject */
29
    private $searchService;
30
31
    /** @var \eZ\Publish\Core\Pagination\Pagerfanta\AdapterFactory\SearchHitAdapterFactory */
32
    private $searchHitAdapterFactory;
33
34
    protected function setUp(): void
35
    {
36
        $this->searchService = $this->createMock(SearchService::class);
37
        $this->searchHitAdapterFactory = new SearchHitAdapterFactory($this->searchService);
38
    }
39
40
    public function testCreateAdapterForContentQuery(): void
41
    {
42
        $query = new Query();
43
44
        $this->assertEquals(
45
            new ContentSearchHitAdapter(
46
                $query,
47
                $this->searchService,
0 ignored issues
show
Bug introduced by
It seems like $this->searchService can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Paginati...tAdapter::__construct() does only seem to accept object<eZ\Publish\API\Repository\SearchService>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
                self::EXAMPLE_LANGUAGE_FILTER
49
            ),
50
            $this->searchHitAdapterFactory->createAdapter($query, self::EXAMPLE_LANGUAGE_FILTER)
51
        );
52
    }
53
54
    public function testCreateAdapterForLocationQuery(): void
55
    {
56
        $query = new LocationQuery();
57
58
        $this->assertEquals(
59
            new LocationSearchHitAdapter(
60
                $query,
61
                $this->searchService,
0 ignored issues
show
Bug introduced by
It seems like $this->searchService can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Paginati...tAdapter::__construct() does only seem to accept object<eZ\Publish\API\Repository\SearchService>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
62
                self::EXAMPLE_LANGUAGE_FILTER
63
            ),
64
            $this->searchHitAdapterFactory->createAdapter($query, self::EXAMPLE_LANGUAGE_FILTER)
65
        );
66
    }
67
68
    /**
69
     * @dataProvider dataProviderForCreateFixedAdapter
70
     */
71
    public function testCreateFixedAdapter(Query $query, string $expectedSearchMethod): void
72
    {
73
        $hits = [
74
            new SearchHit(),
75
            new SearchHit(),
76
            new SearchHit(),
77
        ];
78
79
        $this->searchService
80
            ->expects($this->once())
81
            ->method($expectedSearchMethod)
82
            ->with($query, self::EXAMPLE_LANGUAGE_FILTER)
83
            ->willReturn(
84
                new SearchResult([
85
                    'searchHits' => $hits,
86
                    'totalCount' => count($hits),
87
                ])
88
            );
89
90
        $this->assertEquals(
91
            new FixedAdapter(count($hits), $hits),
92
            $this->searchHitAdapterFactory->createFixedAdapter($query, self::EXAMPLE_LANGUAGE_FILTER)
93
        );
94
    }
95
96
    public function dataProviderForCreateFixedAdapter(): iterable
97
    {
98
        yield 'content query' => [
99
            new Query(),
100
            'findContent',
101
        ];
102
103
        yield 'location query' => [
104
            new LocationQuery(),
105
            'findLocations',
106
        ];
107
    }
108
}
109