Passed
Push — master ( d2e487...b38026 )
by Tobias
05:31
created

EditInPlaceTranslatorTest::testHtmlTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.6333
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\Tests\Unit\Translator;
13
14
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Component\Translation\Loader\ArrayLoader;
18
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
19
use Translation\Bundle\EditInPlace\ActivatorInterface;
20
use Translation\Bundle\Translator\EditInPlaceTranslator;
21
use Translation\Bundle\Translator\TranslatorInterface;
22
23
/**
24
 * @author Damien Alexandre <[email protected]>
25
 */
26
final class EditInPlaceTranslatorTest extends TestCase
27
{
28
    public function testWithNotLocaleAwareTranslator()
29
    {
30
        if (!\interface_exists(NewTranslatorInterface::class)) {
31
            $this->markTestSkipped('Relevant only when NewTranslatorInterface is available.');
32
        }
33
34
        $symfonyTranslator = $this->getMockBuilder(NewTranslatorInterface::class)->getMock();
35
        $activator = new FakeActivator(true);
36
        $requestStack = new RequestStack();
37
38
        $this->expectException(\InvalidArgumentException::class);
39
        $this->expectExceptionMessage('The given translator must implements LocaleAwareInterface.');
40
41
        new EditInPlaceTranslator($symfonyTranslator, $activator, $requestStack);
42
    }
43
44
    public function testEnabled(): void
45
    {
46
        $symfonyTranslator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
47
48
        $request = new Request();
49
        $requestStack = new RequestStack();
50
        $requestStack->push($request);
51
52
        $activator = new FakeActivator(true);
53
        $service = new EditInPlaceTranslator($symfonyTranslator, $activator, $requestStack);
54
55
        $this->assertSame(
56
            '<x-trans data-key="messages|key" data-value="" data-plain="" data-domain="messages" data-locale=""></x-trans>',
57
            $service->trans('key', [])
58
        );
59
    }
60
61
    public function testDisabled(): void
62
    {
63
        $symfonyTranslator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
64
65
        $request = new Request();
66
        $requestStack = new RequestStack();
67
        $requestStack->push($request);
68
69
        $activator = new FakeActivator(false);
70
        $service = new EditInPlaceTranslator($symfonyTranslator, $activator, $requestStack);
71
72
        $this->assertNull(
73
            $service->trans('key', [])
74
        );
75
    }
76
77
    public function testHtmlTranslation(): void
78
    {
79
        $symfonyTranslator = new \Symfony\Component\Translation\Translator('en', null, null, true);
80
        $symfonyTranslator->addLoader('array', new ArrayLoader());
81
        $symfonyTranslator->addResource('array', ['foo' => 'Normal content.'], 'en');
82
        $symfonyTranslator->addResource('array', ['bar' => 'Content with <b>HTML</b> in it.'], 'en');
83
        $symfonyTranslator->addResource('array', ['bar.attr' => 'Content with <b class="alert">HTML</b> in it.'], 'en');
84
85
        $request = new Request();
86
        $requestStack = new RequestStack();
87
        $requestStack->push($request);
88
89
        $activator = new FakeActivator(true);
90
        $service = new EditInPlaceTranslator($symfonyTranslator, $activator, $requestStack);
91
92
        $this->assertSame(
93
            '<x-trans data-key="messages|foo" data-value="Normal content." data-plain="Normal content." data-domain="messages" data-locale="en">Normal content.</x-trans>',
94
            $service->trans('foo', [])
95
        );
96
97
        $this->assertSame(
98
            '<x-trans data-key="messages|bar" data-value="Content with &lt;b&gt;HTML&lt;/b&gt; in it." data-plain="Content with &lt;b&gt;HTML&lt;/b&gt; in it." data-domain="messages" data-locale="en">Content with <b>HTML</b> in it.</x-trans>',
99
            $service->trans('bar', [])
100
        );
101
102
        $this->assertSame(
103
            '<x-trans data-key="messages|bar.attr" data-value="Content with &lt;b class=&quot;alert&quot;&gt;HTML&lt;/b&gt; in it." data-plain="Content with &lt;b class=&quot;alert&quot;&gt;HTML&lt;/b&gt; in it." data-domain="messages" data-locale="en">Content with <b class="alert">HTML</b> in it.</x-trans>',
104
            $service->trans('bar.attr', [])
105
        );
106
    }
107
}
108
109
class FakeActivator implements ActivatorInterface
110
{
111
    private $enabled;
112
113
    public function __construct(bool $enabled = true)
114
    {
115
        $this->enabled = $enabled;
116
    }
117
118
    public function checkRequest(Request $request = null): bool
119
    {
120
        return $this->enabled;
121
    }
122
}
123