1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jacobemerick\Web\Domain\Blog; |
4
|
|
|
|
5
|
|
|
use Aura\Sql\ConnectionLocator; |
6
|
|
|
use Aura\Sql\ExtendedPdo; |
7
|
|
|
|
8
|
|
|
class MysqlPostRepositoryTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $connections; |
12
|
|
|
|
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$extendedPdo = $this->newExtendedPdo(); |
16
|
|
|
$this->connections = new ConnectionLocator(function () use ($extendedPdo) { |
17
|
|
|
return $extendedPdo; |
18
|
|
|
}); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function newExtendedPdo() |
22
|
|
|
{ |
23
|
|
|
$extendedPdo = new ExtendedPdo('sqlite::memory:'); |
24
|
|
|
$extendedPdo->exec('ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`'); |
25
|
|
|
$extendedPdo->exec(" |
26
|
|
|
CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` ( |
27
|
|
|
`id` integer PRIMARY KEY AUTOINCREMENT, |
28
|
|
|
`title` varchar(60) NOT NULL, |
29
|
|
|
`path` varchar(60) NOT NULL, |
30
|
|
|
`category` varchar(20) NOT NULL, |
31
|
|
|
`date` date NOT NULL, |
32
|
|
|
`body` text NOT NULL, |
33
|
|
|
`display` integer NOT NULL |
34
|
|
|
)" |
35
|
|
|
); |
36
|
|
|
return $extendedPdo; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function newMysqlPostRepository() |
40
|
|
|
{ |
41
|
|
|
return new MysqlPostRepository($this->connections); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testFindByUri() |
45
|
|
|
{ |
46
|
|
|
$test_post = [ |
47
|
|
|
'title' => 'test title', |
48
|
|
|
'path' => 'test-uri', |
49
|
|
|
'category' => 'test', |
50
|
|
|
'date' => date('Y-m-d H:i:s'), |
51
|
|
|
'body' => 'test content', |
52
|
|
|
'display' => 1, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$this->connections->getDefault()->perform(" |
56
|
|
|
INSERT INTO jpemeric_blog.post |
57
|
|
|
(title, path, category, date, body, display) |
58
|
|
|
VALUES |
59
|
|
|
(:title, :path, :category, :date, :body, :display)", |
60
|
|
|
$test_post); |
61
|
|
|
|
62
|
|
|
$post = $this->newMysqlPostRepository()->findByUri($test_post['path']); |
63
|
|
|
$this->assertSame($test_post['path'], $post['path']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testIsInstanceOfPostRepository() |
67
|
|
|
{ |
68
|
|
|
$this->assertInstanceOf( |
69
|
|
|
'Jacobemerick\Web\Domain\Blog\PostRepository', |
70
|
|
|
$this->newMysqlPostRepository() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function tearDownAfterClass() |
75
|
|
|
{ |
76
|
|
|
// $this->connections->getDefault()->disconnect(); |
|
|
|
|
77
|
|
|
unlink('jpemeric_blog.db'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.