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

testGetIncomingForeignKeys3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 13
rs 9.9332
cc 1
nc 1
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
    public function testSchemaLock(): void
43
    {
44
        $schemaFromConnec = self::getConnection()->getSchemaManager()->createSchema();
45
        ImmutableCaster::castSchemaToImmutable($schemaFromConnec);
46
47
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
48
        $cache = new ArrayCache();
49
        $tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
50
51
        $schemaFromAnalyser = $tdbmSchemaAnalyzer1->getSchema(true);
52
        $schemaFromAnalyserCached = $tdbmSchemaAnalyzer1->getSchema();
53
        $this->assertEquals($schemaFromConnec->getTableNames(), $schemaFromAnalyser->getTableNames());
54
        $this->assertEquals($schemaFromAnalyser->getTableNames(), $schemaFromAnalyserCached->getTableNames());
55
    }
56
57
    public function testGetSchema(): void
58
    {
59
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
60
        $cache = new ArrayCache();
61
        $tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
62
        $tdbmSchemaAnalyzer2 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
63
64
        // Why don't we go in all lines of code????
65
        $schema1 = $tdbmSchemaAnalyzer1->getSchema();
66
        // One more time to go through cache!
67
        $schema2 = $tdbmSchemaAnalyzer2->getSchema();
68
        $this->assertTrue($schema1 === $schema2);
69
    }
70
71
    public function testGetIncomingForeignKeys(): void
72
    {
73
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
74
        $cache = new ArrayCache();
75
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
76
77
        $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('users');
78
        $this->assertCount(1, $fks);
79
    }
80
81
    public function testGetIncomingForeignKeys2(): void
82
    {
83
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
84
        $cache = new ArrayCache();
85
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
86
87
        $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('contact');
88
        $this->assertCount(1, $fks);
89
    }
90
91
    public function testGetIncomingForeignKeys3(): void
92
    {
93
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
94
        $cache = new ArrayCache();
95
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
96
97
        $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('country');
98
        $this->assertCount(5, $fks);
99
        $tables = [$fks[0]->getLocalTableName(), $fks[1]->getLocalTableName(), $fks[2]->getLocalTableName(), $fks[3]->getLocalTableName(), $fks[4]->getLocalTableName()];
100
        $this->assertContains('users', $tables);
101
        $this->assertContains('all_nullable', $tables);
102
        $this->assertContains('boats', $tables);
103
        $this->assertContains('states', $tables);
104
    }
105
106
    public function testGetPivotTableLinkedToTable(): void
107
    {
108
        $schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
109
        $cache = new ArrayCache();
110
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
111
112
        $pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('rights');
113
        $this->assertCount(1, $pivotTables);
114
        $this->assertEquals('roles_rights', $pivotTables[0]);
115
116
        $pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('animal');
117
        $this->assertCount(0, $pivotTables);
118
119
        $pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('animal');
120
        $this->assertCount(0, $pivotTables);
121
    }
122
123
    /*public function testGetCompulsoryColumnsWithNoInheritance() {
124
        $table = $this->tdbmSchemaAnalyzer->getSchema()->getTable('country');
125
        $compulsoryColumns = $this->tdbmSchemaAnalyzer->getCompulsoryProperties($table);
126
        $this->assertCount(1, $compulsoryColumns);
127
        $this->assertArrayHasKey("label", $compulsoryColumns);
128
    }
129
130
    public function testGetCompulsoryColumnsWithInheritance() {
131
        $table = $this->tdbmSchemaAnalyzer->getSchema()->getTable('users');
132
        $compulsoryColumns = $this->tdbmSchemaAnalyzer->getCompulsoryProperties($table);
133
        $this->assertCount(5, $compulsoryColumns);
134
        $this->assertEquals(['name', 'created_at', 'email', 'country_id', 'login'], array_keys($compulsoryColumns));
135
    }*/
136
}
137