Completed
Push — master ( 4025c4...856f8c )
by Nikita
02:41
created

SchemaTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 36.56 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 11
dl 34
loc 93
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testCreateDatabase() 34 34 6
B testGenerateOutputXml() 0 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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())
0 ignored issues
show
Unused Code introduced by
$firstTableIndex is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
            ->getIndex(FirstTestTable\TestIndex::getName())
82
            ->addColumnIfNotExists(new FirstTestTable\IdColumn())
83
            ->addColumnIfNotExists(new FirstTestTable\SecondColumn(), 32);
84
85
        $firstTableUniqueIndex = $firstTable->addUnique(new FirstTestTable\TestUniqueIndex())
0 ignored issues
show
Unused Code introduced by
$firstTableUniqueIndex is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
            ->getUnique(FirstTestTable\TestUniqueIndex::getName())
87
            ->addColumnIfNotExists(new FirstTestTable\IdColumn(), 1)
88
            ->addColumnIfNotExists(new FirstTestTable\SecondColumn(), 1);
89
90
        $secondTable = $database->createTableIfNotExists(new SecondTestTable())
0 ignored issues
show
Unused Code introduced by
$secondTable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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