Completed
Pull Request — master (#7)
by James
02:12
created

ContainerFactoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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