Passed
Push — master ( 9b7912...982aac )
by Janis
02:15
created

EntityClassWriterTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 1
A testWriteWhenFileExists() 0 21 1
A testConstruct() 0 11 1
A testWrite() 0 29 1
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\Generator\Writer;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\EntityClassWriter;
6
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\WriterConfig;
7
8
class EntityClassWriterTest extends BaseEntityClassWriterTest
9
{
10
    const WRITER_CONFIG_CLASS_PREFIX_VALUE = '';
11
    const WRITER_CONFIG_CLASS_SUFFIX_VALUE = 'Entity';
12
13
    /**
14
     * @var WriterConfig
15
     */
16
    private $writerConfig;
17
18
    /**
19
     * @var EntityClassWriter
20
     */
21
    protected $entityClassWriter;
22
23
    public function setUp()
24
    {
25
        parent::setUp();
26
27
        $this->writerConfig = new WriterConfig(
28
            static::WRITER_CONFIG_DIRECTORY_VALUE,
29
            static::WRITER_CONFIG_NAMESPACE_VALUE,
30
            static::WRITER_CONFIG_CLASS_PREFIX_VALUE,
31
            static::WRITER_CONFIG_CLASS_SUFFIX_VALUE
32
        );
33
34
        $this->entityClassWriter = new EntityClassWriter($this->writerConfig, $this->baseEntityClassWriter);
35
36
        $this->dmsGeneratedDirectory = \implode(
37
            '',
38
            [
39
                $this->writerConfig->getDirectory(),
40
                DIRECTORY_SEPARATOR,
41
                $this->dmsDatabase->getPhpName(),
42
                DIRECTORY_SEPARATOR,
43
                $this->writerConfig->getClassSuffix(),
44
            ]
45
        );
46
    }
47
48
    public function testConstruct()
49
    {
50
        $getWriterConfigMethod = new \ReflectionMethod($this->entityClassWriter, 'getWriterConfig');
51
        $getWriterConfigMethod->setAccessible(true);
52
53
        $baseEntityClassWriterProperty = new \ReflectionProperty($this->entityClassWriter, 'baseEntityClassWriter');
54
        $baseEntityClassWriterProperty->setAccessible(true);
55
56
        $this->assertTrue($getWriterConfigMethod->invoke($this->entityClassWriter) instanceof $this->writerConfig);
57
        $this->assertTrue(
58
            $baseEntityClassWriterProperty->getValue($this->entityClassWriter) instanceof $this->baseEntityClassWriter
59
        );
60
    }
61
62
    /**
63
     * @param array $files
64
     */
65
    public function testWrite(array &$files = [])
66
    {
67
        $this->entityClassWriter->write($this->dmsDatabase, $this->dmsTable, $files);
68
69
        $entityFilePath = \implode(
70
            '',
71
            [
72
                $this->dmsGeneratedDirectory,
73
                DIRECTORY_SEPARATOR,
74
                \sprintf('%s%s.php', $this->dmsTable->getPhpName(), $this->writerConfig->getClassSuffix())
75
            ]
76
        );
77
        $this->assertFileExists($entityFilePath);
78
        $this->assertEquals(
79
            /** @lang PHP */
80
            <<<PHP
81
<?php declare(strict_types=1);
82
83
namespace None\Existent\Namespace\DatabaseNameSnakeCase\Entity;
84
85
use None\Existent\Namespace\DatabaseNameSnakeCase\TableNameSnakeCase4;
86
87
class TableNameSnakeCase4Entity extends TableNameSnakeCase4
88
{
89
}
90
91
PHP
92
            ,
93
            \file_get_contents($entityFilePath)
94
        );
95
    }
96
97
    public function testWriteWhenFileExists()
98
    {
99
        $files = $this->createTestFiles(
100
            [
101
                \sprintf('%s%s.php', $this->dmsTable->getPhpName(), $this->writerConfig->getClassSuffix())
102
            ],
103
            $this->dmsGeneratedDirectory
104
        );
105
106
        $this->entityClassWriter->write($this->dmsDatabase, $this->dmsTable, $files);
107
108
        $repositoryFilePath = \implode(
109
            '',
110
            [
111
                $this->dmsGeneratedDirectory,
112
                DIRECTORY_SEPARATOR,
113
                \sprintf('%s%s.php', $this->dmsTable->getPhpName(), $this->writerConfig->getClassSuffix())
114
            ]
115
        );
116
        $this->assertFileExists($repositoryFilePath);
117
        $this->assertEquals('', \file_get_contents($repositoryFilePath));
118
    }
119
}
120