| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Doctrine\DBAL\Tests\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('fetchAllAssociative') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |         $this->expectException(RuntimeException::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->expectExceptionMessage('Argument "sql" is required in order to execute this command correctly.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->commandTester->execute([ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             'command' => $this->command->getName(), | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             'sql' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function testIncorrectDepthOption() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->expectException(LogicException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->expectExceptionMessage('Option "depth" must contains an integer value.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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->expectConnectionFetchAllAssociative(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $exitCode = $this->commandTester->execute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'command' => $this->command->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             'sql' => 'SELECT 1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         self::assertSame(0, $exitCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         self::assertMatchesRegularExpression('@int.*1.*@', $this->commandTester->getDisplay()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         self::assertMatchesRegularExpression('@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::assertMatchesRegularExpression('@int.*42.*@', $this->commandTester->getDisplay()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         self::assertDoesNotMatchRegularExpression('@array.*1.*@', $this->commandTester->getDisplay()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function expectConnectionExecuteUpdate() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->connectionMock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ->expects(self::exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->method('executeUpdate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->connectionMock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->expects(self::exactly(0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->method('fetchAllAssociative'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     private function expectConnectionFetchAllAssociative() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->connectionMock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->expects(self::exactly(0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ->method('executeUpdate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->connectionMock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             ->expects(self::exactly(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ->method('fetchAllAssociative'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function testStatementsWithFetchResultPrintsResult() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->expectConnectionFetchAllAssociative(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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::assertMatchesRegularExpression('@int.*1.*@', $this->commandTester->getDisplay()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         self::assertMatchesRegularExpression('@array.*1.*@', $this->commandTester->getDisplay()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |