/Packages/Framework/TYPO3.FLOW3/Tests/Unit/Utility/FilesTest.php

https://github.com/crashd0wn/fx-viper · PHP · 192 lines · 77 code · 20 blank · 95 comment · 1 complexity · efc9da2173354ec4e6fc482d069e6c15 MD5 · raw file

  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Utility;
  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 as published by the *
  8. * Free Software Foundation, either version 3 of the License, or (at your *
  9. * option) any later version. *
  10. * *
  11. * This script is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
  13. * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU Lesser General Public *
  17. * License along with the script. *
  18. * If not, see http://www.gnu.org/licenses/lgpl.html *
  19. * *
  20. * The TYPO3 project - inspiring people to share! *
  21. * */
  22. /**
  23. * Testcase for the Utility Files class
  24. *
  25. */
  26. class FilesTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  27. /**
  28. * @test
  29. * @author Bastian Waidelich <bastian@typo3.org>
  30. */
  31. public function getUnixStylePathWorksForPathWithoutSlashes() {
  32. $path = 'foobar';
  33. $this->assertEquals('foobar', \TYPO3\FLOW3\Utility\Files::getUnixStylePath($path));
  34. }
  35. /**
  36. * @test
  37. * @author Bastian Waidelich <bastian@typo3.org>
  38. */
  39. public function getUnixStylePathWorksForPathWithForwardSlashes() {
  40. $path = 'foo/bar/test/';
  41. $this->assertEquals('foo/bar/test/', \TYPO3\FLOW3\Utility\Files::getUnixStylePath($path));
  42. }
  43. /**
  44. * @test
  45. * @author Bastian Waidelich <bastian@typo3.org>
  46. */
  47. public function getUnixStylePathWorksForPathWithBackwardSlashes() {
  48. $path = 'foo\\bar\\test\\';
  49. $this->assertEquals('foo/bar/test/', \TYPO3\FLOW3\Utility\Files::getUnixStylePath($path));
  50. }
  51. /**
  52. * @test
  53. * @author Bastian Waidelich <bastian@typo3.org>
  54. */
  55. public function getUnixStylePathWorksForPathWithForwardAndBackwardSlashes() {
  56. $path = 'foo/bar\\test/';
  57. $this->assertEquals('foo/bar/test/', \TYPO3\FLOW3\Utility\Files::getUnixStylePath($path));
  58. }
  59. /**
  60. * @test
  61. * @author Bastian Waidelich <bastian@typo3.org>
  62. */
  63. public function concatenatePathsWorksForEmptyPath() {
  64. $this->assertEquals('', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array()));
  65. }
  66. /**
  67. * @test
  68. * @author Bastian Waidelich <bastian@typo3.org>
  69. */
  70. public function concatenatePathsWorksForOnePath() {
  71. $this->assertEquals('foo', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('foo')));
  72. }
  73. /**
  74. * @test
  75. * @author Bastian Waidelich <bastian@typo3.org>
  76. */
  77. public function concatenatePathsWorksForTwoPath() {
  78. $this->assertEquals('foo/bar', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('foo', 'bar')));
  79. }
  80. /**
  81. * @test
  82. * @author Bastian Waidelich <bastian@typo3.org>
  83. */
  84. public function concatenatePathsWorksForPathsWithLeadingSlash() {
  85. $this->assertEquals('/foo/bar', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('/foo', 'bar')));
  86. }
  87. /**
  88. * @test
  89. * @author Bastian Waidelich <bastian@typo3.org>
  90. */
  91. public function concatenatePathsWorksForPathsWithTrailingSlash() {
  92. $this->assertEquals('foo/bar', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('foo', 'bar/')));
  93. }
  94. /**
  95. * @test
  96. * @author Bastian Waidelich <bastian@typo3.org>
  97. */
  98. public function concatenatePathsWorksForPathsWithLeadingAndTrailingSlash() {
  99. $this->assertEquals('/foo/bar/bar/foo', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('/foo/bar/', '/bar/foo/')));
  100. }
  101. /**
  102. * @test
  103. * @author Bastian Waidelich <bastian@typo3.org>
  104. */
  105. public function concatenatePathsWorksForBrokenPaths() {
  106. $this->assertEquals('/foo/bar/bar', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('\\foo/bar\\', '\\bar')));
  107. }
  108. /**
  109. * @test
  110. * @author Bastian Waidelich <bastian@typo3.org>
  111. */
  112. public function concatenatePathsWorksForEmptyPathArrayElements() {
  113. $this->assertEquals('foo/bar', \TYPO3\FLOW3\Utility\Files::concatenatePaths(array('foo', '', 'bar')));
  114. }
  115. /**
  116. * @test
  117. * @author Karsten Dambekalns <karsten@typo3.org>
  118. */
  119. public function getUnixStylePathWorksForPathWithDriveLetterAndBackwardSlashes() {
  120. $path = 'c:\\foo\\bar\\test\\';
  121. $this->assertEquals('c:/foo/bar/test/', \TYPO3\FLOW3\Utility\Files::getUnixStylePath($path));
  122. }
  123. /**
  124. * @author Karsten Dambekalns <karsten@typo3.org>
  125. */
  126. public function pathsWithProtocol() {
  127. return array(
  128. array('file:///foo\\bar', 'file:///foo/bar'),
  129. array('vfs:///foo\\bar', 'vfs:///foo/bar'),
  130. array('phar:///foo\\bar', 'phar:///foo/bar')
  131. );
  132. }
  133. /**
  134. * @test
  135. * @dataProvider pathsWithProtocol
  136. * @author Karsten Dambekalns <karsten@typo3.org>
  137. */
  138. public function getUnixStylePathWorksForPathWithProtocol($path, $expected) {
  139. $this->assertEquals($expected, \TYPO3\FLOW3\Utility\Files::getUnixStylePath($path));
  140. }
  141. /**
  142. * @test
  143. * @author Bastian Waidelich <bastian@typo3.org>
  144. */
  145. public function is_linkReturnsFalseForNonExistingFiles() {
  146. $this->assertFalse(\TYPO3\FLOW3\Utility\Files::is_link('NonExistingPath'));
  147. }
  148. /**
  149. * @test
  150. * @author Bastian Waidelich <bastian@typo3.org>
  151. */
  152. public function is_linkReturnsFalseForExistingFileThatIsNoSymlink() {
  153. $targetPathAndFilename = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array(sys_get_temp_dir(), 'FLOW3FilesTestFile'));
  154. file_put_contents($targetPathAndFilename, 'some data');
  155. $this->assertFalse(\TYPO3\FLOW3\Utility\Files::is_link($targetPathAndFilename));
  156. }
  157. /**
  158. * @test
  159. * @author Bastian Waidelich <bastian@typo3.org>
  160. */
  161. public function is_linkReturnsTrueForExistingSymlink() {
  162. $targetPathAndFilename = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array(sys_get_temp_dir(), 'FLOW3FilesTestFile'));
  163. file_put_contents($targetPathAndFilename, 'some data');
  164. $linkPathAndFilename = \TYPO3\FLOW3\Utility\Files::concatenatePaths(array(sys_get_temp_dir(), 'FLOW3FilesTestLink'));
  165. if (file_exists($linkPathAndFilename)) {
  166. unlink($linkPathAndFilename);
  167. }
  168. symlink($targetPathAndFilename, $linkPathAndFilename);
  169. $this->assertTrue(\TYPO3\FLOW3\Utility\Files::is_link($linkPathAndFilename));
  170. }
  171. }
  172. ?>