SyncDbSchemaTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 23.28 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 27
loc 116
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 9 2
A testConstruct() 0 17 1
A testConstruct2() 0 8 1
A testExecuteFirstSql() 0 16 1
A testExecuteNoUnDoneSql() 0 5 1
A testExecuteErrorSql() 13 13 1
A testExecuteSmallId() 14 14 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
namespace FwlibTest\Db;
3
4
use Fwlib\Db\SyncDbSchema;
5
use Fwlib\Test\AbstractDbRelateTest;
6
7
/**
8
 * @copyright   Copyright 2013-2015 Fwolf
9
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
10
 */
11
class SyncDbSchemaTest extends AbstractDbRelateTest
12
{
13
    protected static $dbUsing = 'default';
14
    private static $logTable = 'log_sync_db_schema';
15
16
    /** @var SyncDbSchema */
17
    private static $sds = null;
18
19
20
    public static function setUpBeforeClass()
21
    {
22
        parent::setUpBeforeClass();
23
24
        // Delete log table if exists
25
        if (self::$db->isTableExist(self::$logTable)) {
26
            self::$db->execute('DROP TABLE ' . self::$logTable);
27
        }
28
    }
29
30
31
    public function testConstruct()
32
    {
33
        $this->assertFalse(self::$db->isTableExist(self::$logTable));
34
35
        $this->expectOutputRegex(
36
            '/Log table \w+ does not exists, create it, done\./'
37
        );
38
39
        self::$sds = (new SyncDbSchema)
40
            ->setLogTable(self::$logTable)
41
            ->setDb($this->getServiceContainer()->getDb());
42
43
        $this->assertEquals(self::$logTable, self::$sds->getLogTable());
44
        $this->assertTrue(self::$db->isTableExist(self::$logTable));
45
46
        self::$sds->charsetPhp = 'UTF-8';
0 ignored issues
show
Bug introduced by
The property charsetPhp does not seem to exist in Fwlib\Db\SyncDbSchema.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
    }
48
49
50
    public function testConstruct2()
51
    {
52
        $this->expectOutputRegex('/Log table \w+ already exists\./');
53
        $sds = (new SyncDbSchema)
54
            ->setLogTable(self::$logTable)
55
            ->setDb($this->getServiceContainer()->getDb());
56
        unset($sds);
57
    }
58
59
60
    /**
61
     * 1
62
     */
63
    public function testExecuteFirstSql()
64
    {
65
        $arColumn = self::$db->getMetaColumn(self::$tableUser);
66
        $this->assertFalse(isset($arColumn['temp1']));
67
68
        $this->expectOutputRegex('/Total \d+ SQL executed successful./');
69
        self::$sds->setSql(
70
            42,
71
            'ALTER TABLE ' . self::$tableUser . '
72
                ADD COLUMN temp1 INT NOT NULL DEFAULT 0'
73
        );
74
        self::$sds->execute();
75
76
        $arColumn = self::$db->getMetaColumn(self::$tableUser, true);
77
        $this->assertTrue(isset($arColumn['temp1']));
78
    }
79
80
81
    /**
82
     * 2 No more SQL
83
     */
84
    public function testExecuteNoUnDoneSql()
85
    {
86
        $this->expectOutputRegex('/No un-done SQL to do./');
87
        self::$sds->execute();
88
    }
89
90
91
    /**
92
     * 3 Add an error SQL
93
     */
94 View Code Duplication
    public function testExecuteErrorSql()
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...
95
    {
96
        $this->expectOutputRegex('/Execute abort./');
97
        self::$sds->setSql(
98
            43,
99
            'ALTER TABLE ' . self::$tableUser . '
100
                ADD COLUMN temp1 INT NOT NULL DEFAULT 0'
101
        );
102
        self::$sds->execute();
103
104
        $this->assertEquals(43, self::$sds->lastId);
105
        $this->assertEquals(42, self::$sds->lastIdDone);
106
    }
107
108
109
    /**
110
     * 4 Add SQL with smaller id, will not execute
111
     */
112 View Code Duplication
    public function testExecuteSmallId()
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...
113
    {
114
        $this->expectOutputRegex('/No un-done SQL to do./');
115
        self::$sds->setSql(
116
            22,
117
            'ALTER TABLE ' . self::$tableUser . '
118
                ADD COLUMN temp2 INT NOT NULL DEFAULT 0'
119
        );
120
        self::$sds->execute();
121
122
        // Error SQL 43 is cleared by execute()
123
        $this->assertEquals(42, self::$sds->getLastId());
124
        $this->assertEquals(42, self::$sds->getLastIdDone());
125
    }
126
}
127