Completed
Push — master ( 7f4ef4...16d449 )
by Sergei
34:13 queued 34:08
created

Tests/DBAL/Tools/Console/RunSqlCommandTest.php (4 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Tools\Console;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
9
use Doctrine\DBAL\Tools\Console\ConsoleRunner;
10
use LogicException;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13
use RuntimeException;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Tester\CommandTester;
16
17
class RunSqlCommandTest extends TestCase
18
{
19
    /** @var CommandTester */
20
    private $commandTester;
21
    /** @var RunSqlCommand */
22
    private $command;
23
24
    /** @var Connection|MockObject */
25
    private $connectionMock;
26
27
    protected function setUp() : void
28
    {
29
        $this->command = new RunSqlCommand();
30
31
        (new Application())->add($this->command);
32
33
        $this->commandTester = new CommandTester($this->command);
34
35
        $this->connectionMock = $this->createMock(Connection::class);
36
        $this->connectionMock->method('fetchAll')
37
            ->willReturn([[1]]);
38
        $this->connectionMock->method('executeUpdate')
39
            ->willReturn(42);
40
41
        $helperSet = ConsoleRunner::createHelperSet($this->connectionMock);
42
        $this->command->setHelperSet($helperSet);
43
    }
44
45
    public function testMissingSqlArgument() : void
46
    {
47
        self::expectException(RuntimeException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

47
        self::/** @scrutinizer ignore-call */ 
48
              expectException(RuntimeException::class);
Loading history...
48
        self::expectExceptionMessage('Argument "sql" is required in order to execute this command correctly.');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

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

48
        self::/** @scrutinizer ignore-call */ 
49
              expectExceptionMessage('Argument "sql" is required in order to execute this command correctly.');
Loading history...
49
50
        $this->commandTester->execute([
51
            'command' => $this->command->getName(),
52
            'sql' => null,
53
        ]);
54
    }
55
56
    public function testIncorrectDepthOption() : void
57
    {
58
        self::expectException(LogicException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

58
        self::/** @scrutinizer ignore-call */ 
59
              expectException(LogicException::class);
Loading history...
59
        self::expectExceptionMessage('Option "depth" must contains an integer value.');
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCa...xpectExceptionMessage() is not static, but was called statically. ( Ignorable by Annotation )

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

59
        self::/** @scrutinizer ignore-call */ 
60
              expectExceptionMessage('Option "depth" must contains an integer value.');
Loading history...
60
61
        $this->commandTester->execute([
62
            'command' => $this->command->getName(),
63
            'sql' => 'SELECT 1',
64
            '--depth' => 'string',
65
        ]);
66
    }
67
68
    public function testSelectStatementsPrintsResult() : void
69
    {
70
        $this->expectConnectionFetchAll();
71
72
        $exitCode = $this->commandTester->execute([
73
            'command' => $this->command->getName(),
74
            'sql' => 'SELECT 1',
75
        ]);
76
        $this->assertSame(0, $exitCode);
77
78
        self::assertRegExp('@int.*1.*@', $this->commandTester->getDisplay());
79
        self::assertRegExp('@array.*1.*@', $this->commandTester->getDisplay());
80
    }
81
82
    public function testUpdateStatementsPrintsAffectedLines() : void
83
    {
84
        $this->expectConnectionExecuteUpdate();
85
86
        $this->commandTester->execute([
87
            'command' => $this->command->getName(),
88
            'sql' => 'UPDATE foo SET bar = 42',
89
        ]);
90
91
        self::assertRegExp('@int.*42.*@', $this->commandTester->getDisplay());
92
        self::assertNotRegExp('@array.*1.*@', $this->commandTester->getDisplay());
93
    }
94
95
    private function expectConnectionExecuteUpdate() : void
96
    {
97
        $this->connectionMock
98
            ->expects($this->exactly(1))
99
            ->method('executeUpdate');
100
        $this->connectionMock
101
            ->expects($this->exactly(0))
102
            ->method('fetchAll');
103
    }
104
105
    private function expectConnectionFetchAll() : void
106
    {
107
        $this->connectionMock
108
            ->expects($this->exactly(0))
109
            ->method('executeUpdate');
110
        $this->connectionMock
111
            ->expects($this->exactly(1))
112
            ->method('fetchAll');
113
    }
114
115
    public function testStatementsWithFetchResultPrintsResult() : void
116
    {
117
        $this->expectConnectionFetchAll();
118
119
        $this->commandTester->execute([
120
            'command' => $this->command->getName(),
121
            'sql' => '"WITH bar as (SELECT 1) SELECT * FROM bar',
122
            '--force-fetch' => true,
123
        ]);
124
125
        self::assertRegExp('@int.*1.*@', $this->commandTester->getDisplay());
126
        self::assertRegExp('@array.*1.*@', $this->commandTester->getDisplay());
127
    }
128
}
129