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

RepositoryClassWriterTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 11 1
A testWrite() 0 38 1
A setUp() 0 21 1
A testWriteWhenFileExists() 0 21 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\RepositoryClassWriter;
7
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\WriterConfig;
8
9
class RepositoryClassWriterTest extends EntityClassWriterTest
10
{
11
    const WRITER_CONFIG_CLASS_PREFIX_VALUE = '';
12
    const WRITER_CONFIG_CLASS_SUFFIX_VALUE = 'Repository';
13
14
    /**
15
     * @var WriterConfig
16
     */
17
    private $writerConfig;
18
19
    /**
20
     * @var EntityClassWriter
21
     */
22
    private $repositoryClassWriter;
23
24
    public function setUp()
25
    {
26
        parent::setUp();
27
28
        $this->writerConfig = new WriterConfig(
29
            static::WRITER_CONFIG_DIRECTORY_VALUE,
30
            static::WRITER_CONFIG_NAMESPACE_VALUE,
31
            static::WRITER_CONFIG_CLASS_PREFIX_VALUE,
32
            static::WRITER_CONFIG_CLASS_SUFFIX_VALUE
33
        );
34
35
        $this->repositoryClassWriter = new RepositoryClassWriter($this->writerConfig, $this->entityClassWriter);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Janisbiz\LightOrm\Dm...his->entityClassWriter) of type Janisbiz\LightOrm\Dms\My...r\RepositoryClassWriter is incompatible with the declared type Janisbiz\LightOrm\Dms\My...riter\EntityClassWriter of property $repositoryClassWriter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
37
        $this->dmsGeneratedDirectory = \implode(
38
            '',
39
            [
40
                $this->writerConfig->getDirectory(),
41
                DIRECTORY_SEPARATOR,
42
                $this->dmsDatabase->getPhpName(),
43
                DIRECTORY_SEPARATOR,
44
                $this->writerConfig->getClassSuffix(),
45
            ]
46
        );
47
    }
48
49
    public function testConstruct()
50
    {
51
        $getWriterConfigMethod = new \ReflectionMethod($this->repositoryClassWriter, 'getWriterConfig');
52
        $getWriterConfigMethod->setAccessible(true);
53
54
        $baseEntityClassWriterProperty = new \ReflectionProperty($this->repositoryClassWriter, 'entityClassWriter');
55
        $baseEntityClassWriterProperty->setAccessible(true);
56
57
        $this->assertTrue($getWriterConfigMethod->invoke($this->repositoryClassWriter) instanceof $this->writerConfig);
58
        $this->assertTrue(
59
            $baseEntityClassWriterProperty->getValue($this->repositoryClassWriter) instanceof $this->entityClassWriter
60
        );
61
    }
62
63
    /**
64
     * @param array $files
65
     */
66
    public function testWrite(array &$files = [])
67
    {
68
        $files = [];
69
        $this->repositoryClassWriter->write($this->dmsDatabase, $this->dmsTable, $files);
70
71
        $repositoryFilePath = \implode(
72
            '',
73
            [
74
                $this->dmsGeneratedDirectory,
75
                DIRECTORY_SEPARATOR,
76
                \sprintf('%s%s.php', $this->dmsTable->getPhpName(), $this->writerConfig->getClassSuffix())
77
            ]
78
        );
79
        $this->assertFileExists($repositoryFilePath);
80
        $this->assertEquals(
81
            /** @lang PHP */
82
            <<<PHP
83
<?php declare(strict_types=1);
84
85
namespace None\Existent\Namespace\DatabaseNameSnakeCase\Repository;
86
87
use Janisbiz\LightOrm\Dms\MySQL\Repository\AbstractRepository;
88
use None\Existent\Namespace\DatabaseNameSnakeCase\Repository\TableNameSnakeCase4Repository;
89
90
class TableNameSnakeCase4Repository extends AbstractRepository
91
{
92
    /**
93
     * @return string
94
     */
95
    protected function getEntityClass(): string
96
    {
97
        return TableNameSnakeCase4Repository::class;
98
    }
99
}
100
101
PHP
102
            ,
103
            \file_get_contents($repositoryFilePath)
104
        );
105
    }
106
107
    public function testWriteWhenFileExists()
108
    {
109
        $files = $this->createTestFiles(
110
            [
111
                \sprintf('%s%s.php', $this->dmsTable->getPhpName(), $this->writerConfig->getClassSuffix())
112
            ],
113
            $this->dmsGeneratedDirectory
114
        );
115
116
        $this->repositoryClassWriter->write($this->dmsDatabase, $this->dmsTable, $files);
117
118
        $repositoryFilePath = \implode(
119
            '',
120
            [
121
                $this->dmsGeneratedDirectory,
122
                DIRECTORY_SEPARATOR,
123
                \sprintf('%s%s.php', $this->dmsTable->getPhpName(), $this->writerConfig->getClassSuffix())
124
            ]
125
        );
126
        $this->assertFileExists($repositoryFilePath);
127
        $this->assertEquals('', \file_get_contents($repositoryFilePath));
128
    }
129
}
130