Completed
Push — 2.x ( 359f6e )
by Sullivan
16:25 queued 14:10
created

CallbackFilterTest::testFilterMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
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\DoctrineORMAdminBundle\Tests\Filter;
13
14
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
15
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
16
17
class CallbackFilterTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testFilterClosure()
20
    {
21
        $builder = new ProxyQuery(new QueryBuilder);
22
23
        $filter = new CallbackFilter;
24
        $filter->initialize('field_name', array(
25
            'callback' => function($builder, $alias, $field, $value) {
26
                $builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field));
27
                $builder->setParameter('value', $value);
28
29
                return true;
30
            }
31
        ));
32
33
        $filter->filter($builder, 'alias', 'field', 'myValue');
34
35
        $this->assertEquals(array('CUSTOM QUERY alias.field'), $builder->query);
0 ignored issues
show
Documentation introduced by
The property query does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
        $this->assertEquals(array('value' => 'myValue'), $builder->parameters);
0 ignored issues
show
Documentation introduced by
The property parameters does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
        $this->assertEquals(true, $filter->isActive());
38
    }
39
40
    public function testFilterMethod()
41
    {
42
        $builder = new ProxyQuery(new QueryBuilder);
43
44
        $filter = new CallbackFilter;
45
        $filter->initialize('field_name', array(
46
            'callback' => array($this, 'customCallback')
47
        ));
48
49
        $filter->filter($builder, 'alias', 'field', 'myValue');
50
51
        $this->assertEquals(array('CUSTOM QUERY alias.field'), $builder->query);
0 ignored issues
show
Documentation introduced by
The property query does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
        $this->assertEquals(array('value' => 'myValue'), $builder->parameters);
0 ignored issues
show
Documentation introduced by
The property parameters does not exist on object<Sonata\DoctrineOR...le\Datagrid\ProxyQuery>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
        $this->assertEquals(true, $filter->isActive());
54
    }
55
56
    public function customCallback($builder, $alias, $field, $value)
57
    {
58
        $builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field));
59
        $builder->setParameter('value', $value);
60
61
        return true;
62
    }
63
64
    /**
65
     * @expectedException RuntimeException
66
     */
67
    public function testFilterException()
68
    {
69
        $builder = new ProxyQuery(new QueryBuilder);
70
71
        $filter = new CallbackFilter;
72
        $filter->initialize('field_name', array());
73
74
        $filter->filter($builder, 'alias', 'field', 'myValue');
75
    }
76
}
77