Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

CallBackFilterTest::testFilterClosure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
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', array('callback' => function () {
23
            return;
24
        }));
25
        $res = $filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
Bug introduced by Roel Sint
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...
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());
0 ignored issues
show
Documentation introduced by Roel Sint
array() is of type array, 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...
Bug introduced by Roel Sint
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...
38
        $this->assertNull($res);
39
        $this->assertFalse($filter->isActive());
40
    }
41
42
    public function testFilterMethod()
43
    {
44
        $this->proxyQuery->expects($this->once())
0 ignored issues
show
Documentation Bug introduced by Roel Sint
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...
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
Documentation introduced by dantleech
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())
0 ignored issues
show
Documentation Bug introduced by Roel Sint
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...
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'));
0 ignored issues
show
Documentation introduced by dantleech
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...
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