UtilitiesTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of the Stash package.
  4. *
  5. * (c) Robert Hafner <tedivm@tedivm.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Stash\Test;
  11. use Stash\Utilities;
  12. use Stash\Driver\FileSystem;
  13. /**
  14. * @package Stash
  15. * @author Robert Hafner <tedivm@tedivm.com>
  16. */
  17. class UtilitiesTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testEncoding()
  20. {
  21. $this->assertEquals(Utilities::encoding(true), 'bool', 'encoding recognized \'true\' boolean.');
  22. $this->assertEquals(Utilities::encoding(false), 'bool', 'encoding recognized \'false\' boolean');
  23. $this->assertEquals(Utilities::encoding('String of doom!'), 'string', 'encoding recognized string scalar');
  24. $this->assertEquals(Utilities::encoding(234), 'numeric', 'encoding recognized integer scalar');
  25. $this->assertEquals(Utilities::encoding(1.432), 'numeric', 'encoding recognized float scalar');
  26. $this->assertEquals(Utilities::encoding(pow(2, 31)), 'serialize', 'encoding recognized large number');
  27. $this->assertEquals(Utilities::encoding(pow(2, 31) - 1), 'numeric', 'encoding recognized small number');
  28. $std = new \stdClass();
  29. $this->assertEquals(Utilities::encoding($std), 'serialize', 'encoding recognized object');
  30. $array = array(4, 5, 7);
  31. $this->assertEquals(Utilities::encoding($array), 'serialize', 'encoding recognized array');
  32. }
  33. public function testEncode()
  34. {
  35. $this->assertEquals(Utilities::encode(true), 'true', 'encode returned \'true\' as string.');
  36. $this->assertEquals(Utilities::encode(false), 'false', 'encode returned \'false\' as string');
  37. $this->assertEquals(Utilities::encode('String of doom!'), 'String of doom!', 'encode returned string scalar');
  38. $this->assertEquals(Utilities::encode(234), 234, 'encode returned integer scalar');
  39. $this->assertEquals(Utilities::encode(1.432), 1.432, 'encode returned float scalar');
  40. $std = new \stdClass();
  41. $this->assertEquals(Utilities::encode($std), serialize($std), 'encode serialized object');
  42. $array = array(4, 5, 7);
  43. $this->assertEquals(Utilities::encode($array), serialize($array), 'encode serialized array');
  44. }
  45. public function testDecode()
  46. {
  47. $this->assertTrue(Utilities::decode(Utilities::encode(true), Utilities::encoding(true)), 'decode unpacked boolean \'true\'.');
  48. $this->assertFalse(Utilities::decode(Utilities::encode(false), Utilities::encoding(false)), 'decode unpacked boolean \'false\'');
  49. $string = 'String of doom!';
  50. $this->assertEquals(Utilities::decode(Utilities::encode($string), Utilities::encoding($string)), $string, 'decode unpacked string');
  51. $this->assertEquals(Utilities::decode(Utilities::encode(234), Utilities::encoding(234)), 234, 'Sdecode unpacked integer');
  52. $this->assertEquals(Utilities::decode(Utilities::encode(1.432), Utilities::encoding(1.432)), 1.432, 'decode unpacked float');
  53. $std = new \stdClass();
  54. $this->assertEquals(Utilities::decode(Utilities::encode($std), Utilities::encoding($std)), $std, 'decode unpacked object');
  55. $array = array(4, 5, 7);
  56. $this->assertEquals(Utilities::decode(Utilities::encode($array), Utilities::encoding($array)), $array, 'decode unpacked array');
  57. }
  58. public function testGetBaseDirectory()
  59. {
  60. $filesystem = new FileSystem();
  61. $tmp = sys_get_temp_dir();
  62. $directory = Utilities::getBaseDirectory($filesystem);
  63. $this->assertStringStartsWith($tmp, $directory, 'Base directory is placed inside the system temp directory.');
  64. $this->assertTrue(is_dir($directory), 'Base Directory exists and is a directory');
  65. $this->assertTrue(touch($directory . 'test'), 'Base Directory is writeable.');
  66. }
  67. public function testDeleteRecursive()
  68. {
  69. $tmp = sys_get_temp_dir() . '/stash/';
  70. $dirOne = $tmp . 'test/delete/recursive';
  71. @mkdir($dirOne, 0770, true);
  72. touch($dirOne . '/test');
  73. touch($dirOne . '/test2');
  74. $dirTwo = $tmp . 'recursive/delete/test';
  75. @mkdir($dirTwo, 0770, true);
  76. touch($dirTwo . '/test3');
  77. touch($dirTwo . '/test4');
  78. $this->assertTrue(Utilities::deleteRecursive($dirTwo . '/test3'), 'deleteRecursive returned true when removing single file.');
  79. $this->assertFileNotExists($dirTwo . '/test3', 'deleteRecursive removed single file');
  80. $this->assertTrue(Utilities::deleteRecursive($tmp), 'deleteRecursive returned true when removing directories.');
  81. $this->assertFileNotExists($tmp, 'deleteRecursive cleared out the directory');
  82. $this->assertFalse(Utilities::deleteRecursive($tmp), 'deleteRecursive returned false when passed nonexistant directory');
  83. $tmp = sys_get_temp_dir() . '/stash/test/';
  84. $dirOne = $tmp . '/Test1';
  85. @mkdir($dirOne, 0770, true);
  86. $dirTwo = $tmp . '/Test2';
  87. @mkdir($dirTwo, 0770, true);
  88. Utilities::deleteRecursive($dirOne, true);
  89. $this->assertFileExists($dirTwo, 'deleteRecursive does not erase sibling directories.');
  90. Utilities::deleteRecursive($dirTwo, true);
  91. $this->assertFileNotExists($tmp, 'deleteRecursive cleared out the empty parent directory');
  92. }
  93. /**
  94. * @expectedException Stash\Exception\RuntimeException
  95. */
  96. public function testDeleteRecursiveRelativeException()
  97. {
  98. Utilities::deleteRecursive('../tests/fakename');
  99. }
  100. /**
  101. * @expectedException Stash\Exception\RuntimeException
  102. */
  103. public function testDeleteRecursiveRootException()
  104. {
  105. Utilities::deleteRecursive('/');
  106. }
  107. public function testCheckEmptyDirectory()
  108. {
  109. $tmp = sys_get_temp_dir() . '/stash/';
  110. $dir2 = $tmp . 'emptytest/';
  111. @mkdir($dir2, 0770, true);
  112. $this->assertTrue(Utilities::checkForEmptyDirectory($dir2), 'Returns true for empty directories');
  113. $this->assertFalse(Utilities::checkForEmptyDirectory($tmp), 'Returns false for non-empty directories');
  114. Utilities::deleteRecursive($tmp);
  115. }
  116. /**
  117. * @expectedException Stash\Exception\RuntimeException
  118. */
  119. public function testCheckFileSystemPermissionsNullException()
  120. {
  121. Utilities::checkFileSystemPermissions(null, '0644');
  122. }
  123. /**
  124. * @expectedException Stash\Exception\InvalidArgumentException
  125. */
  126. public function testCheckFileSystemPermissionsFileException()
  127. {
  128. $tmp = sys_get_temp_dir() . '/stash/';
  129. $dir2 = $tmp . 'emptytest/';
  130. @mkdir($dir2, 0770, true);
  131. touch($dir2 . 'testfile');
  132. Utilities::checkFileSystemPermissions($dir2 . 'testfile', '0644');
  133. }
  134. /**
  135. * @expectedException Stash\Exception\InvalidArgumentException
  136. */
  137. public function testCheckFileSystemPermissionsUnaccessibleException()
  138. {
  139. Utilities::checkFileSystemPermissions('/fakedir/cache', '0644');
  140. }
  141. /**
  142. * @expectedException Stash\Exception\InvalidArgumentException
  143. */
  144. public function testCheckFileSystemPermissionsUnwrittableException()
  145. {
  146. Utilities::checkFileSystemPermissions('/home', '0644');
  147. }
  148. }