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

Tests/Traits/GedmoOrmTest.php (3 issues)

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
/*
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)
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()
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
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()
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
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()
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
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