GeneratorTest::testAddWriter()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 13
c 1
b 1
f 0
nc 4
nop 0
dl 0
loc 22
rs 9.8333
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Unit;
4
5
use Janisbiz\LightOrm\Connection\ConnectionInterface;
6
use Janisbiz\LightOrm\Dms\MySQL\Generator\DmsFactory;
7
use Janisbiz\LightOrm\Generator;
8
use Janisbiz\LightOrm\Generator\Dms\DmsDatabaseInterface;
9
use Janisbiz\LightOrm\Generator\Dms\DmsTableInterface;
10
use Janisbiz\LightOrm\Generator\Writer\WriterInterface;
11
use Janisbiz\LightOrm\Tests\Unit\Generator\Writer\AbstractWriterConfigTest;
12
use Janisbiz\LightOrm\Tests\Unit\Generator\Writer\FileTrait;
13
use PHPUnit\Framework\TestCase;
14
15
class GeneratorTest extends TestCase
16
{
17
    use FileTrait;
18
19
    /**
20
     * @var WriterInterface|\PHPUnit_Framework_MockObject_MockObject
21
     */
22
    private $writer;
23
24
    /**
25
     * @var DmsTableInterface|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    private $dmsTable;
28
29
    /**
30
     * @var DmsDatabaseInterface|\PHPUnit_Framework_MockObject_MockObject
31
     */
32
    private $dmsDatabase;
33
34
    /**
35
     * @var DmsFactory|\PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private $dmsFactory;
38
39
    /**
40
     * @var Generator
41
     */
42
    private $generator;
43
44
    public function setUp()
45
    {
46
        $this->writer = $this->createMock(WriterInterface::class);
47
48
        $this->dmsTable = $this->createMock(DmsTableInterface::class);
49
50
        $this->dmsDatabase = $this->createMock(DmsDatabaseInterface::class);
51
        $this->dmsDatabase->method('getDmsTables')->willReturn([
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

51
        $this->dmsDatabase->/** @scrutinizer ignore-call */ 
52
                            method('getDmsTables')->willReturn([

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...
52
            $this->dmsTable,
53
            $this->dmsTable,
54
            $this->dmsTable,
55
        ]);
56
57
        $this->dmsFactory = $this->createMock(DmsFactory::class);
58
        $this->dmsFactory->method('createDmsDatabase')->willReturn($this->dmsDatabase);
59
60
        $this->generator = new Generator($this->dmsFactory);
61
    }
62
63
    public function testConstruct()
64
    {
65
        $dmsFactoryProperty = new \ReflectionProperty($this->generator, 'dmsFactory');
66
        $dmsFactoryProperty->setAccessible(true);
67
68
        $this->assertTrue($dmsFactoryProperty->getValue($this->generator) instanceof $this->dmsFactory);
69
    }
70
71
    public function testAddWriter()
72
    {
73
        $writersToSet = [
74
            $this->writer,
75
            $this->writer,
76
            $this->writer,
77
        ];
78
79
        foreach ($writersToSet as $writerToSet) {
80
            $this->generator->addWriter($writerToSet);
81
        }
82
83
        $writersProperty = new \ReflectionProperty($this->generator, 'writers');
84
        $writersProperty->setAccessible(true);
85
86
        $writers = $writersProperty->getValue($this->generator);
87
88
        $this->assertCount(1, $writers);
89
90
        foreach ($writers as $writerClass => $writer) {
91
            $this->assertEquals(\get_class($this->writer), $writerClass);
92
            $this->assertTrue($writer instanceof $this->writer);
93
        }
94
    }
95
96
    public function testGenerate()
97
    {
98
        $this->testAddWriter();
99
100
        $testFiles = \array_flip($this->createTestFiles(
101
            [
102
                'FileOne.php',
103
                'FileTwo.php',
104
                'FileThree.php',
105
            ],
106
            AbstractWriterConfigTest::DIRECTORY_VALUE
107
        ));
108
109
        \array_walk($testFiles, function (&$i, $path) {
110
            $i = \basename($path);
111
        });
112
113
        $this->writer->expects($this->once())->method('read')->willReturn($testFiles);
114
        $this->writer->expects($this->exactly(\count($this->dmsDatabase->getDmsTables())))->method('write');
115
116
        /** @var ConnectionInterface|\PHPUnit_Framework_MockObject_MockObject $connection */
117
        $connection = $this->createMock(ConnectionInterface::class);
118
119
        $this->generator->generate($connection, 'database_name');
120
121
        foreach (\array_keys($testFiles) as $testFilePath) {
122
            $this->assertFalse(\file_exists($testFilePath));
123
        }
124
    }
125
126
    public function tearDown()
127
    {
128
        $this->removeDirectoryRecursive(\implode(
129
            '',
130
            [
131
                JANISBIZ_LIGHT_ORM_ROOT_DIR,
132
                'var',
133
                DIRECTORY_SEPARATOR,
134
                'light-orm',
135
            ]
136
        ));
137
    }
138
}
139