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