ProxyQueryTest::testSetFirstResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Datagrid;
15
16
use Doctrine\ODM\PHPCR\DocumentManager;
17
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
18
use Doctrine\ODM\PHPCR\Query\Query;
19
use PHPUnit\Framework\TestCase;
20
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery;
21
22
class ProxyQueryTest extends TestCase
23
{
24
    /**
25
     * @var QueryBuilder|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private $qb;
28
29
    /**
30
     * @var ProxyQuery
31
     */
32
    private $pq;
33
34
    protected function setUp(): void
35
    {
36
        $this->qb = $this->createMock(QueryBuilder::class);
37
        $this->query = $this->createMock(Query::class);
38
39
        $this->pq = new ProxyQuery($this->qb, 'a');
40
    }
41
42
    public function testConstructor(): void
43
    {
44
        $this->assertInstanceOf(QueryBuilder::class, $this->pq->getQueryBuilder());
45
    }
46
47
    public function testSetSortBy(): void
48
    {
49
        $this->pq->setSortBy([], ['fieldName' => 'field']);
50
        $this->assertSame('field', $this->pq->getSortBy());
51
    }
52
53
    public function testSetSortOrder(): void
54
    {
55
        $this->pq->setSortOrder('ASC');
56
        $this->assertSame('ASC', $this->pq->getSortOrder());
57
    }
58
59
    public function testSetSortOrderInvalid(): void
60
    {
61
        $this->expectException(\InvalidArgumentException::class);
62
63
        $this->pq->setSortOrder('SOME_ORDER');
64
        $this->assertSame('SOME_ORDER', $this->pq->getSortOrder());
65
    }
66
67
    public function testSetFirstResult(): void
68
    {
69
        $this->qb->expects($this->once())
70
            ->method('setFirstResult')
71
            ->with($this->equalTo(19));
72
73
        $this->pq->setFirstResult(19);
74
    }
75
76
    public function testGetFirstResult(): void
77
    {
78
        $this->qb->expects($this->once())
79
            ->method('getFirstResult');
80
81
        $this->pq->getFirstResult();
0 ignored issues
show
Unused Code introduced by
The call to the method Sonata\DoctrinePHPCRAdmi...Query::getFirstResult() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
82
    }
83
84
    public function testSetMaxResults(): void
85
    {
86
        $this->qb->expects($this->once())
87
            ->method('setMaxResults')
88
            ->with($this->equalTo(29));
89
90
        $this->pq->setMaxResults(29);
91
    }
92
93
    public function testGetMaxResults(): void
94
    {
95
        $this->qb->expects($this->once())
96
            ->method('getMaxResults');
97
98
        $this->pq->getMaxResults();
0 ignored issues
show
Unused Code introduced by
The call to the method Sonata\DoctrinePHPCRAdmi...yQuery::getMaxResults() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
99
    }
100
101
    public function testExecute(): void
102
    {
103
        $this->qb->expects($this->once())
104
            ->method('getQuery')
105
            ->willReturn($this->query);
106
        $this->query->expects($this->once())
107
            ->method('execute')
108
            ->willReturn('test');
109
110
        $res = $this->pq->execute();
111
        $this->assertSame('test', $res);
112
    }
113
114
    public function testExecuteWithSortBy(): void
115
    {
116
        $qb = $this->createPartialMock(QueryBuilder::class, ['getQuery']);
117
118
        $qb->expects($this->once())
119
            ->method('getQuery')
120
            ->willReturn($this->query);
121
        $this->query->expects($this->once())
122
            ->method('execute')
123
            ->willReturn('test');
124
125
        $pq = new ProxyQuery($qb, 'a');
126
127
        $pq->setSortBy([], ['fieldName' => 'field']);
128
        $pq->setSortOrder('ASC');
129
        $res = $pq->execute();
130
        $this->assertSame('test', $res);
131
    }
132
133
    public function testGetAndSetDocumentManager(): void
134
    {
135
        $dm = $this->createMock(DocumentManager::class);
136
        $this->pq->setDocumentManager($dm);
137
        $this->assertSame($dm, $this->pq->getDocumentManager());
138
    }
139
}
140