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