CallBackFilterTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 102
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testFilterNullData() 0 9 1
A testFilterEmptyArrayData() 0 10 1
A testFilterMethod() 0 22 1
A callbackMethod() 0 7 1
A testFilterClosure() 0 27 1
A testWithoutCallback() 0 9 1
A testCallbackNotCallable() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\DoctrinePHPCRAdminBundle\Filter\CallbackFilter;
18
19
class CallBackFilterTest extends BaseTestCase
20
{
21
    public function testFilterNullData(): void
22
    {
23
        $filter = new CallbackFilter();
24
        $filter->initialize('field_name', ['callback' => static function (): void {
25
        }]);
26
        $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...
27
        $this->assertNull($res);
28
        $this->assertFalse($filter->isActive());
29
    }
30
31
    public function testFilterEmptyArrayData(): void
32
    {
33
        $filter = new CallbackFilter();
34
35
        $filter->initialize('field_name', ['callback' => static function (): void {
36
        }]);
37
        $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...
38
        $this->assertNull($res);
39
        $this->assertFalse($filter->isActive());
40
    }
41
42
    public function testFilterMethod(): void
43
    {
44
        $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...
45
            ->method('getQueryBuilder')
46
            ->willReturn($this->qb);
47
48
        $filter = new CallbackFilter();
49
        $filter->initialize('field_name', [
50
            'callback' => [$this, 'callbackMethod'],
51
        ]);
52
53
        $filter->filter($this->proxyQuery, null, 'somefield', ['type' => '', 'value' => 'somevalue']);
54
55
        $opDynamic = $this->qbTester->getNode('where.constraint.operand_dynamic');
56
        $opStatic = $this->qbTester->getNode('where.constraint.operand_static');
57
58
        $this->assertSame('a', $opDynamic->getAlias());
59
        $this->assertSame('somefield', $opDynamic->getField());
60
        $this->assertSame('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(): void
74
    {
75
        $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...
76
            ->method('getQueryBuilder')
77
            ->willReturn($this->qb);
78
79
        $filter = new CallbackFilter();
80
        $filter->initialize('field_name', [
81
            'callback' => static 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', ['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->assertSame('a', $opDynamic->getAlias());
95
        $this->assertSame('somefield', $opDynamic->getField());
96
        $this->assertSame('somevalue', $opStatic->getValue());
97
98
        $this->assertTrue($filter->isActive());
99
    }
100
101
    public function testWithoutCallback(): void
102
    {
103
        $this->expectException(\RuntimeException::class);
104
105
        $filter = new CallbackFilter();
106
107
        $filter->setOption('callback', null);
108
        $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...
109
    }
110
111
    public function testCallbackNotCallable(): void
112
    {
113
        $this->expectException(\RuntimeException::class);
114
115
        $filter = new CallbackFilter();
116
117
        $filter->setOption('callback', 'someCallback');
118
        $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...
119
    }
120
}
121