66 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php declare(strict_types=1);
 | |
| 
 | |
| namespace Test\Unit\Query;
 | |
| 
 | |
| use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
 | |
| use BitBadger\PDODocument\Query\Count;
 | |
| use PHPUnit\Framework\Attributes\TestDox;
 | |
| use PHPUnit\Framework\TestCase;
 | |
| 
 | |
| /**
 | |
|  * Unit tests for the Count class
 | |
|  */
 | |
| #[TestDox('Count Queries (Unit tests)')]
 | |
| class CountTest extends TestCase
 | |
| {
 | |
|     public function tearDown(): void
 | |
|     {
 | |
|         Configuration::$mode = null;
 | |
|         parent::tearDown();
 | |
|     }
 | |
| 
 | |
|     public function testAllSucceeds(): void
 | |
|     {
 | |
|         $this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'),
 | |
|             'SELECT statement not generated correctly');
 | |
|     }
 | |
| 
 | |
|     public function testByFieldsSucceeds(): void
 | |
|     {
 | |
|         Configuration::$mode = Mode::SQLite;
 | |
|         $this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
 | |
|             Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]),
 | |
|             'SELECT statement not generated correctly');
 | |
|     }
 | |
| 
 | |
|     #[TestDox('By contains succeeds for PostgreSQL')]
 | |
|     public function testByContainsSucceedsForPostgreSQL(): void
 | |
|     {
 | |
|         Configuration::$mode = Mode::PgSQL;
 | |
|         $this->assertEquals('SELECT COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'),
 | |
|             'SELECT statement not generated correctly');
 | |
|     }
 | |
| 
 | |
|     #[TestDox('By contains fails for non PostgreSQL')]
 | |
|     public function testByContainsFailsForNonPostgreSQL(): void
 | |
|     {
 | |
|         $this->expectException(DocumentException::class);
 | |
|         Count::byContains('');
 | |
|     }
 | |
| 
 | |
|     #[TestDox('By JSON Path succeeds for PostgreSQL')]
 | |
|     public function testByJsonPathSucceedsForPostgreSQL(): void
 | |
|     {
 | |
|         Configuration::$mode = Mode::PgSQL;
 | |
|         $this->assertEquals('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)',
 | |
|             Count::byJsonPath('a_table'), 'SELECT statement not generated correctly');
 | |
|     }
 | |
| 
 | |
|     #[TestDox('By JSON Path fails for non PostgreSQL')]
 | |
|     public function testByJsonPathFailsForNonPostgreSQL(): void
 | |
|     {
 | |
|         $this->expectException(DocumentException::class);
 | |
|         Count::byJsonPath('');
 | |
|     }
 | |
| }
 |