Completed
Push — 1.x ( e997bd )
by Sullivan
19:38
created

CallbackFilterTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 101
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testFilterNullData() 0 8 1
A testFilterEmptyArrayData() 0 9 1
A testFilterMethod() 0 22 1
A callbackMethod() 0 7 1
B testFilterClosure() 0 26 1
A testWithoutCallback() 0 7 1
A testCallbackNotCallable() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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() { return; }));
23
        $res = $filter->filter($this->proxyQuery, null, 'somefield', null);
0 ignored issues
show
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...
24
        $this->assertNull($res);
25
        $this->assertFalse($filter->isActive());
26
    }
27
28
    public function testFilterEmptyArrayData()
29
    {
30
        $filter = new CallbackFilter();
31
32
        $filter->initialize('field_name', array('callback' => function() { return; }));
33
        $res = $filter->filter($this->proxyQuery, null, 'somefield', array());
0 ignored issues
show
Documentation introduced by
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
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...
34
        $this->assertNull($res);
35
        $this->assertFalse($filter->isActive());
36
    }
37
38
    public function testFilterMethod()
39
    {
40
        $this->proxyQuery->expects($this->once())
41
            ->method('getQueryBuilder')
42
            ->will($this->returnValue($this->qb));
43
44
        $filter = new CallbackFilter();
45
        $filter->initialize('field_name', array(
46
            'callback' => array($this, 'callbackMethod')
47
        ));
48
49
        $filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue'));
0 ignored issues
show
Documentation introduced by
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...
50
51
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
52
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
53
54
        $this->assertEquals('a', $opDynamic->getAlias());
55
        $this->assertEquals('somefield', $opDynamic->getField());
56
        $this->assertEquals('somevalue', $opStatic->getValue());
57
58
        $this->assertTrue($filter->isActive());
59
    }
60
61
    public function callbackMethod(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
62
    {
63
        $queryBuilder = $proxyQuery->getQueryBuilder();
64
        $queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']);
65
66
        return true;
67
    }
68
69
    public function testFilterClosure()
70
    {
71
        $this->proxyQuery->expects($this->once())
72
            ->method('getQueryBuilder')
73
            ->will($this->returnValue($this->qb));
74
75
        $filter = new CallbackFilter();
76
        $filter->initialize('field_name', array(
77
            'callback' => function (ProxyQueryInterface $proxyQuery, $alias, $field, $data) {
78
                $queryBuilder = $proxyQuery->getQueryBuilder();
79
                $queryBuilder->andWhere()->eq()->field('a.'.$field)->literal($data['value']);
80
                return true;
81
            }
82
        ));
83
84
        $filter->filter($this->proxyQuery, null, 'somefield', array('type' => '', 'value' => 'somevalue'));
0 ignored issues
show
Documentation introduced by
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...
85
86
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
87
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
88
89
        $this->assertEquals('a', $opDynamic->getAlias());
90
        $this->assertEquals('somefield', $opDynamic->getField());
91
        $this->assertEquals('somevalue', $opStatic->getValue());
92
93
        $this->assertTrue($filter->isActive());
94
    }
95
96
    /**
97
     * @expectedException RuntimeException
98
     */
99
    public function testWithoutCallback()
100
    {
101
        $filter = new CallbackFilter();
102
103
        $filter->setOption('callback', null);
104
        $filter->filter($this->proxyQuery, null, 'somefield', null);
105
    }
106
107
    /**
108
     * @expectedException RuntimeException
109
     */
110
    public function testCallbackNotCallable()
111
    {
112
        $filter = new CallbackFilter();
113
114
        $filter->setOption('callback', 'someCallback');
115
        $filter->filter($this->proxyQuery, null, 'somefield', null);
116
    }
117
}
118