PageRenderTime 25ms CodeModel.GetById 7ms app.highlight 5ms RepoModel.GetById 11ms app.codeStats 0ms

/Tests/Unit/Configuration/Source/YamlSourceTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 103 lines | 59 code | 12 blank | 32 comment | 0 complexity | deb1986407fac3943e166a1390f5411b MD5 | raw file
  1<?php
  2namespace TYPO3\FLOW3\Tests\Unit\Configuration\Source;
  3
  4/*                                                                        *
  5 * This script belongs to the FLOW3 framework.                            *
  6 *                                                                        *
  7 * It is free software; you can redistribute it and/or modify it under    *
  8 * the terms of the GNU Lesser General Public License, either version 3   *
  9 * of the License, or (at your option) any later version.                 *
 10 *                                                                        *
 11 * The TYPO3 project - inspiring people to share!                         *
 12 *                                                                        */
 13
 14/**
 15 * Testcase for the YAML configuration source
 16 *
 17 */
 18class YamlSourceTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
 19
 20	/**
 21	 * Sets up this test case
 22	 *
 23	 */
 24	protected function setUp() {
 25		\vfsStreamWrapper::register();
 26		\vfsStreamWrapper::setRoot(new \vfsStreamDirectory('testDirectory'));
 27	}
 28
 29	/**
 30	 * @test
 31	 */
 32	public function returnsEmptyArrayOnNonExistingFile() {
 33		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
 34		$configuration = $configurationSource->load('/ThisFileDoesNotExist');
 35		$this->assertEquals(array(), $configuration, 'No empty array was returned.');
 36	}
 37
 38	/**
 39	 * @test
 40	 */
 41	public function optionSetInTheConfigurationFileReallyEndsUpInTheArray() {
 42		$pathAndFilename = __DIR__ . '/../Fixture/YAMLConfigurationFile';
 43		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
 44		$configuration = $configurationSource->load($pathAndFilename);
 45		$this->assertTrue($configuration['configurationFileHasBeenLoaded'], 'The option has not been set by the fixture.');
 46	}
 47
 48	/**
 49	 * @test
 50	 */
 51	public function saveWritesArrayToGivenFileAsYAML() {
 52		$pathAndFilename = \vfsStream::url('testDirectory') . '/YAMLConfiguration';
 53		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
 54		$mockConfiguration = array(
 55			'configurationFileHasBeenLoaded' => TRUE,
 56			'foo' => array(
 57				'bar' => 'Baz'
 58			)
 59		);
 60		$configurationSource->save($pathAndFilename, $mockConfiguration);
 61
 62		$yaml = 'configurationFileHasBeenLoaded: true' . chr(10) . 'foo:' . chr(10) . '    bar: Baz' . chr(10);
 63		$this->assertContains($yaml, file_get_contents($pathAndFilename . '.yaml'), 'Configuration was not written to the file.');
 64	}
 65
 66	/**
 67	 * @test
 68	 */
 69	public function saveWritesDoesNotOverwriteExistingHeaderCommentsIfFileExists() {
 70		$pathAndFilename = \vfsStream::url('testDirectory') . '/YAMLConfiguration';
 71		$comment = '# This comment should stay' . chr(10) . 'Test: foo' . chr(10);
 72		file_put_contents($pathAndFilename . '.yaml', $comment);
 73
 74		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
 75		$configurationSource->save($pathAndFilename, array('configurationFileHasBeenLoaded' => TRUE));
 76
 77		$yaml = file_get_contents($pathAndFilename . '.yaml');
 78		$this->assertContains('# This comment should stay' . chr(10) . chr(10), $yaml, 'Header comment was removed from file.');
 79		$this->assertNotContains('Test: foo', $yaml);
 80	}
 81
 82	/**
 83	 * @test
 84	 */
 85	public function yamlFileIsParsedToArray() {
 86		$expectedConfiguration = array(
 87			'configurationFileHasBeenLoaded' => TRUE,
 88			'TYPO3' => array(
 89				'FLOW3' => array(
 90					'something' => 'foo',
 91					'@bar' => 1,
 92					'aboolean' => TRUE
 93				)
 94			)
 95		);
 96		$pathAndFilename = __DIR__ . '/../Fixture/YAMLConfigurationFile';
 97		$configurationSource = new \TYPO3\FLOW3\Configuration\Source\YamlSource();
 98		$configuration = $configurationSource->load($pathAndFilename);
 99		$this->assertSame($expectedConfiguration, $configuration);
100	}
101
102}
103?>