File: StringDefinitionTest.php

package info (click to toggle)
php-di 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,932 kB
  • sloc: php: 10,572; makefile: 42; xml: 17; sh: 10; pascal: 5
file content (93 lines) | stat: -rw-r--r-- 2,870 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace DI\Test\IntegrationTest\Definitions;

use DI\ContainerBuilder;
use DI\Test\IntegrationTest\BaseContainerTest;
use DI\DependencyException;

/**
 * Test string definitions.
 */
class StringDefinitionTest extends BaseContainerTest
{
    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_string_without_placeholder(ContainerBuilder $builder)
    {
        $builder->addDefinitions([
            'foo' => \DI\string('bar'),
        ]);
        $container = $builder->build();

        self::assertEntryIsCompiled($container, 'foo');
        $this->assertEquals('bar', $container->get('foo'));
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_string_with_placeholder(ContainerBuilder $builder)
    {
        $builder->addDefinitions([
            'foo'         => 'bar',
            'test-string' => \DI\string('Hello {foo}'),
        ]);
        $container = $builder->build();

        $this->assertEquals('Hello bar', $container->get('test-string'));
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_string_with_multiple_placeholders(ContainerBuilder $builder)
    {
        $builder->addDefinitions([
            'foo'         => 'bar',
            'bim'         => 'bam',
            'test-string' => \DI\string('Hello {foo}, {bim}'),
        ]);
        $container = $builder->build();

        $this->assertEquals('Hello bar, bam', $container->get('test-string'));
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_nested_string_expressions(ContainerBuilder $builder)
    {
        $builder->addDefinitions([
            'name'        => 'John',
            'welcome'     => \DI\string('Welcome {name}'),
            'test-string' => \DI\string('{welcome}!'),
        ]);
        $container = $builder->build();

        $this->assertEquals('Welcome John!', $container->get('test-string'));
    }

    /**
     * @dataProvider provideContainer
     */
    #[\PHPUnit\Framework\Attributes\DataProvider('provideContainer')]
    public function test_string_with_nonexistent_placeholder(ContainerBuilder $builder)
    {
        $this->expectException(DependencyException::class);
        $this->expectExceptionMessage('Error while parsing string expression for entry \'test-string\': No entry or class found for \'foo\'');
        $builder->addDefinitions([
            'test-string' => \DI\string('Hello {foo}'),
        ]);
        $container = $builder->build();

        $container->get('test-string');
    }
}