Completed
Push — 2.x-dev-kit ( a9c68a...abe4e5 )
by
unknown
01:52
created

tests/Unit/Datagrid/ProxyQueryTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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');
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();
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();
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 testGetAndSetDocumentManager(): void
115
    {
116
        $dm = $this->createMock(DocumentManager::class);
117
        $this->pq->setDocumentManager($dm);
118
        $this->assertSame($dm, $this->pq->getDocumentManager());
119
    }
120
}
121