Completed
Pull Request — master (#7405)
by Michael
68:49 queued 63:08
created

PaginatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Tools\Pagination;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\Decorator\EntityManagerDecorator;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
11
use Doctrine\ORM\Query;
12
use Doctrine\ORM\Tools\Pagination\Paginator;
13
use Doctrine\Tests\Mocks\ConnectionMock;
14
use Doctrine\Tests\Mocks\DriverMock;
15
use Doctrine\Tests\OrmTestCase;
16
17
class PaginatorTest extends OrmTestCase
18
{
19
    /** @var Connection */
20
    private $connection;
21
    /** @var EntityManagerInterface */
22
    private $em;
23
    /** @var AbstractHydrator */
24
    private $hydrator;
25
26
    protected function setUp() : void
27
    {
28
        $this->connection = $this->getMockBuilder(ConnectionMock::class)
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Do...cuteQuery'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\DBAL\Connection of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
            ->setConstructorArgs([[], new DriverMock()])
30
            ->setMethods(['executeQuery'])
31
            ->getMock()
32
        ;
33
34
        $this->em = $this->getMockBuilder(EntityManagerDecorator::class)
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Do...wHydrator'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\ORM\EntityManagerInterface of property $em.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
            ->setConstructorArgs([$this->getTestEntityManager($this->connection)])
36
            ->setMethods(['newHydrator'])
37
            ->getMock()
38
        ;
39
40
        $this->hydrator = $this->createMock(AbstractHydrator::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...bstractHydrator::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\ORM\Internal\Hydration\AbstractHydrator of property $hydrator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->em->method('newHydrator')->willReturn($this->hydrator);
42
    }
43
44
    public function testExtraParametersAreStrippedWhenWalkerRemovingOriginalSelectElementsIsUsed() : void
45
    {
46
        $paramInWhere     = 1;
47
        $paramInSubSelect = 2;
48
        $returnedIds      = [10];
49
50
        $this->hydrator->method('hydrateAll')->willReturn([$returnedIds]);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\Internal\Hydration\AbstractHydrator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $this->hydrator->/** @scrutinizer ignore-call */ 
51
                         method('hydrateAll')->willReturn([$returnedIds]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
52
        $query = new Query($this->em);
53
        $query->setDQL(
54
            'SELECT u,
55
                (
56
                    SELECT MAX(a.version)
57
                    FROM Doctrine\\Tests\\Models\\CMS\\CmsArticle a
58
                    WHERE a.user = u AND 1 = :paramInSubSelect
59
                ) AS HIDDEN max_version
60
            FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u
61
            WHERE u.id = :paramInWhere'
62
        );
63
        $query->setParameters(['paramInWhere' => $paramInWhere, 'paramInSubSelect' => $paramInSubSelect]);
64
        $paginator = (new Paginator($query, true))->setUseOutputWalkers(false);
65
66
        $this->connection->expects($this->exactly(3))->method('executeQuery');
67
68
        $this->connection->expects($this->at(0))
69
            ->method('executeQuery')
70
            ->with($this->anything(), [$paramInWhere])
71
        ;
72
73
        $this->connection->expects($this->at(1))
74
            ->method('executeQuery')
75
            ->with($this->anything(), [$paramInWhere])
76
        ;
77
78
        $this->connection->expects($this->at(2))
79
            ->method('executeQuery')
80
            ->with($this->anything(), [$paramInSubSelect, $paramInWhere, $returnedIds])
81
        ;
82
83
        $paginator->count();
84
        $paginator->getIterator();
85
    }
86
87
    public function testPaginatorNotCaringAboutExtraParametersWithoutOutputWalkers() : void
88
    {
89
        $this->connection->expects($this->exactly(3))->method('executeQuery');
90
91
        $this->createPaginatorWithExtraParametersWithoutOutputWalkers([])->count();
92
        $this->createPaginatorWithExtraParametersWithoutOutputWalkers([[10]])->count();
93
        $this->createPaginatorWithExtraParametersWithoutOutputWalkers([])->getIterator();
94
    }
95
96
    public function testgetIteratorDoesCareAboutExtraParametersWithoutOutputWalkersWhenResultIsNotEmpty() : void
97
    {
98
        $this->connection->expects($this->exactly(1))->method('executeQuery');
99
        $this->expectException(Query\QueryException::class);
100
        $this->expectExceptionMessage('Too many parameters: the query defines 1 parameters and you bound 2');
101
102
        $this->createPaginatorWithExtraParametersWithoutOutputWalkers([[10]])->getIterator();
103
    }
104
105
    /**
106
     * @param int[][] $willReturnRows
107
     */
108
    private function createPaginatorWithExtraParametersWithoutOutputWalkers(array $willReturnRows) : Paginator
109
    {
110
        $this->hydrator->method('hydrateAll')->willReturn($willReturnRows);
111
        $this->connection->method('executeQuery')->with($this->anything(), []);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\DBAL\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $this->connection->/** @scrutinizer ignore-call */ 
112
                           method('executeQuery')->with($this->anything(), []);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
113
        $query = new Query($this->em);
114
        $query->setDQL('SELECT u FROM Doctrine\\Tests\\Models\\CMS\\CmsUser u');
115
        $query->setParameters(['paramInWhere' => 1]);
116
117
        return (new Paginator($query, true))->setUseOutputWalkers(false);
118
    }
119
}
120