Completed
Pull Request — master (#4831)
by Jordi Sala
10:38 queued 05:56
created

TabMenuTest::testLabelTranslationDomainOverride()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 18
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\AdminBundle\Tests\Menu\Integration;
13
14
use Knp\Menu\MenuFactory;
15
use Knp\Menu\MenuItem;
16
use Prophecy\Argument;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
class TabMenuTest extends BaseMenuTest
20
{
21
    protected $translator;
22
23
    public function getTranslator()
24
    {
25
        if (isset($this->translator)) {
26
            return $this->translator;
27
        }
28
29
        return parent::getTranslator();
30
    }
31
32
    public function testLabelTranslationNominalCase()
33
    {
34
        $translatorProphecy = $this->prophesize(TranslatorInterface::class);
35
        $translatorProphecy
36
            ->trans(
37
                'some-label',
38
                [],
39
                Argument::any(), //messages or null
40
                null
41
            )
42
            ->willReturn('my-translation');
43
44
        $this->translator = $translatorProphecy->reveal();
45
        $factory = new MenuFactory();
46
        $menu = new MenuItem('test-menu', $factory);
47
        $menu->addChild('some-label', ['uri' => '/whatever']);
48
        $this->assertContains('my-translation', $this->renderMenu($menu));
49
    }
50
51
    public function testLabelTranslationWithParameters()
52
    {
53
        $params = ['my' => 'param'];
54
        $translatorProphecy = $this->prophesize(TranslatorInterface::class);
55
        $translatorProphecy
56
            ->trans(
57
                'some-label',
58
                $params,
59
                Argument::any(), // messages or null
60
                null
61
            )
62
            ->willReturn('my-translation');
63
64
        $this->translator = $translatorProphecy->reveal();
65
        $factory = new MenuFactory();
66
        $menu = new MenuItem('test-menu', $factory);
67
        $menu->addChild('some-label', ['uri' => '/whatever'])
68
            ->setExtra('translation_params', $params);
69
70
        $this->assertContains('my-translation', $this->renderMenu($menu));
71
    }
72
73
    public function testLabelTranslationDomainOverride()
74
    {
75
        $translatorProphecy = $this->prophesize(TranslatorInterface::class);
76
        $translatorProphecy
77
            ->trans('some-label', [], 'my_local_domain', null)
78
            ->willReturn('my-translation');
79
        $translatorProphecy
80
            ->trans('some-other-label', [], 'my_global_domain', null)
81
            ->willReturn('my-other-translation');
82
83
        $this->translator = $translatorProphecy->reveal();
84
        $factory = new MenuFactory();
85
        $menu = new MenuItem('test-menu', $factory);
86
        $menu->setExtra('translation_domain', 'my_global_domain');
87
        $menu->addChild('some-label', ['uri' => '/whatever'])
88
            ->setExtra('translation_domain', 'my_local_domain');
89
        $menu->addChild('some-other-label', ['uri' => '/whatever']);
90
91
        $html = $this->renderMenu($menu);
92
        $this->assertContains('my-translation', $html);
93
        $this->assertContains('my-other-translation', $html);
94
    }
95
96
    protected function getTemplate()
97
    {
98
        return 'Core/tab_menu_template.html.twig';
99
    }
100
}
101