Completed
Pull Request — master (#7)
by James
03:57
created

ContainerFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 6 2
A testFactoryThrowsExceptionWhenFileDoesNotReturnContainer() 0 11 1
A testFactoryReturnsContainerIfIncluded() 0 13 1
1
<?php
2
declare(strict_types=1);
3
4
namespace RoaveTest\BehatPsrContainer;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Container\ContainerInterface;
8
use Roave\BehatPsrContainer\ContainerFactory;
9
10
/**
11
 * @covers \Roave\BehatPsrContainer\ContainerFactory
12
 */
13
final class ContainerFactoryTest extends TestCase
14
{
15
    /**
16
     * @var string
17
     */
18
    private $tempFilename;
19
20
    public function setUp() : void
21
    {
22
        $this->tempFilename = tempnam(sys_get_temp_dir(), str_replace('\\', '_', __CLASS__) . '_');
23
    }
24
25
    public function tearDown() : void
26
    {
27
        if (file_exists($this->tempFilename)) {
28
            unlink($this->tempFilename);
29
        }
30
    }
31
32
    public function testFactoryThrowsExceptionWhenFileDoesNotReturnContainer() : void
33
    {
34
        file_put_contents(
35
            $this->tempFilename,
36
            "<?php return new \stdClass();"
37
        );
38
39
        $this->expectException(\RuntimeException::class);
40
        $this->expectExceptionMessage('File ' . $this->tempFilename . ' must return a PSR-11 container');
41
        ContainerFactory::createContainerFromIncludedFile($this->tempFilename);
42
    }
43
44
    public function testFactoryReturnsContainerIfIncluded() : void
45
    {
46
        file_put_contents(
47
            $this->tempFilename,
48
            '<?php return new class implements \Psr\Container\ContainerInterface {
49
                public function get($id) {}
50
                public function has($id) {}
51
            };'
52
        );
53
54
        $container = ContainerFactory::createContainerFromIncludedFile($this->tempFilename);
55
        self::assertInstanceOf(ContainerInterface::class, $container);
56
    }
57
}
58