Completed
Push — master ( ec3686...b09171 )
by Jacob
06:23
created

testConstructSetsConnections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
use Aura\Sql\ConnectionLocator;
4
use Aura\Sql\ExtendedPdo;
5
use Jacobemerick\Web\Domain\Blog\MysqlPostRepository;
6
7
class MysqlPostRepositoryTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
10
    protected $connections;
11
12
    public function __construct()
13
    {
14
        $extendedPdo = $this->newExtendedPdo();
15
        $this->connections = new ConnectionLocator(function () use ($extendedPdo) {
16
            return $extendedPdo;
17
        });
18
    }
19
20
    protected function newExtendedPdo()
21
    {
22
        $extendedPdo = new ExtendedPdo('sqlite::memory:');
23
        $extendedPdo->exec('ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`');
24
        $extendedPdo->exec("
25
            CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` (
26
              `id` integer PRIMARY KEY AUTOINCREMENT,
27
              `title` varchar(60) NOT NULL,
28
              `path` varchar(60) NOT NULL,
29
              `category` varchar(20) NOT NULL,
30
              `date` date NOT NULL,
31
              `body` text NOT NULL,
32
              `display` integer NOT NULL
33
            )"
34
        );
35
        return $extendedPdo;
36
    }
37
38
    public function newMysqlPostRepository()
39
    {
40
        return new MysqlPostRepository($this->connections);
41
    }
42
43
    public function testConstructSetsConnections()
44
    {
45
        $this->assertAttributeEquals(
46
            $this->connections,
47
            'connections',
48
            $this->newMysqlPostRepository()
49
        );
50
    }
51
52
    public function testFindPostByPath()
53
    {
54
        $test_active_post = array(
55
            'title'     => 'test findByPath active',
56
            'path'      => 'test-findbypath-active',
57
            'category'  => 'test-category',
58
            'date'      => (new DateTime())->format('Y-m-d H:i:s'),
59
            'body'      => 'test content',
60
            'display'   => 1
61
        );
62
63
        $this->connections->getDefault()->perform("
64
            INSERT INTO jpemeric_blog.post
65
                (title, path, category, date, body, display)
66
            VALUES
67
                (:title, :path, :category, :date, :body, :display)",
68
            $test_active_post);
69
70
        $active_post = $this->newMysqlPostRepository()->findPostByPath(
71
            $test_active_post['category'],
72
            $test_active_post['path']
73
        );
74
        $this->assertSame($test_active_post['title'], $active_post['title']);
75
76
        $test_inactive_post = array(
77
            'title'     => 'test findByPath inactive',
78
            'path'      => 'test-findbypath-inactive',
79
            'category'  => 'test-category',
80
            'date'      => (new DateTime())->format('Y-m-d H:i:s'),
81
            'body'      => 'test content',
82
            'display'   => 0
83
        );
84
85
        $this->connections->getDefault()->perform("
86
            INSERT INTO jpemeric_blog.post
87
                (title, path, category, date, body, display)
88
            VALUES
89
                (:title, :path, :category, :date, :body, :display)",
90
            $test_inactive_post);
91
92
        $inactive_post = $this->newMysqlPostRepository()->findPostByPath(
93
            $test_inactive_post['category'],
94
            $test_inactive_post['path']
95
        );
96
        $this->assertFalse($inactive_post);
97
98
        $nonexistant_post = $this->newMysqlPostRepository()->findPostByPath(
99
            'test-category',
100
            'test-findbypath-nonexistant'
101
        );
102
        $this->assertFalse($nonexistant_post);
103
   }
104
105
    public function testIsInstanceOfPostRepository()
106
    {
107
        $this->assertInstanceOf(
108
            'Jacobemerick\Web\Domain\Blog\PostRepository',
109
            $this->newMysqlPostRepository()
110
        );
111
    }
112
113
    public static function tearDownAfterClass()
114
    {
115
//        $this->connections->getDefault()->disconnect();
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
116
        unlink('jpemeric_blog.db');
117
    }
118
}
119