Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

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

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
    private $connectionMock;
18
19
    protected function setUp()
20
    {
21
        $application = new Application();
22
        $application->add(new RunSqlCommand());
23
24
        $this->command = $application->find('dbal:run-sql');
25
        $this->commandTester = new CommandTester($this->command);
26
27
        $this->connectionMock = $this->createMock('\Doctrine\DBAL\Connection');
28
        $this->connectionMock->method('fetchAll')
29
            ->willReturn(array(array(1)));
30
        $this->connectionMock->method('executeUpdate')
31
            ->willReturn(42);
32
33
        $helperSet = ConsoleRunner::createHelperSet($this->connectionMock);
34
        $this->command->setHelperSet($helperSet);
35
    }
36
37
    public function testMissingSqlArgument()
38
    {
39
        try {
40
            $this->commandTester->execute(array(
41
                'command' => $this->command->getName(),
42
                'sql' => null,
43
            ));
44
            $this->fail('Expected a runtime exception when omitting sql argument');
45
        } catch (\RuntimeException $e) {
46
            self::assertContains("Argument 'SQL", $e->getMessage());
47
        }
48
    }
49
50
    public function testIncorrectDepthOption()
51
    {
52
        try {
53
            $this->commandTester->execute(array(
54
                'command' => $this->command->getName(),
55
                'sql' => 'SELECT 1',
56
                '--depth' => 'string',
57
            ));
58
            $this->fail('Expected a logic exception when executing with a stringy depth');
59
        } catch (\LogicException $e) {
60
            self::assertContains("Option 'depth'", $e->getMessage());
61
        }
62
    }
63
64 View Code Duplication
    public function testSelectStatementsPrintsResult()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $this->expectConnectionFetchAll();
67
68
        $this->commandTester->execute(array(
69
            'command' => $this->command->getName(),
70
            'sql' => 'SELECT 1',
71
        ));
72
73
        self::assertRegExp('@int.*1.*@', $this->commandTester->getDisplay());
74
        self::assertRegExp('@array.*1.*@', $this->commandTester->getDisplay());
75
    }
76
77 View Code Duplication
    public function testUpdateStatementsPrintsAffectedLines()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $this->expectConnectionExecuteUpdate();
80
81
        $this->commandTester->execute(array(
82
            'command' => $this->command->getName(),
83
            'sql' => 'UPDATE foo SET bar = 42',
84
        ));
85
86
        self::assertRegExp('@int.*42.*@', $this->commandTester->getDisplay());
87
        self::assertNotRegExp('@array.*1.*@', $this->commandTester->getDisplay());
88
    }
89
90 View Code Duplication
    private function expectConnectionExecuteUpdate()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $this->connectionMock
93
            ->expects($this->exactly(1))
94
            ->method('executeUpdate');
95
        $this->connectionMock
96
            ->expects($this->exactly(0))
97
            ->method('fetchAll');
98
    }
99
100 View Code Duplication
    private function expectConnectionFetchAll()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $this->connectionMock
103
            ->expects($this->exactly(0))
104
            ->method('executeUpdate');
105
        $this->connectionMock
106
            ->expects($this->exactly(1))
107
            ->method('fetchAll');
108
    }
109
110 View Code Duplication
    public function testStatementsWithFetchResultPrintsResult()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $this->expectConnectionFetchAll();
113
114
        $this->commandTester->execute(array(
115
            'command' => $this->command->getName(),
116
            'sql' => '"WITH bar as (SELECT 1) SELECT * FROM bar',
117
            '--force-fetch' => true,
118
        ));
119
120
        self::assertRegExp('@int.*1.*@', $this->commandTester->getDisplay());
121
        self::assertRegExp('@array.*1.*@', $this->commandTester->getDisplay());
122
    }
123
}
124