Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

NodeNameFilterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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\DoctrinePHPCRAdminBundle\Filter\NodeNameFilter;
15
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
16
17
class NodeNameFilterTest extends BaseTestCase
18
{
19
    public function setUp()
20
    {
21
        parent::setUp();
22
        $this->filter = new NodeNameFilter();
23
    }
24
25
    public function getChoiceTypeForEmptyTests()
26
    {
27
        return ChoiceType::TYPE_EQUAL;
28
    }
29
30
    public function testFilterNullData()
31
    {
32
        $res = $this->filter->filter($this->proxyQuery, 'a', 'somefield', null);
33
        $this->assertNull($res);
34
        $this->assertFalse($this->filter->isActive());
35
    }
36
37
    public function testFilterEmptyArrayData()
38
    {
39
        $res = $this->filter->filter($this->proxyQuery, 'a', 'somefield', array());
40
        $this->assertNull($res);
41
        $this->assertFalse($this->filter->isActive());
42
    }
43
44
    public function testFilterEmptyArrayDataSpecifiedType()
45
    {
46
        $res = $this->filter->filter($this->proxyQuery, 'a', 'somefield', array('type' => ChoiceType::TYPE_EQUAL));
47
        $this->assertNull($res);
48
        $this->assertFalse($this->filter->isActive());
49
    }
50
51
    public function testFilterEmptyArrayDataWithMeaninglessValue()
52
    {
53
        $this->proxyQuery->expects($this->never())
54
            ->method('andWhere');
55
56
        $this->filter->filter($this->proxyQuery, 'a', 'somefield', array('type' => ChoiceType::TYPE_EQUAL, 'value' => ' '));
57
        $this->assertFalse($this->filter->isActive());
58
    }
59
60
    public function getFilters()
61
    {
62
        return array(
63
            array('eqNodeName', ChoiceType::TYPE_EQUAL),
64
            array('likeNodeName', ChoiceType::TYPE_NOT_CONTAINS, '%somevalue%'),
65
            array('likeNodeName', ChoiceType::TYPE_CONTAINS, '%somevalue%'),
66
            array('likeNodeName', ChoiceType::TYPE_CONTAINS_WORDS, '%somevalue%'),
67
        );
68
    }
69
70
    /**
71
     * @dataProvider getFilters
72
     */
73
    public function testFilterSwitch($operatorMethod, $choiceType, $expectedValue = 'somevalue')
74
    {
75
        $this->proxyQuery->expects($this->exactly(1))
76
            ->method('getQueryBuilder')
77
            ->will($this->returnValue($this->qb));
78
79
        $this->filter->filter(
80
            $this->proxyQuery,
81
            'a',
82
            'somefield',
83
            array('type' => $choiceType, 'value' => 'somevalue')
84
        );
85
86
        $localName = $this->qbTester->getNode('where.constraint.operand_dynamic');
87
        $literal = $this->qbTester->getNode('where.constraint.operand_static');
88
89
        $this->assertEquals('a', $localName->getAlias());
90
        $this->assertEquals($expectedValue, $literal->getValue());
91
92
        $this->assertTrue($this->filter->isActive());
93
    }
94
}
95