aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/StudipFileloaderTest.php
blob: 62c5e0491d48679e1b18d8ae36e30f9b39b01c6a (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
60
61
62
<?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' => '<?php $CONF = 17; ',
                'config-2.php' => '<?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(Exception::class);
        StudipFileloader::load('var://pathto/not-there.php', $container);
    }
}