File: ConfigTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (87 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Utils;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Utils\Config;

/**
 * Tests for SimpleSAML\Utils\Config
 */
class ConfigTest extends TestCase
{
    /**
     * Test default config dir with not environment variable
     * @return void
     */
    public function testDefaultConfigDir(): void
    {
        // clear env var
        putenv('SIMPLESAMLPHP_CONFIG_DIR');
        $configDir = Config::getConfigDir();

        $this->assertEquals($configDir, dirname(dirname(dirname(dirname(__DIR__)))) . '/config');
    }


    /**
     * Test valid dir specified by env var overrides default config dir
     * @return void
     */
    public function testEnvVariableConfigDir(): void
    {
        putenv('SIMPLESAMLPHP_CONFIG_DIR=' . __DIR__);
        $configDir = Config::getConfigDir();

        $this->assertEquals($configDir, __DIR__);
    }

    /**
     * Test valid dir specified by env redirect var overrides default config dir
     * @return void
     */
    public function testEnvRedirectVariableConfigDir(): void
    {
        putenv('REDIRECT_SIMPLESAMLPHP_CONFIG_DIR=' . __DIR__);
        $configDir = Config::getConfigDir();

        $this->assertEquals($configDir, __DIR__);
    }


    /**
     * Test which directory takes precedence
     * @return void
     */
    public function testEnvRedirectPriorityVariableConfigDir(): void
    {
        putenv('SIMPLESAMLPHP_CONFIG_DIR=' . dirname(__DIR__));
        putenv('REDIRECT_SIMPLESAMLPHP_CONFIG_DIR=' . __DIR__);
        $configDir = Config::getConfigDir();

        $this->assertEquals($configDir, dirname(__DIR__));
    }


    /**
     * Test invalid dir specified by env var results in a thrown exception
     * @return void
     */
    public function testInvalidEnvVariableConfigDirThrowsException(): void
    {
        // I used a random hash to ensure this test directory is always invalid
        $invalidDir = __DIR__ . '/e9826ad19cbc4f5bf20c0913ffcd2ce6';
        putenv('SIMPLESAMLPHP_CONFIG_DIR=' . $invalidDir);

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage(
            'Config directory specified by environment variable SIMPLESAMLPHP_CONFIG_DIR is not a directory.  ' .
            'Given: "' . $invalidDir . '"'
        );

        Config::getConfigDir();
    }
}