Completed
Push — master ( 6734fa...551366 )
by
unknown
07:45
created

CallBackFilterTest::testFilterMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
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\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', ['callback' => function () {
23
        }]);
24
        $res = $filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
Bug introduced by
Are you sure the assignment to $res is correct as $filter->filter($this->p...ull, 'somefield', null) (which targets Sonata\DoctrinePHPCRAdmi...allbackFilter::filter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
25
        $this->assertNull($res);
26
        $this->assertFalse($filter->isActive());
27
    }
28
29
    public function testFilterEmptyArrayData()
30
    {
31
        $filter = new CallbackFilter();
32
33
        $filter->initialize('field_name', ['callback' => function () {
34
        }]);
35
        $res = $filter->filter($this->proxyQuery, null, 'somefield', []);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $res is correct as $filter->filter($this->p..., 'somefield', array()) (which targets Sonata\DoctrinePHPCRAdmi...allbackFilter::filter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
        $this->assertNull($res);
37
        $this->assertFalse($filter->isActive());
38
    }
39
40
    public function testFilterMethod()
41
    {
42
        $this->proxyQuery->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<Sonata\DoctrinePH...le\Datagrid\ProxyQuery>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
            ->method('getQueryBuilder')
44
            ->will($this->returnValue($this->qb));
45
46
        $filter = new CallbackFilter();
47
        $filter->initialize('field_name', [
48
            'callback' => [$this, 'callbackMethod'],
49
        ]);
50
51
        $filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']);
52
53
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
54
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
55
56
        $this->assertEquals('a', $opDynamic->getAlias());
57
        $this->assertEquals('somefield', $opDynamic->getField());
58
        $this->assertEquals('somevalue', $opStatic->getValue());
59
60
        $this->assertTrue($filter->isActive());
61
    }
62
63
    public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
64
    {
65
        $queryBuilder = $proxyQuery->getQueryBuilder();
66
        $queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']);
67
68
        return true;
69
    }
70
71
    public function testFilterClosure()
72
    {
73
        $this->proxyQuery->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by
The method expects does not exist on object<Sonata\DoctrinePH...le\Datagrid\ProxyQuery>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
            ->method('getQueryBuilder')
75
            ->will($this->returnValue($this->qb));
76
77
        $filter = new CallbackFilter();
78
        $filter->initialize('field_name', [
79
            'callback' => function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) {
80
                $queryBuilder = $proxyQuery->getQueryBuilder();
81
                $queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']);
82
83
                return true;
84
            },
85
        ]);
86
87
        $filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']);
88
89
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
90
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
91
92
        $this->assertEquals('a', $opDynamic->getAlias());
93
        $this->assertEquals('somefield', $opDynamic->getField());
94
        $this->assertEquals('somevalue', $opStatic->getValue());
95
96
        $this->assertTrue($filter->isActive());
97
    }
98
99
    public function testWithoutCallback()
100
    {
101
        $this->expectException(\RuntimeException::class);
102
103
        $filter = new CallbackFilter();
104
105
        $filter->setOption('callback', null);
106
        $filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
107
    }
108
109
    public function testCallbackNotCallable()
110
    {
111
        $this->expectException(\RuntimeException::class);
112
113
        $filter = new CallbackFilter();
114
115
        $filter->setOption('callback', 'someCallback');
116
        $filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
117
    }
118
}
119