Completed
Push — master ( ac98ee...01982b )
by ANTHONIUS
02:45
created

BuilderTest::testCompile()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\Tests\DI;
15
16
use Dotfiles\Core\DI\Builder;
17
use PHPUnit\Framework\TestCase;
18
use Symfony\Component\DependencyInjection\Container;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Dumper\DumperInterface;
22
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
23
24
class BuilderTest extends TestCase
25
{
26
    public function testCacheFileName(): void
27
    {
28
        // check default cache file generation
29
        $builder = new Builder();
30
        $this->assertContains(getcwd(), $builder->getCacheFileName());
31
32
        // check if directory should be created when not exists
33
        $dir = sys_get_temp_dir().'/dotfiles/test/cache/some-dir';
34
        if (is_dir($dir)) {
35
            rmdir($dir);
36
        }
37
        $builder->setCacheFileName($dir.'/some-file.php');
38
        $this->assertDirectoryExists($dir);
39
    }
40
41
    public function testCompile(): void
42
    {
43
        $definition = $this->createMock(Definition::class);
44
45
        $cb = $this->createMock(ContainerBuilder::class);
46
        $definition->expects($this->any())
47
            ->method('setPublic')
48
            ->will($this->returnSelf())
49
        ;
50
        $contents = <<<EOC
51
<?php
52
53
use Symfony\Component\DependencyInjection\Container;
54
55
class CachedContainer extends Container
56
{
57
}
58
EOC;
59
        $dumper = $this->createMock(PhpDumper::class);
60
        $dumper->expects($this->once())
61
            ->method('dump')
62
            ->with(array('class' => 'CachedContainer'))
63
            ->willReturn($contents)
64
        ;
65
        $cacheFileName = sys_get_temp_dir().'/dotfiles/test/container.php';
66
        $builder = new Builder();
67
        $builder->setCacheFileName($cacheFileName);
68
        $builder->setContainerBuilder($cb)
69
            ->setDumper($dumper)
70
        ;
71
        $builder->compile();
72
        $this->assertFileExists($cacheFileName);
73
        $this->assertEquals($contents, file_get_contents($cacheFileName));
74
75
        $this->assertInstanceOf(Container::class, $builder->getContainer());
76
    }
77
78
    public function testDefaults(): void
79
    {
80
        $builder = new Builder();
81
        $this->assertInstanceOf(ContainerBuilder::class, $builder->getContainerBuilder());
82
83
        $cb = $this->createMock(ContainerBuilder::class);
84
        $cb->expects($this->once())
85
            ->method('isCompiled')
86
            ->willReturn(true)
87
        ;
88
        $builder->setContainerBuilder($cb);
89
        $this->assertInstanceOf(DumperInterface::class, $builder->getDumper());
90
    }
91
}
92