Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

ProxyQueryTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 106
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testConstructor() 0 4 1
A testSetSortBy() 0 5 1
A testSetSortOrder() 0 5 1
A testSetSortOrderInvalid() 0 5 1
A testSetFirstResult() 0 8 1
A testGetFirstResult() 0 7 1
A testSetMaxResults() 0 8 1
A testGetMaxResults() 0 7 1
A testExecute() 0 12 1
A testGetAndSetDocumentManager() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Datagrid;
13
14
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
15
use Sonata\DoctrinePHPCRAdminBundle\Datagrid\ProxyQuery;
16
17
class ProxyQueryTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var QueryBuilder|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $qb;
23
24
    /**
25
     * @var ProxyQuery
26
     */
27
    private $pq;
28
29
    public function setUp()
30
    {
31
        $this->qb = $this->getMockBuilder('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder')
32
            ->disableOriginalConstructor()
33
            ->getMock();
34
        $this->query = $this->getMockBuilder('Doctrine\ODM\PHPCR\Query\Query')
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
38
        $this->pq = new ProxyQuery($this->qb, 'a');
39
    }
40
41
    public function testConstructor()
42
    {
43
        $this->assertInstanceOf('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder', $this->pq->getQueryBuilder());
44
    }
45
46
    public function testSetSortBy()
47
    {
48
        $this->pq->setSortBy(array(), array('fieldName' => 'field'));
49
        $this->assertEquals('field', $this->pq->getSortBy());
50
    }
51
52
    public function testSetSortOrder()
53
    {
54
        $this->pq->setSortOrder('ASC');
55
        $this->assertEquals('ASC', $this->pq->getSortOrder());
56
    }
57
58
    /**
59
     * @expectedException \InvalidArgumentException
60
     */
61
    public function testSetSortOrderInvalid()
62
    {
63
        $this->pq->setSortOrder('SOME_ORDER');
64
        $this->assertEquals('SOME_ORDER', $this->pq->getSortOrder());
65
    }
66
67
    public function testSetFirstResult()
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()
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()
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()
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()
102
    {
103
        $this->qb->expects($this->once())
104
            ->method('getQuery')
105
            ->will($this->returnValue($this->query));
106
        $this->query->expects($this->once())
107
            ->method('execute')
108
            ->will($this->returnValue('test'));
109
110
        $res = $this->pq->execute();
111
        $this->assertEquals('test', $res);
112
    }
113
114
    public function testGetAndSetDocumentManager()
115
    {
116
        $dm = $this->getMockBuilder('Doctrine\\ODM\\PHPCR\\DocumentManager')
117
            ->disableOriginalConstructor()
118
            ->getMock();
119
        $this->pq->setDocumentManager($dm);
120
        $this->assertEquals($dm, $this->pq->getDocumentManager());
121
    }
122
}
123