[TASK] Improve TranslateViewHelperTest
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / TranslateViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Fixtures\TranslateViewHelperFixtureForEmptyString;
17 use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper;
18
19 /**
20 * Test class for TranslateViewHelper
21 */
22 class TranslateViewHelperTest extends ViewHelperBaseTestcase
23 {
24 /**
25 * @var TranslateViewHelper
26 */
27 protected $viewHelper;
28
29 /**
30 * @test
31 */
32 public function renderThrowsExceptionIfNoKeyOrIdParameterIsGiven()
33 {
34 $this->viewHelper = new TranslateViewHelper();
35 $this->injectDependenciesIntoViewHelper($this->viewHelper);
36
37 $this->expectException(\TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException::class);
38 $this->expectExceptionCode(1351584844);
39
40 $this->setArgumentsUnderTest(
41 $this->viewHelper,
42 []
43 );
44 $this->viewHelper->initializeArgumentsAndRender();
45 }
46
47 /**
48 * @test
49 */
50 public function renderReturnsStringForGivenKey()
51 {
52 $this->viewHelper = new TranslateViewHelperFixtureForEmptyString();
53 $this->injectDependenciesIntoViewHelper($this->viewHelper);
54
55 $this->viewHelper->setRenderChildrenClosure(
56 function () {
57 return '<p>hello world</p>';
58 }
59 );
60 $this->setArgumentsUnderTest(
61 $this->viewHelper,
62 [
63 'key' => 'foo'
64 ]
65 );
66 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
67 $this->assertEquals('<p>hello world</p>', $actualResult);
68 }
69
70 /**
71 * @test
72 */
73 public function renderReturnsStringForGivenId()
74 {
75 $this->viewHelper = new TranslateViewHelperFixtureForEmptyString();
76 $this->injectDependenciesIntoViewHelper($this->viewHelper);
77
78 $this->viewHelper->setRenderChildrenClosure(
79 function () {
80 return '<p>hello world</p>';
81 }
82 );
83 $this->setArgumentsUnderTest(
84 $this->viewHelper,
85 [
86 'key' => null,
87 'id' => 'bar'
88 ]
89 );
90 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
91 $this->assertEquals('<p>hello world</p>', $actualResult);
92 }
93
94 /**
95 * @test
96 */
97 public function renderReturnsDefaultIfNoTranslationIsFound()
98 {
99 $this->viewHelper = new TranslateViewHelperFixtureForEmptyString();
100 $this->injectDependenciesIntoViewHelper($this->viewHelper);
101
102 $this->viewHelper->setRenderChildrenClosure(
103 function () {
104 return 'default';
105 }
106 );
107 $this->setArgumentsUnderTest(
108 $this->viewHelper,
109 [
110 'key' => null,
111 'id' => 'bar',
112 'default' => 'default'
113 ]
114 );
115 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
116 $this->assertEquals('default', $actualResult);
117 }
118 }