Completed
Push — master ( 4c6be8...89e88a )
by Jacob
03:08
created

MysqlChangelogRepositoryTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 189
Duplicated Lines 40.21 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 13
c 3
b 0
f 3
lcom 1
cbo 5
dl 76
loc 189
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 22 22 1
A testIsInstanceOfChangelogRepository() 0 9 1
A testImplementsChangelogInterface() 0 9 1
A testConstructSetsConnections() 0 10 1
B testGetChanges() 0 30 2
A testGetChangesFailure() 0 8 1
B testGetChangesRange() 33 33 2
A testGetChangesRangeFailure() 21 21 1
A tearDown() 0 4 1
A tearDownAfterClass() 0 5 1
A insertData() 0 22 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 Jacobemerick\Web\Domain\Stream\Changelog;
4
5
use Aura\Sql\ConnectionLocator;
6
use Aura\Sql\ExtendedPdo;
7
use PHPUnit_Framework_TestCase;
8
9
class MysqlChangelogRepositoryTest extends PHPUnit_Framework_TestCase
10
{
11
12
    protected static $connection;
13
14 View Code Duplication
    public static function setUpBeforeClass()
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...
15
    {
16
        $extendedPdo = new ExtendedPdo('sqlite::memory:');
17
        $extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`");
18
        $extendedPdo->exec("
19
            CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`changelog` (
20
              `id` integer PRIMARY KEY AUTOINCREMENT,
21
              `hash` char(40) NOT NULL,
22
              `message` text,
23
              `message_short` varchar(100),
24
              `datetime` datetime NOT NULL,
25
              `author` varchar(50) NOT NULL,
26
              `commit_link` varchar(100) NOT NULL,
27
              `created_at` datetime,
28
              `updated_at` datetime
29
            )"
30
        );
31
32
        self::$connection = new ConnectionLocator(function () use ($extendedPdo) {
33
            return $extendedPdo;
34
        });
35
    }
36
37
    public function testIsInstanceOfChangelogRepository()
38
    {
39
        $repository = new MysqlChangelogRepository(self::$connection);
40
41
        $this->assertInstanceOf(
42
            'Jacobemerick\Web\Domain\Stream\Changelog\MysqlChangelogRepository',
43
            $repository
44
        );
45
    }
46
47
    public function testImplementsChangelogInterface()
48
    {
49
        $repository = new MysqlChangelogRepository(self::$connection);
50
51
        $this->assertInstanceOf(
52
            'Jacobemerick\Web\Domain\Stream\Changelog\ChangelogRepositoryInterface',
53
            $repository
54
        );
55
    }
56
57
    public function testConstructSetsConnections()
58
    {
59
        $respository = new MysqlChangelogRepository(self::$connection);
60
61
        $this->assertAttributeSame(
62
            self::$connection,
63
            'connections',
64
            $respository
65
        );
66
    }
67
68
    public function testGetChanges()
69
    {
70
        $testData = [
71
            [
72
                'id' => rand(1, 100),
73
                'message' => 'test one',
74
            ],
75
            [
76
                'id' => rand(101, 200),
77
                'message' => 'test two',
78
            ],
79
        ];
80
81
        array_walk($testData, [$this, 'insertData']);
82
83
        $repository = new MysqlChangelogRepository(self::$connection);
84
        $data = $repository->getChanges();
85
86
        $this->assertNotFalse($data);
87
        $this->assertInternalType('array', $data);
88
        foreach ($testData as $key => $testRow) {
89
            $this->assertInternalType('array', $testRow);
90
            $this->assertArraySubset($testRow, $data[$key]);
91
            $this->assertArrayHasKey('id', $data[$key]);
92
            $this->assertArrayHasKey('message', $data[$key]);
93
            $this->assertArrayHasKey('message_short', $data[$key]);
94
            $this->assertArrayHasKey('datetime', $data[$key]);
95
            $this->assertArrayHasKey('commit_link', $data[$key]);
96
        }
97
    }
98
99
    public function testGetChangesFailure()
100
    {
101
        $repository = new MysqlChangelogRepository(self::$connection);
102
        $data = $repository->getChanges();
103
104
        $this->assertEmpty($data);
105
        $this->assertInternalType('array', $data);
106
    }
107
108 View Code Duplication
    public function testGetChangesRange()
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...
109
    {
110
        $testData = [
111
            [
112
                'id' => rand(1, 100),
113
                'message' => 'test one',
114
            ],
115
            [
116
                'id' => rand(101, 200),
117
                'message' => 'test two',
118
            ],
119
            [
120
                'id' => rand(201, 300),
121
                'message' => 'test three',
122
            ],
123
        ];
124
125
        array_walk($testData, [$this, 'insertData']);
126
127
        $repository = new MysqlChangelogRepository(self::$connection);
128
        $data = $repository->getChanges(2, 1);
129
130
        $this->assertNotFalse($data);
131
        $this->assertInternalType('array', $data);
132
        $this->assertCount(2, $data);
133
134
        $testData = array_slice($testData, 1, 2);
135
136
        foreach ($testData as $key => $testRow) {
137
            $this->assertInternalType('array', $testRow);
138
            $this->assertArraySubset($testRow, $data[$key]);
139
        }
140
    }
141
142 View Code Duplication
    public function testGetChangesRangeFailure()
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...
143
    {
144
        $testData = [
145
            [
146
                'id' => rand(1, 100),
147
                'message' => 'test one',
148
            ],
149
            [
150
                'id' => rand(101, 200),
151
                'message' => 'test two',
152
            ],
153
        ];
154
155
        array_walk($testData, [$this, 'insertData']);
156
157
        $repository = new MysqlChangelogRepository(self::$connection);
158
        $data = $repository->getChanges(1, 3);
159
160
        $this->assertEmpty($data);
161
        $this->assertInternalType('array', $data);
162
    }
163
164
    protected function tearDown()
165
    {
166
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`changelog`");
167
    }
168
169
    public static function tearDownAfterClass()
170
    {
171
        self::$connection->getDefault()->disconnect();
0 ignored issues
show
Bug introduced by
The method disconnect() does not exist on Aura\Sql\ExtendedPdoInterface. Did you maybe mean connect()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
172
        unlink('jpemeric_stream.db');
173
    }
174
175
    protected function insertData(array $data)
176
    {
177
        $defaultData = [
178
            'id' => null,
179
            'hash' => '',
180
            'message' => null,
181
            'message_short' => null,
182
            'datetime' => '',
183
            'author' => '',
184
            'commit_link' => '',
185
        ];
186
187
        $data = array_merge($defaultData, $data);
188
189
        return self::$connection->getDefault()->perform("
190
            INSERT INTO `jpemeric_stream`.`changelog`
191
                (id, hash, message, message_short, datetime, author, commit_link)
192
            VALUES
193
                (:id, :hash, :message, :message_short, :datetime, :author, :commit_link)",
194
            $data
195
        );
196
    }
197
}
198