|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Database; |
| 4 | + |
| 5 | +use Illuminate\Database\Connection; |
| 6 | +use Illuminate\Database\Query\Grammars\Grammar; |
| 7 | +use Illuminate\Database\QueryException; |
| 8 | +use Illuminate\Support\Facades\DB; |
| 9 | +use Mockery as m; |
| 10 | +use PDOException; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class DatabaseQueryExceptionTest extends TestCase |
| 14 | +{ |
| 15 | + public function testIfItEmbedsBindingsIntoSql() |
| 16 | + { |
| 17 | + $connection = $this->getConnection(); |
| 18 | + |
| 19 | + $sql = 'SELECT * FROM huehue WHERE a = ? and hue = ?'; |
| 20 | + $bindings = [1, 'br']; |
| 21 | + |
| 22 | + $expectedSql = "SELECT * FROM huehue WHERE a = 1 and hue = 'br'"; |
| 23 | + |
| 24 | + $pdoException = new PDOException('Mock SQL error'); |
| 25 | + $exception = new QueryException($connection->getName(), $sql, $bindings, $pdoException); |
| 26 | + |
| 27 | + DB::shouldReceive('connection')->andReturn($connection); |
| 28 | + $result = $exception->getRawSql(); |
| 29 | + |
| 30 | + $this->assertSame($expectedSql, $result); |
| 31 | + } |
| 32 | + |
| 33 | + public function testIfItReturnsSameSqlWhenThereAreNoBindings() |
| 34 | + { |
| 35 | + $connection = $this->getConnection(); |
| 36 | + |
| 37 | + $sql = "SELECT * FROM huehue WHERE a = 1 and hue = 'br'"; |
| 38 | + $bindings = []; |
| 39 | + |
| 40 | + $expectedSql = $sql; |
| 41 | + |
| 42 | + $pdoException = new PDOException('Mock SQL error'); |
| 43 | + $exception = new QueryException($connection->getName(), $sql, $bindings, $pdoException); |
| 44 | + |
| 45 | + DB::shouldReceive('connection')->andReturn($connection); |
| 46 | + $result = $exception->getRawSql(); |
| 47 | + |
| 48 | + $this->assertSame($expectedSql, $result); |
| 49 | + } |
| 50 | + |
| 51 | + protected function getConnection() |
| 52 | + { |
| 53 | + $connection = m::mock(Connection::class); |
| 54 | + |
| 55 | + $grammar = new Grammar($connection); |
| 56 | + |
| 57 | + $connection->shouldReceive('getName')->andReturn('default'); |
| 58 | + $connection->shouldReceive('getQueryGrammar')->andReturn($grammar); |
| 59 | + $connection->shouldReceive('escape')->with(1, false)->andReturn(1); |
| 60 | + $connection->shouldReceive('escape')->with('br', false)->andReturn("'br'"); |
| 61 | + |
| 62 | + return $connection; |
| 63 | + } |
| 64 | +} |
0 commit comments