Completed
Push — master ( 15ea05...a6b9a5 )
by James
14s
created

ContainerFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testFactoryReturnsContainerIfIncluded() 0 13 1
A setUp() 0 4 1
A tearDown() 0 6 2
A testFactoryThrowsExceptionWhenFileDoesNotReturnContainer() 0 10 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
use Roave\BehatPsrContainer\Exception\NotAPsrContainer;
10
11
/**
12
 * @covers \Roave\BehatPsrContainer\ContainerFactory
13
 */
14
final class ContainerFactoryTest extends TestCase
15
{
16
    /**
17
     * @var string
18
     */
19
    private $tempFilename;
20
21
    public function setUp() : void
22
    {
23
        $this->tempFilename = tempnam(sys_get_temp_dir(), str_replace('\\', '_', __CLASS__) . '_');
24
    }
25
26
    public function tearDown() : void
27
    {
28
        if (file_exists($this->tempFilename)) {
29
            unlink($this->tempFilename);
30
        }
31
    }
32
33
    public function testFactoryThrowsExceptionWhenFileDoesNotReturnContainer() : void
34
    {
35
        file_put_contents(
36
            $this->tempFilename,
37
            "<?php return new \stdClass();"
38
        );
39
40
        $this->expectException(NotAPsrContainer::class);
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