68 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php declare(strict_types=1);
 | 
						|
 | 
						|
namespace Test\Unit\Query;
 | 
						|
 | 
						|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode};
 | 
						|
use BitBadger\PDODocument\Query\Find;
 | 
						|
use PHPUnit\Framework\Attributes\TestDox;
 | 
						|
use PHPUnit\Framework\TestCase;
 | 
						|
 | 
						|
/**
 | 
						|
 * Unit tests for the Find class
 | 
						|
 */
 | 
						|
#[TestDox('Find Queries (Unit tests)')]
 | 
						|
class FindTest extends TestCase
 | 
						|
{
 | 
						|
    protected function tearDown(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = null;
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By ID succeeds')]
 | 
						|
    public function testByIdSucceeds(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::SQLite;
 | 
						|
        $this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
 | 
						|
            'SELECT query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    public function testByFieldsSucceeds(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::SQLite;
 | 
						|
        $this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
 | 
						|
            Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')],
 | 
						|
                FieldMatch::Any),
 | 
						|
            'SELECT query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By contains succeeds for PostgreSQL')]
 | 
						|
    public function testByContainsSucceedsForPostgreSQL(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::PgSQL;
 | 
						|
        $this->assertEquals('SELECT data FROM disc WHERE data @> :criteria', Find::byContains('disc'),
 | 
						|
            'SELECT query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By contains fails for non PostgreSQL')]
 | 
						|
    public function testByContainsFailsForNonPostgreSQL(): void
 | 
						|
    {
 | 
						|
        $this->expectException(DocumentException::class);
 | 
						|
        Find::byContains('');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By JSON Path succeeds for PostgreSQL')]
 | 
						|
    public function testByJsonPathSucceedsForPostgreSQL(): void
 | 
						|
    {
 | 
						|
        Configuration::$mode = Mode::PgSQL;
 | 
						|
        $this->assertEquals('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)',
 | 
						|
            Find::byJsonPath('light'), 'SELECT query not generated correctly');
 | 
						|
    }
 | 
						|
 | 
						|
    #[TestDox('By JSON Path fails for non PostgreSQL')]
 | 
						|
    public function testByJsonPathFailsForNonPostgreSQL(): void
 | 
						|
    {
 | 
						|
        $this->expectException(DocumentException::class);
 | 
						|
        Find::byJsonPath('');
 | 
						|
    }
 | 
						|
}
 |