Completed
Push — master ( 007b3d...3e6077 )
by
unknown
05:34
created

GedmoOrmTest::testPersonalTranslatableEntity()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
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\TranslationBundle\Filter\TranslationFieldFilter;
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 TranslationFieldFilter();
90
        $filter->initialize('title');
91
92
        $filter->filter($builder, 'o', 'title', array('type' => null, 'value' => 'foo'));
0 ignored issues
show
Documentation introduced by
array('type' => null, 'value' => 'foo') is of type array<string,null|string...ull","value":"string"}>, 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...
93
        $this->assertEquals(
94
            'SELECT o FROM '.self::ARTICLE.' o LEFT JOIN o.translations tff'
95
            ." WHERE (tff.field = 'title' AND tff.content LIKE '%foo%') OR o.title LIKE '%foo%'",
96
            $builder->getDQL()
97
        );
98
        $this->assertTrue($filter->isActive());
99
    }
100
101 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...
102
    {
103
        $qb = $this->em->createQueryBuilder()
104
                       ->select('o')
105
                       ->from(self::ARTICLE, 'o');
106
        $builder = new ProxyQuery($qb);
107
108
        $filter = new TranslationFieldFilter();
109
        $filter->initialize('title');
110
111
        $filter->filter($builder, 'o', 'title', array('type' => null, 'value' => null));
0 ignored issues
show
Documentation introduced by
array('type' => null, 'value' => null) is of type array<string,null,{"type":"null","value":"null"}>, 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...
112
        $this->assertEquals(
113
            'SELECT o FROM '.self::ARTICLE.' o',
114
            $builder->getDQL()
115
        );
116
        $this->assertFalse($filter->isActive());
117
    }
118
119 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...
120
    {
121
        $qb = $this->em->createQueryBuilder()
122
                       ->select('o')
123
                       ->from(self::ARTICLE, 'o')
124
                       ->leftJoin('o.translations', 'tff');
125
        $builder = new ProxyQuery($qb);
126
127
        $filter = new TranslationFieldFilter();
128
        $filter->initialize('title');
129
130
        $filter->filter($builder, 'o', 'title', array('type' => null, 'value' => 'foo'));
0 ignored issues
show
Documentation introduced by
array('type' => null, 'value' => 'foo') is of type array<string,null|string...ull","value":"string"}>, 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...
131
        $this->assertEquals(
132
            'SELECT o FROM '.self::ARTICLE.' o LEFT JOIN o.translations tff'
133
            ." WHERE (tff.field = 'title' AND tff.content LIKE '%foo%') OR o.title LIKE '%foo%'",
134
            $builder->getDQL()
135
        );
136
        $this->assertTrue($filter->isActive());
137
    }
138
139
    protected function getUsedEntityFixtures()
140
    {
141
        return array(
142
            self::ARTICLE,
143
            self::TRANSLATION,
144
        );
145
    }
146
}
147