Completed
Push — master ( 6b2632...1706e7 )
by
unknown
07:57 queued 10s
created

NodeNameFilterTest::testFilterSwitch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\DoctrinePHPCRAdminBundle\Filter\NodeNameFilter;
17
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
18
19
class NodeNameFilterTest extends BaseTestCase
20
{
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->filter = new NodeNameFilter();
0 ignored issues
show
Bug introduced by
The property filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    public function getChoiceTypeForEmptyTests()
28
    {
29
        return ChoiceType::TYPE_EQUAL;
30
    }
31
32
    public function testFilterNullData(): void
33
    {
34
        $res = $this->filter->filter($this->proxyQuery, 'a', 'somefield', null);
35
        $this->assertNull($res);
36
        $this->assertFalse($this->filter->isActive());
37
    }
38
39
    public function testFilterEmptyArrayData(): void
40
    {
41
        $res = $this->filter->filter($this->proxyQuery, 'a', 'somefield', []);
42
        $this->assertNull($res);
43
        $this->assertFalse($this->filter->isActive());
44
    }
45
46
    public function testFilterEmptyArrayDataSpecifiedType(): void
47
    {
48
        $res = $this->filter->filter($this->proxyQuery, 'a', 'somefield', ['type' => ChoiceType::TYPE_EQUAL]);
49
        $this->assertNull($res);
50
        $this->assertFalse($this->filter->isActive());
51
    }
52
53
    public function testFilterEmptyArrayDataWithMeaninglessValue(): void
54
    {
55
        $this->filter->filter($this->proxyQuery, 'a', 'somefield', ['type' => ChoiceType::TYPE_EQUAL, 'value' => ' ']);
56
        $this->assertFalse($this->filter->isActive());
57
    }
58
59
    public function getFilters()
60
    {
61
        return [
62
            ['eqNodeName', ChoiceType::TYPE_EQUAL],
63
            ['likeNodeName', ChoiceType::TYPE_NOT_CONTAINS, '%somevalue%'],
64
            ['likeNodeName', ChoiceType::TYPE_CONTAINS, '%somevalue%'],
65
            ['likeNodeName', ChoiceType::TYPE_CONTAINS_WORDS, '%somevalue%'],
66
        ];
67
    }
68
69
    /**
70
     * @dataProvider getFilters
71
     */
72
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = 'somevalue'): void
0 ignored issues
show
Unused Code introduced by
The parameter $operatorMethod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        $this->proxyQuery->expects($this->exactly(1))
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...
75
            ->method('getQueryBuilder')
76
            ->willReturn($this->qb);
77
78
        $this->filter->filter(
79
            $this->proxyQuery,
80
            'a',
81
            'somefield',
82
            ['type' => $choiceType, 'value' => 'somevalue']
83
        );
84
85
        $localName = $this->qbTester->getNode('where.constraint.operand_dynamic');
86
        $literal = $this->qbTester->getNode('where.constraint.operand_static');
87
88
        $this->assertSame('a', $localName->getAlias());
89
        $this->assertSame($expectedValue, $literal->getValue());
90
91
        $this->assertTrue($this->filter->isActive());
92
    }
93
}
94