695c3b644b2ef0b3b42476127dddd8f150229beb
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / Configuration / YamlSourceTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Configuration;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Form\Mvc\Configuration\Exception\NoSuchFileException;
19 use TYPO3\CMS\Form\Mvc\Configuration\Exception\ParseErrorException;
20 use TYPO3\CMS\Form\Mvc\Configuration\YamlSource;
21
22 /**
23 * Test case
24 */
25 class YamlSourceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27
28 /**
29 * @test
30 */
31 public function loadThrowsExceptionIfFileToLoadNotExists()
32 {
33 $this->expectException(NoSuchFileException::class);
34 $this->expectExceptionCode(1471473378);
35
36 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
37 'dummy',
38 ], [], '', false);
39
40 $input = [
41 'EXT:form/Resources/Forms/_example.yaml'
42 ];
43
44 $mockYamlSource->_call('load', $input);
45 }
46
47 /**
48 * @test
49 */
50 public function loadThrowsExceptionIfFileToLoadIsNotValidYamlUseSymfonyParser()
51 {
52 if (!extension_loaded('yaml')) {
53 $this->expectException(ParseErrorException::class);
54 $this->expectExceptionCode(1480195405);
55
56 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
57 'dummy',
58 ], [], '', false);
59
60 $input = [
61 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/Invalid.yaml'
62 ];
63
64 $mockYamlSource->_call('load', $input);
65 }
66 }
67
68 /**
69 * @test
70 */
71 public function loadThrowsExceptionIfFileToLoadIsNotValidYamlUsePhpExtensionParser()
72 {
73 if (!extension_loaded('yaml')) {
74 $this->markTestSkipped('Yaml PHP extension not available, skipping.');
75 }
76 $this->expectException(ParseErrorException::class);
77 $this->expectExceptionCode(1391894094);
78
79 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
80 'dummy',
81 ], [], '', false);
82
83 $input = [
84 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/Invalid.yaml'
85 ];
86
87 $mockYamlSource->_call('load', $input);
88 }
89
90 /**
91 * @test
92 */
93 public function getHeaderFromFileReturnsHeaderPart()
94 {
95 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, [
96 'dummy',
97 ], [], '', false);
98
99 $input = GeneralUtility::getFileAbsFileName('EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/Header.yaml');
100 $expected =
101 '# Header 1
102 # Header 2
103 ';
104
105 $this->assertSame($expected, $mockYamlSource->_call('getHeaderFromFile', $input));
106 }
107
108 /**
109 * @test
110 */
111 public function loadOverruleNonArrayValuesOverArrayValues()
112 {
113 $mockYamlSource = $this->getAccessibleMock(YamlSource::class, ['dummy'], [], '', false);
114
115 $input = [
116 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/OverruleNonArrayValuesOverArrayValues1.yaml',
117 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/OverruleNonArrayValuesOverArrayValues2.yaml'
118 ];
119
120 $expected = [
121 'Form' => [
122 'klaus01' => null,
123 'key03' => 'value2',
124 ],
125 ];
126
127 $this->assertSame($expected, $mockYamlSource->_call('load', $input));
128 }
129 }