Passed
Push — master ( baf85c...9b1b58 )
by Alexander
03:01
created

GiiTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 43
rs 10
wmc 6
1
<?php
2
3
namespace Yiisoft\Yii\Gii\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\Container\ContainerInterface;
7
use Yiisoft\Composer\Config\Builder;
8
use Yiisoft\Di\Container;
9
use Yiisoft\Files\FileHelper;
10
use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException;
11
use Yiisoft\Yii\Gii\Generator\Controller\Generator as ControllerGenerator;
12
use Yiisoft\Yii\Gii\GiiInterface;
13
14
/**
15
 * GiiTestCase is the base class for all gii related test cases
16
 */
17
class GiiTestCase extends TestCase
18
{
19
    private ?ContainerInterface $container;
20
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
        FileHelper::createDirectory(__DIR__ . '/runtime');
25
        $this->container = new Container(require Builder::path('tests', dirname(__DIR__)));
26
    }
27
28
    protected function tearDown(): void
29
    {
30
        parent::tearDown();
31
        FileHelper::removeDirectory(__DIR__ . '/runtime');
32
        $this->container = null;
33
    }
34
35
    protected function getContainer(): ContainerInterface
36
    {
37
        return $this->container;
38
    }
39
40
    public function testGeneratorInstance(): void
41
    {
42
        $controllerGenerator = $this->getContainer()->get(GiiInterface::class)->getGenerator('controller');
43
        $this->assertInstanceOf(ControllerGenerator::class, $controllerGenerator);
44
    }
45
46
    public function testUnknownGeneratorInstance(): void
47
    {
48
        $this->expectException(GeneratorNotFoundException::class);
49
        $this->getContainer()->get(GiiInterface::class)->getGenerator('unknown');
50
    }
51
52
    public function testWrongGeneratorInstance(): void
53
    {
54
        $this->getContainer()->get(GiiInterface::class)->addGenerator('wrong', new \stdClass());
55
        $this->expectException(\RuntimeException::class);
56
        $this->expectExceptionMessage(
57
            'Generator should be GeneratorInterface instance. "' . get_class(new \stdClass()) . '" given.'
58
        );
59
        $this->getContainer()->get(GiiInterface::class)->getGenerator('wrong');
60
    }
61
}
62