File: DefinitionFileTest.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 (49 lines) | stat: -rw-r--r-- 1,651 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
<?php

declare(strict_types=1);

namespace DI\Test\UnitTest\Definition\Source;

use DI\Definition\ObjectDefinition;
use DI\Definition\Source\DefinitionFile;
use DI\Definition\ValueDefinition;
use PHPUnit\Framework\TestCase;

/**
 * @covers \DI\Definition\Source\DefinitionFile
 */
#[\PHPUnit\Framework\Attributes\CoversClass(\DI\Definition\Source\DefinitionFile::class)]
class DefinitionFileTest extends TestCase
{
    /**
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function should_load_definition_from_file()
    {
        $source = new DefinitionFile(__DIR__ . '/Fixtures/definitions.php');

        /** @var ValueDefinition $definition */
        $definition = $source->getDefinition('foo');
        $this->assertInstanceOf(ValueDefinition::class, $definition);
        $this->assertEquals('bar', $definition->getValue());
        $this->assertIsString($definition->getValue());

        /** @var ObjectDefinition $definition */
        $definition = $source->getDefinition('bim');
        $this->assertInstanceOf(ObjectDefinition::class, $definition);
        $this->assertEquals('bim', $definition->getName());
        $this->assertEquals('bim', $definition->getClassName());
    }

    /**
     * @see https://github.com/PHP-DI/PHP-DI/issues/242
     */
    public function testDefinitionsWithoutKeyThrowAnError()
    {
        $this->expectException('Exception');
        $this->expectExceptionMessage('The PHP-DI definition is not indexed by an entry name in the definition array');
        $source = new DefinitionFile(__DIR__ . '/Fixtures/definitions-fail.php');
        $source->getDefinition('foo');
    }
}