aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/flexi/FactoryTest.php
blob: 2df9f6aaae74177281b5f14988dfd39517c14f8c (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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

use Flexi\Factory;
use Flexi\TemplateNotFoundException;
use Flexi\PhpTemplate;

final class FactoryTestCase extends \Codeception\Test\Unit
{
    private Factory $factory;

    public function setUp(): void
    {
        $this->setUpFS();

        $this->factory = new Factory('var://templates');
    }

    public function tearDown(): void
    {
        unset($this->factory);
        stream_wrapper_unregister('var');
    }

    public function setUpFS(): void
    {
        ArrayFileStream::set_filesystem([
            'templates' => [
                'foo.php'           => 'some content',
                'baz.unknown'       => 'some content',
                'multiplebasenames' => [
                    'foo.txt' => 'there is no matching template class',
                    'foo.php' => 'some content',
                    'bar.txt' => 'there is no matching template class',
                ],
                'baz.known-ext' => 'some content',
            ],
        ]);
        if (!stream_wrapper_register('var', ArrayFileStream::class)) {
            die('Failed to register protocol');
        }
    }

    public function testShouldCreateFactory()
    {
        $factory = new Factory('.');
        $this->assertNotNull($factory);
    }

    public function testShouldCreateFactoryUsingPath()
    {
        $path = 'var://';
        $factory = new Factory($path);
        $this->assertNotNull($factory);
    }

    public function testShouldOpenTemplateUsingRelativePath()
    {
        $foo = $this->factory->open('foo');
        $this->assertNotNull($foo);
    }

    public function testShouldOpenTemplateUsingAbsolutePath()
    {
        $foo = $this->factory->open('var://templates/foo');
        $this->assertNotNull($foo);
    }

    public function testShouldThrowAnExceptionOpeningAMissingTemplateWithoutFileExtension()
    {
        $this->expectException(TemplateNotFoundException::class);
        $this->factory->open('bar');
    }

    public function testShouldThrowAnExceptionOpeningAMissingTemplateWithFileExtension()
    {
        $this->expectException(TemplateNotFoundException::class);
        $this->factory->open('bar.php');
    }

    public function testShouldOpenTemplateUsingExtension()
    {
        $this->assertInstanceOf(
            PhpTemplate::class,
            $this->factory->open('foo.php')
        );
    }

    public function testShouldThrowAnExceptionWhenOpeningATemplateWithUnknownExtension()
    {
        $this->expectException(TemplateNotFoundException::class);
        $this->factory->open('baz');
    }

    public function testShouldThrowAnExceptionOpeningATemplateInANonExistingDirectory()
    {
        $this->expectException(TemplateNotFoundException::class);
        $this->factory->open('doesnotexist/foo');
    }

    public function testShouldSearchForASupportedTemplate()
    {
        $this->assertInstanceOf(
            PhpTemplate::class,
            $this->factory->open('multiplebasenames/foo')
        );
    }

    public function testShouldRespondToAddedHandlers()
    {
        $handler = new class('', $this->factory) extends Flexi\Template {
            public function _render(): string
            {
                return '';
            }
        };
        $this->factory->add_handler('known-ext', $handler::class);
        $this->factory->open('baz.known-ext');
    }
}