3 class XdgTest extends PHPUnit_Framework_TestCase
6 * @return \XdgBaseDir\Xdg
8 public function getXdg()
10 return new \XdgBaseDir\Xdg();
13 public function testGetHomeDir()
15 putenv('HOME=/fake-dir');
16 $this->assertEquals('/fake-dir', $this->getXdg()->getHomeDir());
19 public function testGetFallbackHomeDir()
22 putenv('HOMEDRIVE=C:');
23 putenv('HOMEPATH=fake-dir');
24 $this->assertEquals('C:/fake-dir', $this->getXdg()->getHomeDir());
27 public function testXdgPutCache()
29 putenv('XDG_DATA_HOME=tmp/');
30 putenv('XDG_CONFIG_HOME=tmp/');
31 putenv('XDG_CACHE_HOME=tmp/');
32 $this->assertEquals('tmp/', $this->getXdg()->getHomeCacheDir());
35 public function testXdgPutData()
37 putenv('XDG_DATA_HOME=tmp/');
38 $this->assertEquals('tmp/', $this->getXdg()->getHomeDataDir());
41 public function testXdgPutConfig()
43 putenv('XDG_CONFIG_HOME=tmp/');
44 $this->assertEquals('tmp/', $this->getXdg()->getHomeConfigDir());
47 public function testXdgDataDirsShouldIncludeHomeDataDir()
49 putenv('XDG_DATA_HOME=tmp/');
50 putenv('XDG_CONFIG_HOME=tmp/');
52 $this->assertArrayHasKey('tmp/', array_flip($this->getXdg()->getDataDirs()));
55 public function testXdgConfigDirsShouldIncludeHomeConfigDir()
57 putenv('XDG_CONFIG_HOME=tmp/');
59 $this->assertArrayHasKey('tmp/', array_flip($this->getXdg()->getConfigDirs()));
63 * If XDG_RUNTIME_DIR is set, it should be returned
65 public function testGetRuntimeDir()
67 putenv('XDG_RUNTIME_DIR=/tmp/');
68 $runtimeDir = $this->getXdg()->getRuntimeDir();
70 $this->assertEquals(is_dir($runtimeDir), true);
74 * In strict mode, an exception should be shown if XDG_RUNTIME_DIR does not exist
76 * @expectedException \RuntimeException
78 public function testGetRuntimeDirShouldThrowException()
80 putenv('XDG_RUNTIME_DIR=');
81 $this->getXdg()->getRuntimeDir(true);
85 * In fallback mode a directory should be created
87 public function testGetRuntimeDirShouldCreateDirectory()
89 putenv('XDG_RUNTIME_DIR=');
90 $dir = $this->getXdg()->getRuntimeDir(false);
91 $permission = decoct(fileperms($dir) & 0777);
92 $this->assertEquals(700, $permission);
96 * Ensure, that the fallback directories are created with correct permission
98 public function testGetRuntimeShouldDeleteDirsWithWrongPermission()
100 $runtimeDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . XdgBaseDir\Xdg::RUNTIME_DIR_FALLBACK . getenv('USER');
103 mkdir($runtimeDir, 0764, true);
105 // Permission should be wrong now
106 $permission = decoct(fileperms($runtimeDir) & 0777);
107 $this->assertEquals(764, $permission);
109 putenv('XDG_RUNTIME_DIR=');
110 $dir = $this->getXdg()->getRuntimeDir(false);
112 // Permission should be fixed
113 $permission = decoct(fileperms($dir) & 0777);
114 $this->assertEquals(700, $permission);