XdgTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class XdgTest extends PHPUnit_Framework_TestCase
  3. {
  4. /**
  5. * @return \XdgBaseDir\Xdg
  6. */
  7. public function getXdg()
  8. {
  9. return new \XdgBaseDir\Xdg();
  10. }
  11. public function testGetHomeDir()
  12. {
  13. putenv('HOME=/fake-dir');
  14. $this->assertEquals('/fake-dir', $this->getXdg()->getHomeDir());
  15. }
  16. public function testGetFallbackHomeDir()
  17. {
  18. putenv('HOME=');
  19. putenv('HOMEDRIVE=C:');
  20. putenv('HOMEPATH=fake-dir');
  21. $this->assertEquals('C:/fake-dir', $this->getXdg()->getHomeDir());
  22. }
  23. public function testXdgPutCache()
  24. {
  25. putenv('XDG_DATA_HOME=tmp/');
  26. putenv('XDG_CONFIG_HOME=tmp/');
  27. putenv('XDG_CACHE_HOME=tmp/');
  28. $this->assertEquals('tmp/', $this->getXdg()->getHomeCacheDir());
  29. }
  30. public function testXdgPutData()
  31. {
  32. putenv('XDG_DATA_HOME=tmp/');
  33. $this->assertEquals('tmp/', $this->getXdg()->getHomeDataDir());
  34. }
  35. public function testXdgPutConfig()
  36. {
  37. putenv('XDG_CONFIG_HOME=tmp/');
  38. $this->assertEquals('tmp/', $this->getXdg()->getHomeConfigDir());
  39. }
  40. public function testXdgDataDirsShouldIncludeHomeDataDir()
  41. {
  42. putenv('XDG_DATA_HOME=tmp/');
  43. putenv('XDG_CONFIG_HOME=tmp/');
  44. $this->assertArrayHasKey('tmp/', array_flip($this->getXdg()->getDataDirs()));
  45. }
  46. public function testXdgConfigDirsShouldIncludeHomeConfigDir()
  47. {
  48. putenv('XDG_CONFIG_HOME=tmp/');
  49. $this->assertArrayHasKey('tmp/', array_flip($this->getXdg()->getConfigDirs()));
  50. }
  51. /**
  52. * If XDG_RUNTIME_DIR is set, it should be returned
  53. */
  54. public function testGetRuntimeDir()
  55. {
  56. putenv('XDG_RUNTIME_DIR=/tmp/');
  57. $runtimeDir = $this->getXdg()->getRuntimeDir();
  58. $this->assertEquals(is_dir($runtimeDir), true);
  59. }
  60. /**
  61. * In strict mode, an exception should be shown if XDG_RUNTIME_DIR does not exist
  62. *
  63. * @expectedException \RuntimeException
  64. */
  65. public function testGetRuntimeDirShouldThrowException()
  66. {
  67. putenv('XDG_RUNTIME_DIR=');
  68. $this->getXdg()->getRuntimeDir(true);
  69. }
  70. /**
  71. * In fallback mode a directory should be created
  72. */
  73. public function testGetRuntimeDirShouldCreateDirectory()
  74. {
  75. putenv('XDG_RUNTIME_DIR=');
  76. $dir = $this->getXdg()->getRuntimeDir(false);
  77. $permission = decoct(fileperms($dir) & 0777);
  78. $this->assertEquals(700, $permission);
  79. }
  80. /**
  81. * Ensure, that the fallback directories are created with correct permission
  82. */
  83. public function testGetRuntimeShouldDeleteDirsWithWrongPermission()
  84. {
  85. $runtimeDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . XdgBaseDir\Xdg::RUNTIME_DIR_FALLBACK . getenv('USER');
  86. rmdir($runtimeDir);
  87. mkdir($runtimeDir, 0764, true);
  88. // Permission should be wrong now
  89. $permission = decoct(fileperms($runtimeDir) & 0777);
  90. $this->assertEquals(764, $permission);
  91. putenv('XDG_RUNTIME_DIR=');
  92. $dir = $this->getXdg()->getRuntimeDir(false);
  93. // Permission should be fixed
  94. $permission = decoct(fileperms($dir) & 0777);
  95. $this->assertEquals(700, $permission);
  96. }
  97. }