Completed
Push — master ( 01a2b5...abb9d0 )
by Alexander
02:01
created

MigratorTest::testRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 39
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Quantick\DeployMigration\Tests\Lib\Service;
5
6
7
use Illuminate\Console\OutputStyle;
8
use Illuminate\Container\Container;
9
use Illuminate\Database\Connection;
10
use Illuminate\Database\Query\Builder;
11
use Illuminate\Filesystem\Filesystem;
12
use PHPUnit\Framework\TestCase;
13
use Quantick\DeployMigration\Lib\Service\Instantiator;
14
use Quantick\DeployMigration\Lib\Service\Migrator;
15
use Quantick\DeployMigration\Tests\Lib\Service\stub\TestCommand;
16
use Symfony\Component\Console\Input\ArrayInput;
17
use Symfony\Component\Console\Output\ConsoleOutput;
18
19
class MigratorTest extends TestCase
20
{
21
    /**
22
     * @throws \ReflectionException
23
     * @throws \Throwable
24
     */
25
    public function testRun()
26
    {
27
        $connection = $this->createMock(Connection::class);
28
        $connection->expects(static::once())->method('beginTransaction');
29
        $connection->expects(static::once())->method('commit');
30
        $container = $this->createMock(Container::class);
31
32
        $instantiator = Instantiator::create((new Filesystem())->files(__DIR__ . '/stub/migrations'));
33
        $collection   = $instantiator->run();
34
35
        $migrator = new Migrator($connection, $container);
36
37
        $builder     = $this->createMock(Builder::class);
38
        $infoBuilder = $this->createMock(Builder::class);
39
40
        $connection->expects(static::exactly(2))->method('table')
41
                   ->willReturnOnConsecutiveCalls(
42
                       $builder,
43
                       $infoBuilder
44
                   )
45
        ;
46
47
        $builder->expects(static::once())->method('insert');
48
        $infoBuilder->expects(static::once())->method('insert');
49
50
        $builder->method('where')->willReturn($builder);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $builder->/** @scrutinizer ignore-call */ 
51
                  method('where')->willReturn($builder);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $builder->method('count')->willReturn(0);
52
53
        $testCommand = new TestCommand();
54
55
        $container->method('get')->with(TestCommand::class)->willReturn($testCommand);
56
57
58
        $output = new OutputStyle(new ArrayInput([]), new ConsoleOutput());
59
        $output->progressStart(1);
60
61
        $container->method('make')->willReturn($output);
62
        $migrator->run($collection, $output);
63
        $output->progressFinish();
64
    }
65
66
    public function testRunWithError()
67
    {
68
        $connection = $this->createMock(Connection::class);
69
        $connection->expects(static::once())->method('beginTransaction');
70
        $connection->expects(static::once())->method('rollBack');
71
        $container = $this->createMock(Container::class);
72
73
        $instantiator = Instantiator::create((new Filesystem())->files(__DIR__ . '/stub/migrations'));
74
        $collection   = $instantiator->run();
75
76
        $migrator = new Migrator($connection, $container);
77
78
        $builder     = $this->createMock(Builder::class);
79
        $infoBuilder = $this->createMock(Builder::class);
80
81
        $connection->expects(static::exactly(2))->method('table')
82
                   ->willReturnOnConsecutiveCalls(
83
                       $builder,
84
                       $infoBuilder
85
                   )
86
        ;
87
88
        $infoBuilder->expects(static::once())->method('insert');
89
90
        $builder->method('where')->willReturn($builder);
91
        $builder->method('count')->willReturn(0);
92
93
        $testCommand = $this->createMock(TestCommand::class);
94
95
        $container->method('get')->with(TestCommand::class)->willReturn($testCommand);
96
97
        $output = new OutputStyle(new ArrayInput([]), new ConsoleOutput());
98
        $testCommand->method('run')->willThrowException(new \RuntimeException('exception'));
99
        $output->progressStart(1);
100
101
        $container->method('make')->willReturn($output);
102
        $this->expectException(\RuntimeException::class);
103
        $migrator->run($collection, $output);
104
        $output->progressFinish();
105
    }
106
}