1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taisiya\PropelBundle\Database; |
4
|
|
|
|
5
|
|
|
use Taisiya\PropelBundle\Database\Exception\InvalidArgumentException; |
6
|
|
|
use Taisiya\PropelBundle\Database\TestDatabase\FirstTestTable; |
7
|
|
|
use Taisiya\PropelBundle\Database\TestDatabase\SecondTestTable; |
8
|
|
|
use Taisiya\PropelBundle\Database\TestDatabase\TestDatabase; |
9
|
|
|
use Taisiya\PropelBundle\PHPUnitTestCase; |
10
|
|
|
use Taisiya\PropelBundle\XMLAssertsTrait; |
11
|
|
|
|
12
|
|
|
class SchemaTest extends PHPUnitTestCase |
13
|
|
|
{ |
14
|
|
|
use XMLAssertsTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers Schema::createDatabase |
18
|
|
|
* @covers Schema::getDatabases |
19
|
|
|
* @covers Schema::getDatabase |
20
|
|
|
* @covers Schema::hasDatabase |
21
|
|
|
* @covers Schema::removeDatabase |
22
|
|
|
* @covers Schema::createDatabaseIfNotExists |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
public function testCreateDatabase() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$schema = new Schema(); |
27
|
|
|
$this->assertCount(0, $schema->getDatabases()); |
28
|
|
|
|
29
|
|
|
$database = new TestDatabase(); |
30
|
|
|
|
31
|
|
|
for ($i = 0; $i < 2; $i++) { |
32
|
|
|
try { |
33
|
|
|
$schema->createDatabase($database); |
34
|
|
|
} catch (InvalidArgumentException $e) { |
35
|
|
|
$this->assertGreaterThan(0, $i); |
36
|
|
|
} |
37
|
|
|
$this->assertCount(1, $schema->getDatabases()); |
38
|
|
|
$this->assertEquals($database, $schema->getDatabases()[TestDatabase::getName()]); |
39
|
|
|
$this->assertEquals($database, $schema->getDatabase(TestDatabase::getName())); |
40
|
|
|
$this->assertTrue($schema->hasDatabase(TestDatabase::getName())); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
for ($i = 0; $i < 2; $i++) { |
44
|
|
|
try { |
45
|
|
|
$schema->removeDatabase($database); |
46
|
|
|
} catch (InvalidArgumentException $e) { |
47
|
|
|
$this->assertGreaterThan(0, $i); |
48
|
|
|
} |
49
|
|
|
$this->assertCount(0, $schema->getDatabases()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
for ($i = 0; $i < 2; $i++) { |
53
|
|
|
$schema->createDatabaseIfNotExists($database); |
54
|
|
|
$this->assertCount(1, $schema->getDatabases()); |
55
|
|
|
$this->assertTrue($schema->hasDatabase(TestDatabase::getName())); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @covers Schema::generateOutputXml |
61
|
|
|
* @covers Database::appendToXmlDocument |
62
|
|
|
* @covers Table::appendToXmlDocument |
63
|
|
|
* @covers Column::appendToXmlDocument |
64
|
|
|
* @covers Index::appendToXmlDocument |
65
|
|
|
* @covers IndexColumn::appendToXmlDocument |
66
|
|
|
*/ |
67
|
|
|
public function testGenerateOutputXml() |
68
|
|
|
{ |
69
|
|
|
$schema = new Schema(); |
70
|
|
|
|
71
|
|
|
$database = $schema->createDatabaseIfNotExists(new TestDatabase()) |
72
|
|
|
->getDatabase(TestDatabase::getName()); |
73
|
|
|
|
74
|
|
|
$firstTable = $database->createTableIfNotExists(new FirstTestTable()) |
75
|
|
|
->getTable(FirstTestTable::getName()) |
76
|
|
|
->createColumnIfNotExists(new FirstTestTable\IdColumn()) |
77
|
|
|
->createColumnIfNotExists(new FirstTestTable\SecondColumn()) |
78
|
|
|
->createColumnIfNotExists(new FirstTestTable\ThirdColumn()); |
79
|
|
|
|
80
|
|
|
$firstTableIndex = $firstTable->addIndex(new FirstTestTable\TestIndex()) |
|
|
|
|
81
|
|
|
->getIndex(FirstTestTable\TestIndex::getName()) |
82
|
|
|
->addColumnIfNotExists(new FirstTestTable\IdColumn()) |
83
|
|
|
->addColumnIfNotExists(new FirstTestTable\SecondColumn(), 32); |
84
|
|
|
|
85
|
|
|
$firstTableUniqueIndex = $firstTable->addUnique(new FirstTestTable\TestUniqueIndex()) |
|
|
|
|
86
|
|
|
->getUnique(FirstTestTable\TestUniqueIndex::getName()) |
87
|
|
|
->addColumnIfNotExists(new FirstTestTable\IdColumn(), 1) |
88
|
|
|
->addColumnIfNotExists(new FirstTestTable\SecondColumn(), 1); |
89
|
|
|
|
90
|
|
|
$secondTable = $database->createTableIfNotExists(new SecondTestTable()) |
|
|
|
|
91
|
|
|
->getTable(SecondTestTable::getName()); |
92
|
|
|
|
93
|
|
|
$xml = $schema->generateOutputXml(); |
94
|
|
|
$this->assertXmlHasProlog($xml); |
95
|
|
|
|
96
|
|
|
$this->assertXmlHasElements($xml, '/database', 1); |
97
|
|
|
$this->assertXmlHasElements($xml, '/database/table', 2); |
98
|
|
|
$this->assertXmlHasElements($xml, '/database/table[@name="first"]/column', 3); |
99
|
|
|
$this->assertXmlHasElements($xml, '/database/table[@name="first"]/index', 1); |
100
|
|
|
$this->assertXmlHasElements($xml, '/database/table[@name="first"]/index/index-column', 2); |
101
|
|
|
$this->assertXmlHasElements($xml, '/database/table[@name="first"]/unique', 1); |
102
|
|
|
$this->assertXmlHasElements($xml, '/database/table[@name="first"]/unique/unique-column', 2); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.