Completed
Push — master ( 1db3cd...632e40 )
by Jeroen
24:52 queued 11:31
created

AbstractDoctrineDBALAdminListConfiguratorTest.php (1 issue)

mismatching argument types.

Documentation Minor

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
namespace Kunstmaan\AdminListBundle\Tests\AdminList\Configurator;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Doctrine\DBAL\Statement;
8
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineDBALAdminListConfigurator;
9
use Kunstmaan\AdminListBundle\AdminList\Filter;
10
use Kunstmaan\AdminListBundle\AdminList\FilterBuilder;
11
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\AbstractDBALFilterType;
12
use Pagerfanta\Pagerfanta;
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Session\Session;
16
17
class AbstractDoctrineDBALAdminListConfiguratorTest extends TestCase
18
{
19
    /** @var Connection */
20
    private $connectionMock;
21
22
    public function setUp()
23
    {
24
        $this->connectionMock = $this->createMock(Connection::class);
25
        $this->connectionMock
26
            ->expects($this->any())
27
            ->method('executeQuery')
28
            ->willReturn($this->createMock(Statement::class))
29
        ;
30
    }
31
32 View Code Duplication
    public function testGetEditUrl()
33
    {
34
        $abstractMock = $this->setUpAbstractMock();
35
36
        $editUrl = $abstractMock->getEditUrlFor(['id' => 888]);
37
        $this->assertIsArray($editUrl);
38
        $this->assertArrayHasKey('path', $editUrl);
39
        $this->assertArrayHasKey('params', $editUrl);
40
        $this->assertEquals('bundle_admin_myentity_'.AbstractDoctrineDBALadminListConfigurator::SUFFIX_EDIT, $editUrl['path']);
41
        $this->assertContains(888, $editUrl['params']);
42
    }
43
44 View Code Duplication
    public function testGetDeleteUrl()
45
    {
46
        $abstractMock = $this->setUpAbstractMock();
47
48
        $editUrl = $abstractMock->getDeleteUrlFor(['id' => 888]);
49
        $this->assertIsArray($editUrl);
50
        $this->assertArrayHasKey('path', $editUrl);
51
        $this->assertArrayHasKey('params', $editUrl);
52
        $this->assertEquals('bundle_admin_myentity_'.AbstractDoctrineDBALAdminListConfigurator::SUFFIX_DELETE, $editUrl['path']);
53
        $this->assertContains(888, $editUrl['params']);
54
    }
55
56
    public function testGetPagerFanta()
57
    {
58
        $abstractMock = $this->setUpAbstractMock();
59
60
        $this->assertInstanceOf(Pagerfanta::class, $abstractMock->getPagerfanta());
61
    }
62
63
    public function testGetQueryBuilderAndIterator()
64
    {
65
        $abstractMock = $this->setUpAbstractMock(['getFilterBuilder']);
66
67
        $abstractDBALFilterTypeMock = $this->createMock(AbstractDBALFilterType::class);
68
69
        $filterBuilderMock = $this->createMock(FilterBuilder::class);
70
        $filterBuilderMock
71
            ->expects($this->any())
72
            ->method('getCurrentFilters')
73
            ->willReturn([
74
                new Filter('foo', ['type' => $abstractDBALFilterTypeMock, 'options' => []], 'uid'),
75
            ])
76
        ;
77
78
        $abstractMock
79
            ->expects($this->any())
80
            ->method('getFilterBuilder')
81
            ->willReturn($filterBuilderMock)
82
        ;
83
84
        $requestMock = $this->getMockBuilder(Request::class)
85
            ->setConstructorArgs([['page' => 1], [], ['_route' => 'testroute']])
86
            ->setMethods(['getSession'])
87
            ->getMock()
88
        ;
89
90
        $sessionMock = $this->createMock(Session::class);
91
        $sessionMock
92
            ->expects($this->once())
93
            ->method('has')
94
            ->willReturn(true)
95
        ;
96
        $sessionMock
97
            ->expects($this->once())
98
            ->method('get')
99
            ->with('listconfig_testroute')
100
            ->willReturn(['page' => 1, 'orderBy' => 'foo', 'orderDirection' => 'up'])
101
        ;
102
103
        $requestMock
104
            ->expects($this->exactly(2))
105
            ->method('getSession')
106
            ->willReturn($sessionMock)
107
        ;
108
109
        $abstractMock->bindRequest($requestMock);
0 ignored issues
show
$requestMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

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...
110
        $this->assertInstanceOf(QueryBuilder::class, $abstractMock->getQueryBuilder());
111
        $this->assertInstanceOf(\Traversable::class, $abstractMock->getIterator());
112
    }
113
114
    public function testSetGetCountField()
115
    {
116
        $abstractMock = $this->setUpAbstractMock();
117
118
        $this->assertInstanceOf(AbstractDoctrineDBALAdminListConfigurator::class, $abstractMock->setCountField('foo'));
119
        $this->assertIsString($abstractMock->getCountField());
120
    }
121
122
    public function testSetGetDistinctCount()
123
    {
124
        $abstractMock = $this->setUpAbstractMock();
125
126
        $this->assertInstanceOf(AbstractDoctrineDBALAdminListConfigurator::class, $abstractMock->setUseDistinctCount(false));
127
        $this->assertFalse($abstractMock->getUseDistinctCount());
128
    }
129
130 View Code Duplication
    public function setUpAbstractMock(array $methods = [])
131
    {
132
        /** @var AbstractDoctrineDBALAdminListConfigurator $abstractMock */
133
        $abstractMock = $this->getMockForAbstractClass(AbstractDoctrineDBALAdminListConfigurator::class, [$this->connectionMock], '', true, true, true, $methods);
134
        $abstractMock
135
            ->expects($this->any())
136
            ->method('getBundleName')
137
            ->willReturn('Bundle')
138
        ;
139
        $abstractMock
140
            ->expects($this->any())
141
            ->method('getEntityName')
142
            ->willReturn('MyEntity')
143
        ;
144
145
        return $abstractMock;
146
    }
147
}
148