Completed
Push — master ( a2526c...4c6be8 )
by Jacob
03:16
created

testGetActivitiesFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Jacobemerick\Web\Domain\Stream\Activity;
4
5
use Aura\Sql\ConnectionLocator;
6
use Aura\Sql\ExtendedPdo;
7
use PHPUnit_Framework_TestCase;
8
9
class MysqlActivityRepositoryTest 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_stream.db` AS `jpemeric_stream`");
18
        $extendedPdo->exec("
19
            CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`activity` (
20
              `id` integer PRIMARY KEY AUTOINCREMENT,
21
              `message` text NOT NULL,
22
              `message_long` text NOT NULL,
23
              `datetime` datetime NOT NULL,
24
              `metadata` text NOT NULL,
25
              `type` varchar(10) NOT NULL,
26
              `type_id` integer 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 testIsInstanceOfActivityRepository()
38
    {
39
        $repository = new MysqlActivityRepository(self::$connection);
40
41
        $this->assertInstanceOf(
42
            'Jacobemerick\Web\Domain\Stream\Activity\MysqlActivityRepository',
43
            $repository
44
        );
45
    }
46
47
    public function testImplementsActivityInterface()
48
    {
49
        $repository = new MysqlActivityRepository(self::$connection);
50
51
        $this->assertInstanceOf(
52
            'Jacobemerick\Web\Domain\Stream\Activity\ActivityRepositoryInterface',
53
            $repository
54
        );
55
    }
56
57
    public function testConstructSetsConnections()
58
    {
59
        $respository = new MysqlActivityRepository(self::$connection);
60
61
        $this->assertAttributeSame(
62
            self::$connection,
63
            'connections',
64
            $respository
65
        );
66
    }
67
68
    public function testGetActivityById()
69
    {
70
        $testData = [
71
            'id' => rand(1, 100),
72
            'message' => 'test data',
73
        ];
74
75
        $this->insertData($testData);
76
77
        $repository = new MysqlActivityRepository(self::$connection);
78
        $data = $repository->getActivityById($testData['id']);
79
80
        $this->assertNotFalse($data);
81
        $this->assertInternalType('array', $data);
82
        $this->assertArraySubset($testData, $data);
83
    }
84
85
    public function testGetActivityByIdFailure()
86
    {
87
        $testData = [
88
            'id' => rand(1, 100),
89
            'message' => 'test data',
90
        ];
91
92
        $this->insertData($testData);
93
94
        $repository = new MysqlActivityRepository(self::$connection);
95
        $data = $repository->getActivityById($testData['id'] + 1);
96
97
        $this->assertFalse($data);
98
    }
99
100
    public function testGetActivities()
101
    {
102
        $testData = [
103
            [
104
                'id' => rand(1, 100),
105
                'message' => 'test one',
106
            ],
107
            [
108
                'id' => rand(101, 200),
109
                'message' => 'test two',
110
            ],
111
        ];
112
113
        $this->insertData($testData[0]);
114
        $this->insertData($testData[1]);
115
116
        $repository = new MysqlActivityRepository(self::$connection);
117
        $data = $repository->getActivities();
118
119
        $this->assertNotFalse($data);
120
        $this->assertInternalType('array', $data);
121
        foreach ($testData as $key => $testRow) {
122
            $this->assertInternalType('array', $testRow);
123
            $this->assertArraySubset($testRow, $data[$key]);
124
        }
125
    }
126
127
    public function testGetActivitiesFailure()
128
    {
129
        $repository = new MysqlActivityRepository(self::$connection);
130
        $data = $repository->getActivities();
131
132
        $this->assertEmpty($data);
133
        $this->assertInternalType('array', $data);
134
    }
135
136
    public function testGetActivitiesRange() {}
137
138
    public function testGetActivitiesRangeFailure() {}
139
140
    public function testGetActivitiesCount()
141
    {
142
        $testData = [
143
            [
144
                'id' => rand(1, 100),
145
                'message' => 'test one',
146
            ],
147
            [
148
                'id' => rand(101, 200),
149
                'message' => 'test two',
150
            ],
151
        ];
152
153
        $this->insertData($testData[0]);
154
        $this->insertData($testData[1]);
155
156
        $repository = new MysqlActivityRepository(self::$connection);
157
        $data = $repository->getActivitiesCount();
158
159
        $this->assertNotFalse($data);
160
        $this->assertStringMatchesFormat('%d', $data);
161
        $this->assertEquals(count($testData), $data);
162
    }
163
164
    public function testGetActivitiesCountEmpty()
165
    {
166
        $repository = new MysqlActivityRepository(self::$connection);
167
        $data = $repository->getActivitiesCount();
168
169
        $this->assertNotFalse($data);
170
        $this->assertStringMatchesFormat('%d', $data);
171
        $this->assertEquals('0', $data);
172
    }
173
174
    public function testGetActivitiesByType() {}
175
176
    public function testGetActivitiesByTypeFailure() {}
177
178
    public function testGetActivitiesByTypeRange() {}
179
180
    public function testGetActivitiesByTypeRangeFailure() {}
181
182
    public function testGetActivitiesByTypeCount() {}
183
184
    public function testGetActivitiesByTypeCountEmpty() {}
185
186
    protected function tearDown()
187
    {
188
        self::$connection->getDefault()->perform("DELETE FROM `jpemeric_stream`.`activity`");
189
    }
190
191
    public static function tearDownAfterClass()
192
    {
193
        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...
194
        unlink('jpemeric_stream.db');
195
    }
196
197
    protected function insertData(array $data)
198
    {
199
        $defaultData = [
200
            'id' => null,
201
            'message' => '',
202
            'message_long' => '',
203
            'datetime' => '',
204
            'metadata' => '',
205
            'type' => '',
206
            'type_id' => '',
207
        ];
208
209
        $data = array_merge($defaultData, $data);
210
211
        return self::$connection->getDefault()->perform("
212
            INSERT INTO `jpemeric_stream`.`activity`
213
                (id, message, message_long, datetime, metadata, type, type_id)
214
            VALUES
215
                (:id, :message, :message_long, :datetime, :metadata, :type, :type_id)",
216
            $data
217
        );
218
    }
219
}
220