2023-06-28 20:18:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Illuminate\Tests\Database;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Connection;
|
2024-10-31 20:01:30 +02:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2023-06-28 20:18:22 +01:00
|
|
|
use Illuminate\Database\Query\Grammars\PostgresGrammar;
|
|
|
|
|
use Mockery as m;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class DatabasePostgresQueryGrammarTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function testToRawSql()
|
|
|
|
|
{
|
|
|
|
|
$connection = m::mock(Connection::class);
|
|
|
|
|
$connection->shouldReceive('escape')->with('foo', false)->andReturn("'foo'");
|
2025-02-12 20:00:57 +03:30
|
|
|
$grammar = new PostgresGrammar($connection);
|
2023-06-28 20:18:22 +01:00
|
|
|
|
|
|
|
|
$query = $grammar->substituteBindingsIntoRawSql(
|
|
|
|
|
'select * from "users" where \'{}\' ?? \'Hello\\\'\\\'World?\' AND "email" = ?',
|
|
|
|
|
['foo'],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertSame('select * from "users" where \'{}\' ? \'Hello\\\'\\\'World?\' AND "email" = \'foo\'', $query);
|
|
|
|
|
}
|
2024-10-29 19:35:03 +05:30
|
|
|
|
|
|
|
|
public function testCustomOperators()
|
|
|
|
|
{
|
|
|
|
|
PostgresGrammar::customOperators(['@@@', '@>', '']);
|
|
|
|
|
PostgresGrammar::customOperators(['@@>', 1]);
|
|
|
|
|
|
|
|
|
|
$connection = m::mock(Connection::class);
|
2025-02-12 20:00:57 +03:30
|
|
|
$grammar = new PostgresGrammar($connection);
|
2024-10-29 19:35:03 +05:30
|
|
|
|
|
|
|
|
$operators = $grammar->getOperators();
|
|
|
|
|
|
|
|
|
|
$this->assertIsList($operators);
|
|
|
|
|
$this->assertContains('@@@', $operators);
|
|
|
|
|
$this->assertContains('@@>', $operators);
|
|
|
|
|
$this->assertNotContains('', $operators);
|
|
|
|
|
$this->assertNotContains(1, $operators);
|
|
|
|
|
$this->assertSame(array_unique($operators), $operators);
|
|
|
|
|
}
|
2024-10-31 20:01:30 +02:00
|
|
|
|
|
|
|
|
public function testCompileTruncate()
|
|
|
|
|
{
|
2025-02-12 20:00:57 +03:30
|
|
|
$connection = m::mock(Connection::class);
|
|
|
|
|
$connection->shouldReceive('getTablePrefix')->andReturn('');
|
|
|
|
|
|
|
|
|
|
$postgres = new PostgresGrammar($connection);
|
2024-10-31 20:01:30 +02:00
|
|
|
$builder = m::mock(Builder::class);
|
|
|
|
|
$builder->from = 'users';
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
'truncate "users" restart identity cascade' => [],
|
|
|
|
|
], $postgres->compileTruncate($builder));
|
|
|
|
|
|
2025-03-31 16:05:51 +02:00
|
|
|
PostgresGrammar::cascadeOnTruncate(false);
|
2024-10-31 20:01:30 +02:00
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
'truncate "users" restart identity' => [],
|
|
|
|
|
], $postgres->compileTruncate($builder));
|
|
|
|
|
|
2025-03-31 16:05:51 +02:00
|
|
|
PostgresGrammar::cascadeOnTruncate();
|
2024-10-31 20:01:30 +02:00
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
'truncate "users" restart identity cascade' => [],
|
|
|
|
|
], $postgres->compileTruncate($builder));
|
|
|
|
|
}
|
2023-06-28 20:18:22 +01:00
|
|
|
}
|