74 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php declare(strict_types=1);
 | 
						|
 | 
						|
namespace Test\Unit\Query;
 | 
						|
 | 
						|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
 | 
						|
use BitBadger\PDODocument\Query\Exists;
 | 
						|
use PHPUnit\Framework\Attributes\TestDox;
 | 
						|
use PHPUnit\Framework\TestCase;
 | 
						|
 | 
						|
/**
 | 
						|
 * Unit tests for the Exists class
 | 
						|
 */
 | 
						|
#[TestDox('Exists Queries (Unit tests)')]
 | 
						|
class ExistsTest extends TestCase
 | 
						|
{
 | 
						|
    protected function tearDown(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = null;
 | 
						|
    }
 | 
						|
 | 
						|
    public function testQuerySucceeds(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::SQLite;
 | 
						|
        $this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),
 | 
						|
            'Existence query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By ID succeeds')]
 | 
						|
    public function testByIdSucceeds(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::SQLite;
 | 
						|
        $this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'),
 | 
						|
            'Existence query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    public function testByFieldsSucceeds(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::SQLite;
 | 
						|
        $this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)",
 | 
						|
            Exists::byFields('box', [Field::NE('status', 'occupied', ':status')]),
 | 
						|
            'Existence query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By contains succeeds for PostgreSQL')]
 | 
						|
    public function testByContainsSucceedsForPostgreSQL(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::PgSQL;
 | 
						|
        $this->assertEquals('SELECT EXISTS (SELECT 1 FROM pocket WHERE data @> :criteria)',
 | 
						|
            Exists::byContains('pocket'), 'Existence query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By contains fails for non PostgreSQL')]
 | 
						|
    public function testByContainsFailsForNonPostgreSQL(): void
 | 
						|
    {
 | 
						|
        $this->expectException(DocumentException::class);
 | 
						|
        Exists::byContains('');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By JSON Path succeeds for PostgreSQL')]
 | 
						|
    public function testByJsonPathSucceedsForPostgreSQL(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::PgSQL;
 | 
						|
        $this->assertEquals('SELECT EXISTS (SELECT 1 FROM lint WHERE jsonb_path_exists(data, :path::jsonpath))',
 | 
						|
            Exists::byJsonPath('lint'), 'Existence query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By JSON Path fails for non PostgreSQL')]
 | 
						|
    public function testByJsonPathFailsForNonPostgreSQL(): void
 | 
						|
    {
 | 
						|
        $this->expectException(DocumentException::class);
 | 
						|
        Exists::byJsonPath('');
 | 
						|
    }
 | 
						|
}
 |