Completed
Push — master ( bae860...fc1986 )
by Grégoire
11s
created

tests/Filter/ModelFilterTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DoctrineMongoDBAdminBundle\Tests\Filter;
15
16
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
17
use Sonata\CoreBundle\Form\Type\EqualType;
18
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
19
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter;
20
21
class DocumentStub
22
{
23
    public function getId()
24
    {
25
        return decbin(random_int(0, getrandmax()));
26
    }
27
}
28
29
class ModelFilterTest extends FilterWithQueryBuilderTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
30
{
31
    /**
32
     * @param array $options
33
     *
34
     * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
35
     */
36
    public function getFieldDescription(array $options)
37
    {
38
        $fieldDescription = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
39
        $fieldDescription->expects($this->once())->method('getOptions')->will($this->returnValue($options));
40
        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('field_name'));
41
42
        return $fieldDescription;
43
    }
44
45
    public function testFilterEmpty(): void
46
    {
47
        $filter = new ModelFilter();
48
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);
49
50
        $builder = new ProxyQuery($this->getQueryBuilder());
51
52
        $filter->filter($builder, 'alias', 'field', null);
53
        $filter->filter($builder, 'alias', 'field', []);
54
55
        $this->assertFalse($filter->isActive());
56
    }
57
58
    public function testFilterArray(): void
59
    {
60
        $filter = new ModelFilter();
61
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
62
63
        $builder = new ProxyQuery($this->getQueryBuilder());
64
65
        $filter->filter($builder, 'alias', 'field', [
66
            'type' => EqualType::TYPE_IS_EQUAL,
67
            'value' => [new DocumentStub(), new DocumentStub()],
68
        ]);
69
70
        // the alias is now computer by the entityJoin method
71
        $this->assertTrue($filter->isActive());
72
    }
73
74
    public function testFilterScalar(): void
75
    {
76
        $filter = new ModelFilter();
77
        $filter->initialize('field_name', ['field_options' => ['class' => 'FooBar'], 'field_mapping' => true]);
78
79
        $builder = new ProxyQuery($this->getQueryBuilder());
80
81
        $filter->filter($builder, 'alias', 'field', ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
82
83
        $this->assertTrue($filter->isActive());
84
    }
85
86
    public function testAssociationWithInvalidMapping(): void
87
    {
88
        $this->expectException(\RuntimeException::class);
89
90
        $filter = new ModelFilter();
91
        $filter->initialize('field_name', ['mapping_type' => 'foo', 'field_mapping' => true]);
92
93
        $builder = new ProxyQuery($this->getQueryBuilder());
94
95
        $filter->apply($builder, 'asd');
96
    }
97
98
    public function testAssociationWithValidMappingAndEmptyFieldName(): void
99
    {
100
        $this->expectException(\RuntimeException::class);
101
102
        $filter = new ModelFilter();
103
        $filter->initialize('field_name', ['mapping_type' => ClassMetadataInfo::ONE, 'field_mapping' => true]);
104
105
        $builder = new ProxyQuery($this->getQueryBuilder());
106
107
        $filter->apply($builder, 'asd');
108
        $this->assertTrue($filter->isActive());
109
    }
110
111
    public function testAssociationWithValidMapping(): void
112
    {
113
        $filter = new ModelFilter();
114
        $filter->initialize('field_name', [
115
            'mapping_type' => ClassMetadataInfo::ONE,
116
            'field_name' => 'field_name',
117
            'association_mapping' => [
118
                'fieldName' => 'association_mapping',
119
            ], 'field_mapping' => true,
120
        ]);
121
122
        $builder = new ProxyQuery($this->getQueryBuilder());
123
124
        $filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
125
126
        $this->assertTrue($filter->isActive());
127
    }
128
129
    public function testAssociationWithValidParentAssociationMappings(): void
130
    {
131
        $filter = new ModelFilter();
132
        $filter->initialize('field_name', [
133
            'mapping_type' => ClassMetadataInfo::ONE,
134
            'field_name' => 'field_name',
135
            'parent_association_mappings' => [
136
                [
137
                    'fieldName' => 'association_mapping',
138
                ],
139
                [
140
                    'fieldName' => 'sub_association_mapping',
141
                ],
142
            ],
143
            'association_mapping' => [
144
                'fieldName' => 'sub_sub_association_mapping',
145
            ], 'field_mapping' => true,
146
        ]);
147
148
        $builder = new ProxyQuery($this->getQueryBuilder());
149
150
        $filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
151
152
        $this->assertTrue($filter->isActive());
153
    }
154
}
155