Passed
Pull Request — master (#171)
by ARP
03:17
created

TDBMSchemaAnalyzerTest::testSchemaCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 18
rs 9.8666
cc 2
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM;
23
24
use Doctrine\Common\Cache\ArrayCache;
25
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
26
use TheCodingMachine\TDBM\Utils\ImmutableCaster;
27
28
class TDBMSchemaAnalyzerTest extends TDBMAbstractServiceTest
29
{
30
    /**
31
     * @var TDBMSchemaAnalyzer
32
     */
33
    protected $tdbmSchemaAnalyzer;
34
35
    protected function setUp(): void
36
    {
37
        parent::setUp();
38
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
39
        $this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer);
40
    }
41
42
    //todo better test the interactions bewteen lock file and cache
43
    public function testSchemaCache()
44
    {
45
        $schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
46
        if (file_exists($schemaFilePath)) {
47
            unlink($schemaFilePath);
48
        }
49
        $this->assertFileNotExists($schemaFilePath);
50
51
        $schemaFromConnec = self::getConnection()->getSchemaManager()->createSchema();
52
        ImmutableCaster::castSchemaToImmutable($schemaFromConnec);
53
54
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
55
        $cache = new ArrayCache();
56
        $tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
57
58
        $schemaFromAnalyser = $tdbmSchemaAnalyzer1->getSchema();
59
        $this->assertFileExists($schemaFilePath);
60
        $this->assertEquals($schemaFromConnec, $schemaFromAnalyser);
61
    }
62
63
    public function testGetSchema(): void
64
    {
65
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
66
        $cache = new ArrayCache();
67
        $tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
68
        $tdbmSchemaAnalyzer2 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
69
70
        // Why don't we go in all lines of code????
71
        $schema1 = $tdbmSchemaAnalyzer1->getSchema();
72
        // One more time to go through cache!
73
        $schema2 = $tdbmSchemaAnalyzer2->getSchema();
74
        $this->assertTrue($schema1 === $schema2);
75
    }
76
77
    public function testGetIncomingForeignKeys(): void
78
    {
79
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
80
        $cache = new ArrayCache();
81
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
82
83
        $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('users');
84
        $this->assertCount(1, $fks);
85
    }
86
87
    public function testGetIncomingForeignKeys2(): void
88
    {
89
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
90
        $cache = new ArrayCache();
91
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
92
93
        $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('contact');
94
        $this->assertCount(1, $fks);
95
    }
96
97
    public function testGetIncomingForeignKeys3(): void
98
    {
99
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
100
        $cache = new ArrayCache();
101
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
102
103
        $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('country');
104
        $this->assertCount(5, $fks);
105
        $tables = [$fks[0]->getLocalTableName(), $fks[1]->getLocalTableName(), $fks[2]->getLocalTableName(), $fks[3]->getLocalTableName(), $fks[4]->getLocalTableName()];
106
        $this->assertContains('users', $tables);
107
        $this->assertContains('all_nullable', $tables);
108
        $this->assertContains('boats', $tables);
109
        $this->assertContains('states', $tables);
110
    }
111
112
    public function testGetPivotTableLinkedToTable(): void
113
    {
114
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
115
        $cache = new ArrayCache();
116
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
117
118
        $pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('rights');
119
        $this->assertCount(1, $pivotTables);
120
        $this->assertEquals('roles_rights', $pivotTables[0]);
121
122
        $pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('animal');
123
        $this->assertCount(0, $pivotTables);
124
125
        $pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('animal');
126
        $this->assertCount(0, $pivotTables);
127
    }
128
129
    /*public function testGetCompulsoryColumnsWithNoInheritance() {
130
        $table = $this->tdbmSchemaAnalyzer->getSchema()->getTable('country');
131
        $compulsoryColumns = $this->tdbmSchemaAnalyzer->getCompulsoryProperties($table);
132
        $this->assertCount(1, $compulsoryColumns);
133
        $this->assertArrayHasKey("label", $compulsoryColumns);
134
    }
135
136
    public function testGetCompulsoryColumnsWithInheritance() {
137
        $table = $this->tdbmSchemaAnalyzer->getSchema()->getTable('users');
138
        $compulsoryColumns = $this->tdbmSchemaAnalyzer->getCompulsoryProperties($table);
139
        $this->assertCount(5, $compulsoryColumns);
140
        $this->assertEquals(['name', 'created_at', 'email', 'country_id', 'login'], array_keys($compulsoryColumns));
141
    }*/
142
}
143