aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/StudipFileloaderTest.php
blob: ec457939a3722bd38cb7bf36ee4ff8e56a9ce8a6 (plain)
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
<?php

/**
 * Testcase for StudipFileloader class.
 *
 *
 * @author    mlunzena
 * @copyright (c) Authors
 */

class StudipFileloaderTestCase extends \Codeception\Test\Unit {

    public function setUp(): void {
        ArrayFileStream::set_filesystem(
            [
                'pathto' => [
                    'config-1.php' => '<? $CONF = 17; '
                    , 'config-2.php' => '<? $CONF = 17 + $offset; '
                ]]);

        if (!stream_wrapper_register("var", "ArrayFileStream")) {
            new Exception("Failed to register protocol");
        }
    }

    public function tearDown(): void {
        stream_wrapper_unregister("var");
    }


    public function test_should_inject_vars() {
        $container = [];
        StudipFileloader::load('var://pathto/config-1.php', $container);
        $this->assertEquals(['CONF' => 17], $container);
    }

    public function test_should_inject_vars_twice() {

        foreach (range(1,2) as $i) {
            $container = [];
            StudipFileloader::load('var://pathto/config-1.php', $container);
        }
        $this->assertEquals(['CONF' => 17], $container);
    }

    public function test_should_use_optional_bindings()
    {
        $container = [];
        $offset = 25;
        StudipFileloader::load('var://pathto/config-2.php', $container, compact('offset'));
        $this->assertEquals(['CONF' => 42], $container);
    }

    public function test_should_balk_upon_file_not_found()
    {
        $this->expectException(\PHPUnit\Framework\Exception::class);
        StudipFileloader::load('var://pathto/not-there.php', $container);
    }
}