FLOW3-Composer /Tests/Unit/Configuration/Source/YamlSourceTest.php

Language PHP Lines 103
MD5 Hash deb1986407fac3943e166a1390f5411b
Repository https://github.com/christianjul/FLOW3-Composer.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
<?php
namespace TYPO3\FLOW3\Tests\Unit\Configuration\Source;

/*                                                                        *
 * This script belongs to the FLOW3 framework.                            *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 * Testcase for the YAML configuration source
 *
 */
class YamlSourceTest extends \TYPO3\FLOW3\Tests\UnitTestCase {

	/**
	 * Sets up this test case
	 *
	 */
	protected function setUp() {
		\vfsStreamWrapper::register();
		\vfsStreamWrapper::setRoot(new \vfsStreamDirectory('testDirectory'));
	}

	/**
	 * @test
	 */
	public function returnsEmptyArrayOnNonExistingFile() {
		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
		$configuration = $configurationSource->load('/ThisFileDoesNotExist');
		$this->assertEquals(array(), $configuration, 'No empty array was returned.');
	}

	/**
	 * @test
	 */
	public function optionSetInTheConfigurationFileReallyEndsUpInTheArray() {
		$pathAndFilename = __DIR__ . '/../Fixture/YAMLConfigurationFile';
		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
		$configuration = $configurationSource->load($pathAndFilename);
		$this->assertTrue($configuration['configurationFileHasBeenLoaded'], 'The option has not been set by the fixture.');
	}

	/**
	 * @test
	 */
	public function saveWritesArrayToGivenFileAsYAML() {
		$pathAndFilename = \vfsStream::url('testDirectory') . '/YAMLConfiguration';
		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
		$mockConfiguration = array(
			'configurationFileHasBeenLoaded' => TRUE,
			'foo' => array(
				'bar' => 'Baz'
			)
		);
		$configurationSource->save($pathAndFilename, $mockConfiguration);

		$yaml = 'configurationFileHasBeenLoaded: true' . chr(10) . 'foo:' . chr(10) . '    bar: Baz' . chr(10);
		$this->assertContains($yaml, file_get_contents($pathAndFilename . '.yaml'), 'Configuration was not written to the file.');
	}

	/**
	 * @test
	 */
	public function saveWritesDoesNotOverwriteExistingHeaderCommentsIfFileExists() {
		$pathAndFilename = \vfsStream::url('testDirectory') . '/YAMLConfiguration';
		$comment = '# This comment should stay' . chr(10) . 'Test: foo' . chr(10);
		file_put_contents($pathAndFilename . '.yaml', $comment);

		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
		$configurationSource->save($pathAndFilename, array('configurationFileHasBeenLoaded' => TRUE));

		$yaml = file_get_contents($pathAndFilename . '.yaml');
		$this->assertContains('# This comment should stay' . chr(10) . chr(10), $yaml, 'Header comment was removed from file.');
		$this->assertNotContains('Test: foo', $yaml);
	}

	/**
	 * @test
	 */
	public function yamlFileIsParsedToArray() {
		$expectedConfiguration = array(
			'configurationFileHasBeenLoaded' => TRUE,
			'TYPO3' => array(
				'FLOW3' => array(
					'something' => 'foo',
					'@bar' => 1,
					'aboolean' => TRUE
				)
			)
		);
		$pathAndFilename = __DIR__ . '/../Fixture/YAMLConfigurationFile';
		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
		$configuration = $configurationSource->load($pathAndFilename);
		$this->assertSame($expectedConfiguration, $configuration);
	}

}
?>
Back to Top