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

testStatementsWithFetchResultPrintsResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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');
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->createMock('\Doctrine\DBAL\Connection') of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\DBAL\Connection of property $connectionMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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))
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