Completed
Push — master ( 6b2632...1706e7 )
by
unknown
07:57 queued 10s
created

ProxyQueryTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 4 1
A testSetSortBy() 0 5 1
A testSetSortOrder() 0 5 1
A testSetSortOrderInvalid() 0 7 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 setUp() 0 7 1
A testGetAndSetDocumentManager() 0 6 1
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
    public function setUp(): void
35
    {
36
        $this->qb = $this->createMock(QueryBuilder::class);
37
        $this->query = $this->createMock(Query::class);
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
39
        $this->pq = new ProxyQuery($this->qb, 'a');
0 ignored issues
show
Documentation introduced by
$this->qb is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\PHPC...y\Builder\QueryBuilder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 testGetAndSetDocumentManager(): void
115
    {
116
        $dm = $this->createMock(DocumentManager::class);
117
        $this->pq->setDocumentManager($dm);
0 ignored issues
show
Documentation introduced by
$dm is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\PHPCR\DocumentManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
        $this->assertSame($dm, $this->pq->getDocumentManager());
119
    }
120
}
121