Completed
Push — master ( a76a39...6e9755 )
by
unknown
14:19
created

tests/Filter/CallBackFilterTest.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
/*
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\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\DoctrinePHPCRAdminBundle\Filter\CallbackFilter;
16
17
class CallBackFilterTest extends BaseTestCase
18
{
19
    public function testFilterNullData()
20
    {
21
        $filter = new CallbackFilter();
22
        $filter->initialize('field_name', array('callback' => function () {
23
            return;
24
        }));
25
        $res = $filter->filter($this->proxyQuery, null, 'somefield', null);
26
        $this->assertNull($res);
27
        $this->assertFalse($filter->isActive());
28
    }
29
30
    public function testFilterEmptyArrayData()
31
    {
32
        $filter = new CallbackFilter();
33
34
        $filter->initialize('field_name', array('callback' => function () {
35
            return;
36
        }));
37
        $res = $filter->filter($this->proxyQuery, null, 'somefield', array());
38
        $this->assertNull($res);
39
        $this->assertFalse($filter->isActive());
40
    }
41
42
    public function testFilterMethod()
43
    {
44
        $this->proxyQuery->expects($this->once())
45
            ->method('getQueryBuilder')
46
            ->will($this->returnValue($this->qb));
47
48
        $filter = new CallbackFilter();
49
        $filter->initialize('field_name', array(
50
            'callback' => array($this, 'callbackMethod'),
51
        ));
52
53
        $filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue'));
0 ignored issues
show
array('type' => '', 'value' => 'somevalue') is of type array<string,string,{"ty...ing","value":"string"}>, but the function expects a string.

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...
54
55
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
56
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
57
58
        $this->assertEquals('a', $opDynamic->getAlias());
59
        $this->assertEquals('somefield', $opDynamic->getField());
60
        $this->assertEquals('somevalue', $opStatic->getValue());
61
62
        $this->assertTrue($filter->isActive());
63
    }
64
65
    public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
66
    {
67
        $queryBuilder = $proxyQuery->getQueryBuilder();
68
        $queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']);
69
70
        return true;
71
    }
72
73
    public function testFilterClosure()
74
    {
75
        $this->proxyQuery->expects($this->once())
76
            ->method('getQueryBuilder')
77
            ->will($this->returnValue($this->qb));
78
79
        $filter = new CallbackFilter();
80
        $filter->initialize('field_name', array(
81
            'callback' => function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) {
82
                $queryBuilder = $proxyQuery->getQueryBuilder();
83
                $queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']);
84
85
                return true;
86
            },
87
        ));
88
89
        $filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue'));
90
91
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
92
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
93
94
        $this->assertEquals('a', $opDynamic->getAlias());
95
        $this->assertEquals('somefield', $opDynamic->getField());
96
        $this->assertEquals('somevalue', $opStatic->getValue());
97
98
        $this->assertTrue($filter->isActive());
99
    }
100
101
    /**
102
     * @expectedException \RuntimeException
103
     */
104
    public function testWithoutCallback()
105
    {
106
        $filter = new CallbackFilter();
107
108
        $filter->setOption('callback', null);
109
        $filter->filter($this->proxyQuery, null, 'somefield', null);
110
    }
111
112
    /**
113
     * @expectedException \RuntimeException
114
     */
115
    public function testCallbackNotCallable()
116
    {
117
        $filter = new CallbackFilter();
118
119
        $filter->setOption('callback', 'someCallback');
120
        $filter->filter($this->proxyQuery, null, 'somefield', null);
121
    }
122
}
123