AbstractWriterTest::testRead()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.7998
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit\Generator\Writer;
4
5
use Janisbiz\LightOrm\Generator\Dms\DmsDatabaseInterface;
6
use Janisbiz\LightOrm\Generator\Dms\DmsTableInterface;
7
use Janisbiz\LightOrm\Generator\Writer\AbstractWriter;
8
use Janisbiz\LightOrm\Generator\Writer\AbstractWriterConfig;
9
use PHPUnit\Framework\TestCase;
10
11
class AbstractWriterTest extends TestCase
12
{
13
    use FileTrait;
14
15
    /**
16
     * @var AbstractWriter|\PHPUnit_Framework_MockObject_MockObject
17
     */
18
    private $abstractWriter;
19
20
    /**
21
     * @var AbstractWriterConfig|\PHPUnit_Framework_MockObject_MockObject
22
     */
23
    private $abstractWriterConfig;
24
25
    /**
26
     * @var DmsDatabaseInterface|\PHPUnit_Framework_MockObject_MockObject
27
     */
28
    private $dmsDatabase;
29
30
    /**
31
     * @var DmsTableInterface|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $dmsTable;
34
35
    public function setUp()
36
    {
37
        $this->abstractWriter = $this->getMockForAbstractClass(AbstractWriter::class);
38
39
        $this->abstractWriterConfig = $this->getMockForAbstractClass(AbstractWriterConfig::class);
40
        $abstractWriterConfigReflection = new \ReflectionClass($this->abstractWriterConfig);
41
42
        $directoryProperty = $abstractWriterConfigReflection->getProperty('directory');
43
        $directoryProperty->setAccessible(true);
44
        $directoryProperty->setValue($this->abstractWriterConfig, AbstractWriterConfigTest::DIRECTORY_VALUE);
45
46
        $namespaceProperty = $abstractWriterConfigReflection->getProperty('namespace');
47
        $namespaceProperty->setAccessible(true);
48
        $namespaceProperty->setValue($this->abstractWriterConfig, AbstractWriterConfigTest::NAMESPACE_VALUE);
49
50
        $classPrefixProperty = $abstractWriterConfigReflection->getProperty('classPrefix');
51
        $classPrefixProperty->setAccessible(true);
52
        $classPrefixProperty->setValue($this->abstractWriterConfig, AbstractWriterConfigTest::CLASS_PREFIX_VALUE);
53
54
        $classSuffixProperty = $abstractWriterConfigReflection->getProperty('classSuffix');
55
        $classSuffixProperty->setAccessible(true);
56
        $classSuffixProperty->setValue($this->abstractWriterConfig, AbstractWriterConfigTest::CLASS_SUFFIX_VALUE);
57
        
58
        $this->abstractWriter->method('getWriterConfig')->willReturn($this->abstractWriterConfig);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $this->abstractWriter->/** @scrutinizer ignore-call */ 
59
                               method('getWriterConfig')->willReturn($this->abstractWriterConfig);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
60
        $this->dmsDatabase = $this->createMock(DmsDatabaseInterface::class);
61
        $this->dmsDatabase->method('getPhpName')->willReturn('DmsDatabasePhpName');
62
63
        $this->dmsTable = $this->createMock(DmsTableInterface::class);
64
        $this->dmsTable->method('getPhpName')->willReturn('DmsTablePhpName');
65
    }
66
67
    public function testGenerateNamespace()
68
    {
69
        $this->assertEquals(
70
            \sprintf(
71
                '%s\\%s\\%s%s',
72
                AbstractWriterConfigTest::NAMESPACE_VALUE_EXPECTED,
73
                $this->dmsDatabase->getPhpName(),
74
                AbstractWriterConfigTest::CLASS_PREFIX_VALUE_EXPECTED,
75
                AbstractWriterConfigTest::CLASS_SUFFIX_VALUE_EXPECTED
76
            ),
77
            $this->abstractWriter->generateNamespace($this->dmsDatabase)
78
        );
79
    }
80
81
    public function testGenerateClassName()
82
    {
83
        $this->assertEquals(
84
            \implode(
85
                '',
86
                [
87
                    AbstractWriterConfigTest::CLASS_PREFIX_VALUE_EXPECTED,
88
                    $this->dmsTable->getPhpName(),
89
                    AbstractWriterConfigTest::CLASS_SUFFIX_VALUE_EXPECTED,
90
                ]
91
            ),
92
            $this->abstractWriter->generateClassName($this->dmsTable)
93
        );
94
    }
95
96
    public function testGenerateFQDN()
97
    {
98
        $this->assertEquals(
99
            \sprintf(
100
                '%s\\%s\\%s%s\\%s%s%s',
101
                AbstractWriterConfigTest::NAMESPACE_VALUE_EXPECTED,
102
                $this->dmsDatabase->getPhpName(),
103
                AbstractWriterConfigTest::CLASS_PREFIX_VALUE_EXPECTED,
104
                AbstractWriterConfigTest::CLASS_SUFFIX_VALUE_EXPECTED,
105
                AbstractWriterConfigTest::CLASS_PREFIX_VALUE_EXPECTED,
106
                $this->dmsTable->getPhpName(),
107
                AbstractWriterConfigTest::CLASS_SUFFIX_VALUE_EXPECTED
108
            ),
109
            $this->abstractWriter->generateFQDN($this->dmsDatabase, $this->dmsTable)
110
        );
111
    }
112
    
113
    public function testRead()
114
    {
115
        $expectedFiles = $this->createTestFiles(
116
            [
117
                'FileOne.php',
118
                'FileTwo.php',
119
                'FileThree.php',
120
            ],
121
            \implode('', [
122
                $this->abstractWriterConfig->getDirectory(),
123
                DIRECTORY_SEPARATOR,
124
                $this->dmsDatabase->getPhpName(),
125
                DIRECTORY_SEPARATOR,
126
                $this->abstractWriterConfig->getClassPrefix(),
127
                $this->abstractWriterConfig->getClassSuffix(),
128
            ])
129
        );
130
131
        $files = $this->abstractWriter->read($this->dmsDatabase);
132
133
        $this->assertCount(\count($expectedFiles), $files);
134
        $this->assertEquals($expectedFiles, \array_keys($files));
135
    }
136
137
    public function testReadWhenDirectoryDoesNotExist()
138
    {
139
        $files = $this->abstractWriter->read($this->dmsDatabase);
140
141
        $this->assertCount(0, $files);
142
    }
143
144
    public function tearDown()
145
    {
146
        $this->removeDirectoryRecursive(\implode(
147
            '',
148
            [
149
                JANISBIZ_LIGHT_ORM_ROOT_DIR,
150
                'var',
151
                DIRECTORY_SEPARATOR,
152
                'light-orm',
153
            ]
154
        ));
155
    }
156
}
157