Completed
Push — master ( 89e88a...8c8014 )
by Jacob
03:15
created

MysqlSeriesRepositoryTest::insertSeriesPostData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 18
loc 18
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Jacobemerick\Web\Domain\Blog\Series;
4
5
use Aura\Sql\ConnectionLocator;
6
use Aura\Sql\ExtendedPdo;
7
use PHPUnit_Framework_TestCase;
8
9
class MysqlSeriesRepositoryTest extends PHPUnit_Framework_TestCase
10
{
11
12
    protected static $connection;
13
14
    public static function setUpBeforeClass()
15
    {
16
        $extendedPdo = new ExtendedPdo('sqlite::memory:');
17
        $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`");
18
19
        $extendedPdo->exec("
20
            CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` (
21
                `id` integer PRIMARY KEY AUTOINCREMENT,
22
                `title` varchar(60) NOT NULL,
23
                `path` varchar(60) NOT NULL,
24
                `category` varchar(15) NOT NULL,
25
                `date` datetime,
26
                `body` text,
27
                `display` integer(1) NOT NULL
28
            )"
29
        );
30
        $extendedPdo->exec("
31
            CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series` (
32
                `id` integer PRIMARY KEY AUTOINCREMENT,
33
                `title` text NOT NULL,
34
                `description` text NOT NULL
35
            )"
36
        );
37
        $extendedPdo->exec("
38
            CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series_post` (
39
                `series` integer NOT NULL,
40
                `post` integer NOT NULL,
41
                `order` integer(1) NOT NULL
42
            )"
43
        );
44
45
        self::$connection = new ConnectionLocator(function () use ($extendedPdo) {
46
            return $extendedPdo;
47
        });
48
    }
49
50
    public function testIsInstanceOfSeriesRepository()
51
    {
52
        $repository = new MysqlSeriesRepository(self::$connection);
53
54
        $this->assertInstanceOf(
55
            'Jacobemerick\Web\Domain\Blog\Series\MysqlSeriesRepository',
56
            $repository
57
        );
58
    }
59
60
    public function testImplementsSeriesInterface()
61
    {
62
        $repository = new MysqlSeriesRepository(self::$connection);
63
64
        $this->assertInstanceOf(
65
            'Jacobemerick\Web\Domain\Blog\Series\SeriesRepositoryInterface',
66
            $repository
67
        );
68
    }
69
70
    public function testConstructSetsConnections()
71
    {
72
        $respository = new MysqlSeriesRepository(self::$connection);
73
74
        $this->assertAttributeSame(
75
            self::$connection,
76
            'connections',
77
            $respository
78
        );
79
    }
80
81
    public function testGetSeriesForPost()
82
    {
83
        $testPostData = [
84
            [
85
                'id' => rand(1, 100),
86
                'title' => 'test one',
87
                'category' => 'test category',
88
                'path' => 'test-one',
89
                'display' => 1,
90
            ],
91
            [
92
                'id' => rand(101, 200),
93
                'title' => 'test two',
94
                'category' => 'test category',
95
                'path' => 'test-two',
96
                'display' => 1,
97
            ],
98
        ];
99
100
        $testSeriesData = [
101
            [
102
                'id' => rand(1, 100),
103
                'title' => 'test one',
104
                'description' => 'test description',
105
            ],
106
        ];
107
108
        $testSeriesPostData = [
109
            [
110
                'series' => reset($testSeriesData)['id'],
111
                'post' => $testPostData[0]['id'],
112
                'order' => 1,
113
            ],
114
            [
115
                'series' => reset($testSeriesData)['id'],
116
                'post' => $testPostData[1]['id'],
117
                'order' => 2,
118
            ],
119
        ];
120
121
        array_walk($testPostData, [$this, 'insertPostData']);
122
        array_walk($testSeriesData, [$this, 'insertSeriesData']);
123
        array_walk($testSeriesPostData, [$this, 'insertSeriesPostData']);
124
125
        $repository = new MysqlSeriesRepository(self::$connection);
126
        $data = $repository->getSeriesForPost(reset($testPostData)['id']);
127
128
        $this->assertNotFalse($data);
129
        $this->assertInternalType('array', $data);
130
        foreach ($testPostData as $key => $testPostRow) {
131
            $this->assertInternalType('array', $data[$key]);
132
            $this->assertArrayHasKey('series_title', $data[$key]);
133
            $this->assertEquals(reset($testSeriesData)['title'], $data[$key]['series_title']);
134
            $this->assertArrayHasKey('series_description', $data[$key]);
135
            $this->assertEquals(reset($testSeriesData)['description'], $data[$key]['series_description']);
136
            $this->assertArrayHasKey('post', $data[$key]);
137
            $this->assertEquals($testPostRow['id'], $data[$key]['post']);
138
            $this->assertArrayHasKey('title', $data[$key]);
139
            $this->assertEquals($testPostRow['title'], $data[$key]['title']);
140
            $this->assertArrayHasKey('category', $data[$key]);
141
            $this->assertEquals($testPostRow['category'], $data[$key]['category']);
142
            $this->assertArrayHasKey('path', $data[$key]);
143
            $this->assertEquals($testPostRow['path'], $data[$key]['path']);
144
        }
145
    }
146
147
    public function testGetSeriesForPostOrder() {}
148
149
    public function testGetSeriesForPostSeriesFilter() {}
150
151
    public function testGetSeriesForPostInactive() {}
152
153
    public function testGetSeriesForPostFailure() {}
154
155
    protected function tearDown()
156
    {
157
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_blog`.`series`");
158
    }
159
160
    public static function tearDownAfterClass()
161
    {
162
        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...
163
        unlink('jpemeric_blog.db');
164
    }
165
166 View Code Duplication
    protected function insertPostData(array $data)
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...
167
    {
168
        $defaultData = [
169
            'id' => null,
170
            'title' => '',
171
            'path' => '',
172
            'category' => '',
173
            'date' => '',
174
            'body' => '',
175
            'display' => 0,
176
        ];
177
178
        $data = array_merge($defaultData, $data);
179
180
        return self::$connection->getDefault()->perform("
181
            INSERT INTO `jpemeric_blog`.`post`
182
                (id, title, path, category, date, body, display)
183
            VALUES
184
                (:id, :title, :path, :category, :date, :body, :display)",
185
            $data
186
        );
187
    }
188
189 View Code Duplication
    protected function insertSeriesData(array $data)
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...
190
    {
191
        $defaultData = [
192
            'id' => null,
193
            'title' => '',
194
            'description' => '',
195
        ];
196
197
        $data = array_merge($defaultData, $data);
198
199
        return self::$connection->getDefault()->perform("
200
            INSERT INTO `jpemeric_blog`.`series`
201
                (id, title, description)
202
            VALUES
203
                (:id, :title, :description)",
204
            $data
205
        );
206
    }
207
208 View Code Duplication
    protected function insertSeriesPostData(array $data)
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...
209
    {
210
        $defaultData = [
211
            'series' => '',
212
            'post' => '',
213
            'order' => 0,
214
        ];
215
216
        $data = array_merge($defaultData, $data);
217
218
        return self::$connection->getDefault()->perform("
219
            INSERT INTO `jpemeric_blog`.`series_post`
220
                (series, post, `order`)
221
            VALUES
222
                (:series, :post, :order)",
223
            $data
224
        );
225
    }
226
}
227