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\Collections\ArrayCollection; |
15
|
|
|
use Sonata\TranslationBundle\Model\Gedmo\AbstractPersonalTranslation; |
16
|
|
|
use Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface; |
17
|
|
|
use Sonata\TranslationBundle\Traits\Gedmo\PersonalTranslatable; |
18
|
|
|
use Sonata\TranslationBundle\Traits\Translatable; |
19
|
|
|
|
20
|
|
|
class ModelTranslatable implements TranslatableInterface |
21
|
|
|
{ |
22
|
|
|
use Translatable; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
class ModelPersonalTranslatable implements TranslatableInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
use PersonalTranslatable; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ArrayCollection |
31
|
|
|
*/ |
32
|
|
|
protected $translations; |
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->translations = new ArrayCollection(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
class ModelPersonalTranslation extends AbstractPersonalTranslation |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @author Nicolas Bastien <[email protected]> |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
class GedmoTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @test Translatable |
51
|
|
|
*/ |
52
|
|
|
public function testTranslatableModel() |
53
|
|
|
{ |
54
|
|
|
$model = new ModelTranslatable(); |
55
|
|
|
$model->setLocale('fr'); |
56
|
|
|
|
57
|
|
|
$this->assertSame('fr', $model->getLocale()); |
58
|
|
|
$this->assertTrue($model instanceof \Sonata\TranslationBundle\Model\TranslatableInterface); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @test PersonalTranslatable |
63
|
|
|
*/ |
64
|
|
|
public function testPersonalTranslatableModel() |
65
|
|
|
{ |
66
|
|
|
$model = new ModelPersonalTranslatable(); |
67
|
|
|
$model->setLocale('fr'); |
68
|
|
|
|
69
|
|
|
$this->assertSame('fr', $model->getLocale()); |
70
|
|
|
$this->assertTrue($model instanceof \Sonata\TranslationBundle\Model\TranslatableInterface); |
71
|
|
|
|
72
|
|
|
$model->addTranslation(new ModelPersonalTranslation('en', 'title', 'Title en')); |
73
|
|
|
$model->addTranslation(new ModelPersonalTranslation('it', 'title', 'Title it')); |
74
|
|
|
$model->addTranslation(new ModelPersonalTranslation('es', 'title', 'Title es')); |
75
|
|
|
|
76
|
|
|
$this->assertSame('Title en', $model->getTranslation('title', 'en')); |
77
|
|
|
$this->assertSame('Title it', $model->getTranslation('title', 'it')); |
78
|
|
|
$this->assertSame('Title es', $model->getTranslation('title', 'es')); |
79
|
|
|
|
80
|
|
|
$this->assertSame(3, count($model->getTranslations())); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.