summaryrefslogtreecommitdiff
path: root/tests/Unit/Database/Migration/MigrateTest.php
blob: 5f0f4ec624c47275200a9e6ace5f06056fe080b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

namespace Engelsystem\Test\Unit\Database;

use Engelsystem\Application;
use Engelsystem\Database\Migration\Migrate;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Schema\Builder as SchemaBuilder;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MigrateTest extends TestCase
{
    /**
     * @covers \Engelsystem\Database\Migration\Migrate::__construct
     * @covers \Engelsystem\Database\Migration\Migrate::getMigrations
     * @covers \Engelsystem\Database\Migration\Migrate::run
     * @covers \Engelsystem\Database\Migration\Migrate::setOutput
     */
    public function testRun()
    {
        /** @var Application|MockObject $app */
        $app = $this->getMockBuilder(Application::class)
            ->setMethods(['instance'])
            ->getMock();
        /** @var SchemaBuilder|MockObject $builder */
        $builder = $this->getMockBuilder(SchemaBuilder::class)
            ->disableOriginalConstructor()
            ->getMock();
        /** @var Migrate|MockObject $migration */
        $migration = $this->getMockBuilder(Migrate::class)
            ->setConstructorArgs([$builder, $app])
            ->setMethods(['initMigration', 'getMigrationFiles', 'getMigrated', 'migrate', 'setMigrated'])
            ->getMock();

        $migration->expects($this->atLeastOnce())
            ->method('initMigration');
        $migration->expects($this->atLeastOnce())
            ->method('getMigrationFiles')
            ->willReturn([
                'foo/1234_01_23_123456_init_foo.php',
                'foo/9876_03_22_210000_random_hack.php',
                'foo/4567_11_01_000000_do_stuff.php',
                'foo/9999_99_99_999999_another_foo.php',
            ]);
        $migration->expects($this->atLeastOnce())
            ->method('getMigrated')
            ->willReturn(new Collection([
                ['id' => 1, 'migration' => '1234_01_23_123456_init_foo'],
                ['id' => 2, 'migration' => '4567_11_01_000000_do_stuff'],
            ]));
        $migration->expects($this->atLeastOnce())
            ->method('migrate')
            ->withConsecutive(
                ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP],
                ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Migrate::UP],
                ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP],
                ['foo/9999_99_99_999999_another_foo.php', '9999_99_99_999999_another_foo', Migrate::UP],
                ['foo/9876_03_22_210000_random_hack.php', '9876_03_22_210000_random_hack', Migrate::UP],
                ['foo/4567_11_01_000000_do_stuff.php', '4567_11_01_000000_do_stuff', Migrate::DOWN]
            );
        $migration->expects($this->atLeastOnce())
            ->method('setMigrated')
            ->withConsecutive(
                ['9876_03_22_210000_random_hack', Migrate::UP],
                ['9999_99_99_999999_another_foo', Migrate::UP],
                ['9876_03_22_210000_random_hack', Migrate::UP],
                ['9999_99_99_999999_another_foo', Migrate::UP],
                ['9876_03_22_210000_random_hack', Migrate::UP],
                ['4567_11_01_000000_do_stuff', Migrate::DOWN]
            );

        $migration->run('foo', Migrate::UP);

        $messages = [];
        $migration->setOutput(function ($text) use (&$messages) {
            $messages[] = $text;
        });

        $migration->run('foo', Migrate::UP);

        $this->assertCount(4, $messages);
        foreach (
            [
                'init_foo'    => 'skipping',
                'do_stuff'    => 'skipping',
                'random_hack' => 'migrating',
                'another_foo' => 'migrating',
            ] as $value => $type
        ) {
            $contains = false;
            foreach ($messages as $message) {
                if (!Str::contains(mb_strtolower($message), $type) || !Str::contains(mb_strtolower($message), $value)) {
                    continue;
                }

                $contains = true;
                break;
            }

            $this->assertTrue($contains, sprintf('Missing message "%s: %s"', $type, $value));
        }

        $messages = [];
        $migration->run('foo', Migrate::UP, true);
        $this->assertCount(3, $messages);

        $migration->run('foo', Migrate::DOWN, true);
    }

    /**
     * @covers \Engelsystem\Database\Migration\Migrate::getMigrated
     * @covers \Engelsystem\Database\Migration\Migrate::getMigrationFiles
     * @covers \Engelsystem\Database\Migration\Migrate::getTableQuery
     * @covers \Engelsystem\Database\Migration\Migrate::initMigration
     * @covers \Engelsystem\Database\Migration\Migrate::migrate
     * @covers \Engelsystem\Database\Migration\Migrate::setMigrated
     */
    public function testRunIntegration()
    {
        $app = new Application();
        $dbManager = new CapsuleManager($app);
        $dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
        $dbManager->bootEloquent();
        $db = $dbManager->getConnection();
        $db->useDefaultSchemaGrammar();
        $schema = $db->getSchemaBuilder();

        $app->instance('schema', $schema);
        $app->bind(SchemaBuilder::class, 'schema');

        $migration = new Migrate($schema, $app);

        $messages = [];
        $migration->setOutput(function ($msg) use (&$messages) {
            $messages[] = $msg;
        });

        $migration->run(__DIR__ . '/Stub', Migrate::UP);

        $this->assertTrue($schema->hasTable('migrations'));

        $migrations = $db->table('migrations')->get();
        $this->assertCount(3, $migrations);

        $this->assertTrue($migrations->contains('migration', '2001_04_11_123456_create_lorem_ipsum_table'));
        $this->assertTrue($migrations->contains('migration', '2017_12_24_053300_another_stuff'));
        $this->assertTrue($migrations->contains('migration', '2022_12_22_221222_add_some_feature'));

        $this->assertTrue($schema->hasTable('lorem_ipsum'));

        $migration->run(__DIR__ . '/Stub', Migrate::DOWN, true);

        $migrations = $db->table('migrations')->get();
        $this->assertCount(2, $migrations);

        $migration->run(__DIR__ . '/Stub', Migrate::DOWN);

        $migrations = $db->table('migrations')->get();
        $this->assertCount(0, $migrations);

        $this->assertFalse($schema->hasTable('lorem_ipsum'));
    }
}