Completed
Pull Request — master (#3)
by Joao
04:56
created

MigrationTest::testGetMigrationSql3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace Test;
3
4
use ByJG\AnyDataset\Store\PdoMysql;
5
use ByJG\DbMigration\Migration;
6
use ByJG\Util\Uri;
7
8
// backward compatibility
9
if (!class_exists('\PHPUnit\Framework\TestCase')) {
10
    class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
11
}
12
13
class MigrationTest extends \PHPUnit\Framework\TestCase
14
{
15
    /**
16
     * @var Migration
17
     */
18
    protected $object;
19
20
    public function setUp()
21
    {
22
        $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/dirstructure');
23
    }
24
25
    public function tearDown()
26
    {
27
        $this->object = null;
28
    }
29
30
    public function testGetBaseSql()
31
    {
32
        $base = $this->object->getBaseSql();
33
        $this->assertEquals(__DIR__ . '/dirstructure/base.sql', $base);
34
    }
35
36
    /**
37
     * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile
38
     */
39
    public function testGetBaseSqlNotFound()
40
    {
41
        $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid');
42
        $this->object->getBaseSql();
0 ignored issues
show
Unused Code introduced by
The call to the method ByJG\DbMigration\Migration::getBaseSql() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
43
    }
44
45
    public function testGetMigrationSql1()
46
    {
47
        $version = $this->object->getMigrationSql(1, 1);
48
        $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00001.sql', $version);
49
    }
50
51
    public function testGetMigrationSql2()
52
    {
53
        $version = $this->object->getMigrationSql(2, 1);
54
        $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00002.sql', $version);
55
    }
56
57
    public function testGetMigrationSql3()
58
    {
59
        $version = $this->object->getMigrationSql(12, 1);
60
        $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00012-dev.sql', $version);
61
    }
62
63
    /**
64
     * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile
65
     * @expectedExceptionMessage version number '13'
66
     */
67
    public function testGetMigrationSql4()
68
    {
69
        $this->object->getMigrationSql(13, 1);
70
    }
71
72
    public function testGetMigrationSqlDown1()
73
    {
74
        $version = $this->object->getMigrationSql(1, -1);
75
        $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00001.sql', $version);
76
    }
77
78
    public function testGetMigrationSqlDown2()
79
    {
80
        $version = $this->object->getMigrationSql(2, -1);
81
        $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00002.sql', $version);
82
    }
83
84
    public function testGetMigrationSqlDown3()
85
    {
86
        $version = $this->object->getMigrationSql(12, -1);
87
        $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00012-dev.sql', $version);
88
    }
89
90
    /**
91
     * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile
92
     * @expectedExceptionMessage version number '13'
93
     */
94
    public function testGetMigrationSqlDown4()
95
    {
96
        $this->object->getMigrationSql(13, -1);
97
    }
98
}
99