Completed
Pull Request — 2.x (#311)
by
unknown
01:26
created

KnplabsOrmTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 90
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 2
A testTranslationFieldFilter() 0 18 1
A testTranslationFieldFilterWithoutValue() 0 17 1
A testTranslationFieldFilterIfAlreadyJoined() 0 19 1
A getUsedEntityFixtures() 0 7 1
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\TranslationBundle\Tests\Model;
15
16
use Doctrine\Common\EventManager;
17
use Gedmo\Translatable\TranslatableListener;
18
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
19
use Sonata\TranslationBundle\Enum\TranslationFilterMode;
20
use Sonata\TranslationBundle\Filter\TranslationFieldFilter;
21
use Sonata\TranslationBundle\Test\DoctrineOrmTestCase;
22
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\Knplabs\Article;
23
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\Knplabs\ArticleTranslation;
24
25
/**
26
 * @author Omar Jbara <[email protected]>
27
 */
28
class KnplabsOrmTest extends DoctrineOrmTestCase
29
{
30
    public const ARTICLE = Article::class;
31
    public const TRANSLATION = ArticleTranslation::class;
32
33
    /** @var TranslatableListener */
34
    private $translatableListener;
35
36
    protected function setUp()
37
    {
38
        parent::setUp();
39
40
        if (!class_exists('Doctrine\\ORM\\Version')) {
41
            $this->markTestSkipped('Doctrine ORM is not available.');
42
        }
43
44
        $evm = new EventManager();
45
        $this->translatableListener = new TranslatableListener();
46
        $this->translatableListener->setTranslatableLocale('en');
47
        $this->translatableListener->setDefaultLocale('en');
48
        $evm->addEventSubscriber($this->translatableListener);
49
50
        $this->getMockSqliteEntityManager($evm);
51
    }
52
53
    public function testTranslationFieldFilter()
54
    {
55
        $qb = $this->em->createQueryBuilder()
56
            ->select('a')
57
            ->from(self::ARTICLE, 'a');
58
        $builder = new ProxyQuery($qb);
59
60
        $filter = new TranslationFieldFilter(TranslationFilterMode::KNPLABS);
61
        $filter->initialize('title');
62
63
        $filter->filter($builder, 'a', 'title', ['type' => null, 'value' => 'foo']);
64
        $this->assertSame(
65
            'SELECT a FROM '.self::ARTICLE.' a LEFT JOIN a.translations tff'
66
            ." WHERE tff.title LIKE '%foo%'",
67
            $builder->getDQL()
68
        );
69
        $this->assertTrue($filter->isActive());
70
    }
71
72
    public function testTranslationFieldFilterWithoutValue()
73
    {
74
        $qb = $this->em->createQueryBuilder()
75
            ->select('a')
76
            ->from(self::ARTICLE, 'a');
77
        $builder = new ProxyQuery($qb);
78
79
        $filter = new TranslationFieldFilter(TranslationFilterMode::KNPLABS);
80
        $filter->initialize('title');
81
82
        $filter->filter($builder, 'a', 'title', ['type' => null, 'value' => null]);
83
        $this->assertSame(
84
            'SELECT a FROM '.self::ARTICLE.' a',
85
            $builder->getDQL()
86
        );
87
        $this->assertFalse($filter->isActive());
88
    }
89
90
    public function testTranslationFieldFilterIfAlreadyJoined()
91
    {
92
        $qb = $this->em->createQueryBuilder()
93
            ->select('a')
94
            ->from(self::ARTICLE, 'a')
95
            ->leftJoin('a.translations', 'tff');
96
        $builder = new ProxyQuery($qb);
97
98
        $filter = new TranslationFieldFilter(TranslationFilterMode::KNPLABS);
99
        $filter->initialize('title');
100
101
        $filter->filter($builder, 'a', 'title', ['type' => null, 'value' => 'foo']);
102
        $this->assertSame(
103
            'SELECT a FROM '.self::ARTICLE.' a LEFT JOIN a.translations tff'
104
            ." WHERE tff.title LIKE '%foo%'",
105
            $builder->getDQL()
106
        );
107
        $this->assertTrue($filter->isActive());
108
    }
109
110
    protected function getUsedEntityFixtures(): array
111
    {
112
        return [
113
            self::ARTICLE,
114
            self::TRANSLATION,
115
        ];
116
    }
117
}
118