Passed
Pull Request — master (#1)
by Peter
07:07
created

testGetCreationDateCreatesADateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Databases\Migrations;
6
7
use AbterPhp\Framework\Filesystem\FileFinder;
8
use DateTime;
9
use Opulence\Databases\Adapters\Pdo\Connection;
10
use Opulence\Databases\Adapters\Pdo\Statement;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13
14
class BaseMigrationsTest extends TestCase
15
{
16
    /** @var BaseMigration - System Under Test */
17
    protected $sut;
18
19
    /** @var Connection|MockObject */
20
    protected $connectionMock;
21
22
    /** @var FileFinder|MockObject */
23
    protected $fileFinderMock;
24
25
    /** @var string */
26
    protected $migrationsPath = 'foo';
27
28
    /** @var string */
29
    protected $driverName = 'bar';
30
31
    public function setUp(): void
32
    {
33
        $this->connectionMock = $this->createMock(Connection::class);
34
35
        $this->fileFinderMock = $this->createMock(FileFinder::class);
36
37
        $this->sut = new BaseMigration($this->connectionMock, $this->fileFinderMock);
38
39
        parent::setUp();
40
    }
41
42
    public function testGetCreationDateCreatesADateTime()
43
    {
44
        $actualResult = $this->sut->getCreationDate();
45
46
        $this->assertInstanceOf(DateTime::class, $actualResult);
47
48
        $diff = (new DateTime())->diff($actualResult);
49
        $this->assertSame('0,0,0,0,0', $diff->format('%y,%m,%d,%h,%m'));
50
    }
51
52
    public function testDownLoadsAFileAndExecutesItAsSql()
53
    {
54
        $content = 'SELECT 1;';
55
56
        $statementMock = $this->createMock(Statement::class);
57
        $statementMock->expects($this->once())->method('execute')->willReturn(true);
58
59
        $this->fileFinderMock
60
            ->expects($this->any())
61
            ->method('read')
62
            ->with('down/foo-bar')
63
            ->willReturn($content);
64
65
        $this->connectionMock
66
            ->expects($this->any())
67
            ->method('prepare')
68
            ->with($content)
69
            ->willReturn($statementMock);
70
71
        $this->sut->down();
72
    }
73
74
    public function testUpLoadsAFileAndExecutesItAsSql()
75
    {
76
        $content = 'SELECT 1;';
77
78
        $statementMock = $this->createMock(Statement::class);
79
        $statementMock->expects($this->once())->method('execute')->willReturn(true);
80
81
        $this->fileFinderMock
82
            ->expects($this->any())
83
            ->method('read')
84
            ->with('up/foo-bar')
85
            ->willReturn($content);
86
87
        $this->connectionMock
88
            ->expects($this->any())
89
            ->method('prepare')
90
            ->with($content)
91
            ->willReturn($statementMock);
92
93
        $this->sut->up();
94
    }
95
96
    public function testDownThrowsExceptionIfExecutionFails()
97
    {
98
        $this->expectException(Exception::class);
99
100
        $exceptionData = ['foo', 'bar', 'baz'];
101
102
        $content = 'SELECT 1;';
103
104
        $statementMock = $this->createMock(Statement::class);
105
        $statementMock->expects($this->once())->method('execute')->willReturn(false);
106
        $statementMock->expects($this->atLeastOnce())->method('errorInfo')->willReturn($exceptionData);
107
108
        $this->fileFinderMock
109
            ->expects($this->any())
110
            ->method('read')
111
            ->with('down/foo-bar')
112
            ->willReturn($content);
113
114
        $this->connectionMock
115
            ->expects($this->any())
116
            ->method('prepare')
117
            ->with($content)
118
            ->willReturn($statementMock);
119
120
        $this->sut->down();
121
    }
122
123
    public function testUpThrowsExceptionIfExecutionFails()
124
    {
125
        $this->expectException(Exception::class);
126
127
        $exceptionData = ['foo', 'bar', 'baz'];
128
129
        $content = 'SELECT 1;';
130
131
        $statementMock = $this->createMock(Statement::class);
132
        $statementMock->expects($this->once())->method('execute')->willReturn(false);
133
        $statementMock->expects($this->atLeastOnce())->method('errorInfo')->willReturn($exceptionData);
134
135
        $this->fileFinderMock
136
            ->expects($this->any())
137
            ->method('read')
138
            ->with('up/foo-bar')
139
            ->willReturn($content);
140
141
        $this->connectionMock
142
            ->expects($this->any())
143
            ->method('prepare')
144
            ->with($content)
145
            ->willReturn($statementMock);
146
147
        $this->sut->up();
148
    }
149
}
150