Completed
Push — master ( e51e73...fb0ce7 )
by Michael
17s queued 12s
created

Tests/DBAL/Tools/Console/RunSqlCommandTest.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Tools\Console;
4
5
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
6
use Doctrine\DBAL\Tools\Console\ConsoleRunner;
7
use Symfony\Component\Console\Application;
8
use Symfony\Component\Console\Tester\CommandTester;
9
10
class RunSqlCommandTest extends \PHPUnit\Framework\TestCase
11
{
12
    /** @var CommandTester */
13
    private $commandTester;
14
    /** @var RunSqlCommand */
15
    private $command;
16
17
    /**
18
     * @var \Doctrine\DBAL\Connection
19
     */
20
    private $connectionMock;
21
22
    protected function setUp()
23
    {
24
        $application = new Application();
25
        $application->add(new RunSqlCommand());
26
27
        $this->command = $application->find('dbal:run-sql');
28
        $this->commandTester = new CommandTester($this->command);
29
30
        $this->connectionMock = $this->createMock('\Doctrine\DBAL\Connection');
31
        $this->connectionMock->method('fetchAll')
32
            ->willReturn(array(array(1)));
33
        $this->connectionMock->method('executeUpdate')
34
            ->willReturn(42);
35
36
        $helperSet = ConsoleRunner::createHelperSet($this->connectionMock);
37
        $this->command->setHelperSet($helperSet);
38
    }
39
40
    public function testMissingSqlArgument()
41
    {
42
        try {
43
            $this->commandTester->execute(array(
44
                'command' => $this->command->getName(),
45
                'sql' => null,
46
            ));
47
            $this->fail('Expected a runtime exception when omitting sql argument');
48
        } catch (\RuntimeException $e) {
49
            self::assertContains("Argument 'SQL", $e->getMessage());
50
        }
51
    }
52
53
    public function testIncorrectDepthOption()
54
    {
55
        try {
56
            $this->commandTester->execute(array(
57
                'command' => $this->command->getName(),
58
                'sql' => 'SELECT 1',
59
                '--depth' => 'string',
60
            ));
61
            $this->fail('Expected a logic exception when executing with a stringy depth');
62
        } catch (\LogicException $e) {
63
            self::assertContains("Option 'depth'", $e->getMessage());
64
        }
65
    }
66
67
    public function testSelectStatementsPrintsResult()
68
    {
69
        $this->expectConnectionFetchAll();
70
71
        $this->commandTester->execute(array(
72
            'command' => $this->command->getName(),
73
            'sql' => 'SELECT 1',
74
        ));
75
76
        self::assertRegExp('@int.*1.*@', $this->commandTester->getDisplay());
77
        self::assertRegExp('@array.*1.*@', $this->commandTester->getDisplay());
78
    }
79
80
    public function testUpdateStatementsPrintsAffectedLines()
81
    {
82
        $this->expectConnectionExecuteUpdate();
83
84
        $this->commandTester->execute(array(
85
            'command' => $this->command->getName(),
86
            'sql' => 'UPDATE foo SET bar = 42',
87
        ));
88
89
        self::assertRegExp('@int.*42.*@', $this->commandTester->getDisplay());
90
        self::assertNotRegExp('@array.*1.*@', $this->commandTester->getDisplay());
91
    }
92
93
    private function expectConnectionExecuteUpdate()
94
    {
95
        $this->connectionMock
96
            ->expects($this->exactly(1))
1 ignored issue
show
The method expects() does not exist on Doctrine\DBAL\Connection. ( Ignorable by Annotation )

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

96
            ->/** @scrutinizer ignore-call */ 
97
              expects($this->exactly(1))

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...
97
            ->method('executeUpdate');
98
        $this->connectionMock
99
            ->expects($this->exactly(0))
100
            ->method('fetchAll');
101
    }
102
103
    private function expectConnectionFetchAll()
104
    {
105
        $this->connectionMock
106
            ->expects($this->exactly(0))
107
            ->method('executeUpdate');
108
        $this->connectionMock
109
            ->expects($this->exactly(1))
110
            ->method('fetchAll');
111
    }
112
113
    public function testStatementsWithFetchResultPrintsResult()
114
    {
115
        $this->expectConnectionFetchAll();
116
117
        $this->commandTester->execute(array(
118
            'command' => $this->command->getName(),
119
            'sql' => '"WITH bar as (SELECT 1) SELECT * FROM bar',
120
            '--force-fetch' => true,
121
        ));
122
123
        self::assertRegExp('@int.*1.*@', $this->commandTester->getDisplay());
124
        self::assertRegExp('@array.*1.*@', $this->commandTester->getDisplay());
125
    }
126
}
127