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