NodeNameFilterTest::testFilterNullData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->filter = new NodeNameFilter();
25
    }
26
27
    public function getChoiceTypeForEmptyTests()
28
    {
29
        return ChoiceType::TYPE_EQUAL;
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
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]);
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
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' => ' ']);
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
56
        $this->assertFalse($this->filter->isActive());
57
    }
58
59
    public function getFilters()
60
    {
61
        return [
62
            ['eqNodeName', ChoiceType::TYPE_EQUAL],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
63
            ['likeNodeName', ChoiceType::TYPE_NOT_CONTAINS, '%somevalue%'],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_NOT_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_NOT_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
64
            ['likeNodeName', ChoiceType::TYPE_CONTAINS, '%somevalue%'],
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...oiceType::TYPE_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
65
            ['likeNodeName', ChoiceType::TYPE_CONTAINS_WORDS, '%somevalue%'],
66
        ];
67
    }
68
69
    /**
70
     * @dataProvider getFilters
71
     */
72
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = 'somevalue'): void
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