Completed
Pull Request — 2.x (#143)
by
unknown
11:18
created

GedmoOrmTest::testTranslationFieldFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 23
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 23
loc 23
rs 9.0856
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\TranslationBundle\Tests\Traits;
13
14
use Doctrine\Common\EventManager;
15
use Gedmo\Translatable\TranslatableListener;
16
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
17
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
18
use Sonata\TranslationBundle\Test\DoctrineOrmTestCase;
19
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslatable;
20
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation;
21
22
class GedmoOrmTest extends DoctrineOrmTestCase
23
{
24
    const ARTICLE = 'Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslatable';
25
    const TRANSLATION = 'Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation';
26
27
    /** @var TranslatableListener */
28
    private $translatableListener;
29
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
34
        if (!class_exists('Doctrine\\ORM\\Version')) {
35
            $this->markTestSkipped('Doctrine ORM is not available.');
36
        }
37
38
        $evm = new EventManager();
39
        $this->translatableListener = new TranslatableListener();
40
        $this->translatableListener->setTranslatableLocale('en');
41
        $this->translatableListener->setDefaultLocale('en');
42
        $evm->addEventSubscriber($this->translatableListener);
43
44
        $this->getMockSqliteEntityManager($evm);
45
    }
46
47
    public function testPersonalTranslatableEntity()
48
    {
49
        $article = new ArticlePersonalTranslatable();
50
        $article->setTitle('en');
51
52
        $this->em->persist($article);
53
        $this->em->flush();
54
55
        $this->translatableListener->setTranslatableLocale('de');
56
        $article->setTitle('de');
57
58
        $ltTranslation = new ArticlePersonalTranslation();
59
        $ltTranslation
60
            ->setField('title')
61
            ->setContent('lt')
62
            ->setObject($article)
0 ignored issues
show
Documentation introduced by
$article is of type object<Sonata\Translatio...lePersonalTranslatable>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
            ->setLocale('lt')
64
        ;
65
        $this->em->persist($ltTranslation);
66
        $this->em->persist($article);
67
        $this->em->flush();
68
69
        // Tests if Gedmo\Locale annotation exists
70
        $article->setLocale('pl');
71
        $article->setTitle('pl');
72
        $this->em->persist($article);
73
        $this->em->flush();
74
75
        $this->em->clear();
76
77
        $article = $this->em->find(self::ARTICLE, array('id' => 1));
78
        $translations = $article->getTranslations();
79
        $this->assertCount(3, $translations);
80
    }
81
82 View Code Duplication
    public function testTranslationFieldFilter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $qb = $this->em->createQueryBuilder()
85
                       ->select('o')
86
                       ->from(self::ARTICLE, 'o');
87
        $builder = new ProxyQuery($qb);
88
89
        $filter = new CallbackFilter();
90
        $filter->initialize('title', array(
91
            'callback' => array(
92
                'Sonata\TranslationBundle\Admin\Extension\Gedmo\TranslatableAdminExtension',
93
                'translationFieldFilter',
94
            ),
95
        ));
96
97
        $filter->filter($builder, 'o', 'title', array('type' => null, 'value' => 'foo'));
98
        $this->assertEquals(
99
            "SELECT o FROM ".self::ARTICLE." o LEFT JOIN o.translations t"
100
            ." WHERE (t.field = 'title' AND t.content LIKE '%foo%') OR o.title LIKE '%foo%'",
101
            $builder->getDQL()
102
        );
103
        $this->assertEquals(true, $filter->isActive());
104
    }
105
106 View Code Duplication
    public function testTranslationFieldFilterWithoutValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $qb = $this->em->createQueryBuilder()
109
                       ->select('o')
110
                       ->from(self::ARTICLE, 'o');
111
        $builder = new ProxyQuery($qb);
112
113
        $filter = new CallbackFilter();
114
        $filter->initialize('title', array(
115
            'callback' => array(
116
                'Sonata\TranslationBundle\Admin\Extension\Gedmo\TranslatableAdminExtension',
117
                'translationFieldFilter',
118
            ),
119
        ));
120
121
        $filter->filter($builder, 'o', 'title', array('type' => null, 'value' => null));
122
        $this->assertEquals(
123
            "SELECT o FROM ".self::ARTICLE." o",
124
            $builder->getDQL()
125
        );
126
        $this->assertEquals(false, $filter->isActive());
127
    }
128
129 View Code Duplication
    public function testTranslationFieldFilterIfAlreadyJoined()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $qb = $this->em->createQueryBuilder()
132
                       ->select('o')
133
                       ->from(self::ARTICLE, 'o')
134
                       ->leftJoin('o.translations', 't');
135
        $builder = new ProxyQuery($qb);
136
137
        $filter = new CallbackFilter();
138
        $filter->initialize('title', array(
139
            'callback' => array(
140
                'Sonata\TranslationBundle\Admin\Extension\Gedmo\TranslatableAdminExtension',
141
                'translationFieldFilter',
142
            ),
143
        ));
144
145
        $filter->filter($builder, 'o', 'title', array('type' => null, 'value' => 'foo'));
146
        $this->assertEquals(
147
            "SELECT o FROM ".self::ARTICLE." o LEFT JOIN o.translations t"
148
            ." WHERE (t.field = 'title' AND t.content LIKE '%foo%') OR o.title LIKE '%foo%'",
149
            $builder->getDQL()
150
        );
151
        $this->assertEquals(true, $filter->isActive());
152
    }
153
154
    protected function getUsedEntityFixtures()
155
    {
156
        return array(
157
            self::ARTICLE,
158
            self::TRANSLATION,
159
        );
160
    }
161
}
162