* @license MIT */ declare(strict_types=1); namespace Test\Unit; use BitBadger\PDODocument\{Configuration, Field, Mode, Op}; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; /** * Unit tests for the Field class */ #[TestDox('Field (Unit tests)')] class FieldTest extends TestCase { public function testAppendParameterSucceedsForExists(): void { $this->assertEquals([], Field::exists('exists')->appendParameter([]), 'exists should not have appended a parameter'); } #[TestDox('Append parameter succeeds for notExists')] public function testAppendParameterSucceedsForNotExists(): void { $this->assertEquals([], Field::notExists('absent')->appendParameter([]), 'notExists should not have appended a parameter'); } public function testAppendParameterSucceedsForBetween(): void { $this->assertEquals(['@nummin' => 5, '@nummax' => 9], Field::between('exists', 5, 9, '@num')->appendParameter([]), 'BT should have appended min and max parameters'); } public function testAppendParameterSucceedsForOthers(): void { $this->assertEquals(['@test' => 33], Field::equal('the_field', 33, '@test')->appendParameter([]), 'Field parameter not returned correctly'); } #[TestDox('To where succeeds for exists without qualifier for PostgreSQL')] public function testToWhereSucceedsForExistsWithoutQualifierForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL); try { $this->assertEquals("data->>'that_field' IS NOT NULL", Field::exists('that_field')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for exists without qualifier for SQLite')] public function testToWhereSucceedsForExistsWithoutQualifierForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $this->assertEquals("data->>'that_field' IS NOT NULL", Field::exists('that_field')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for notExists without qualifier for PostgreSQL')] public function testToWhereSucceedsForNotExistsWithoutQualifierForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL); try { $this->assertEquals("data->>'a_field' IS NULL", Field::notExists('a_field')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for notExists without qualifier for SQLite')] public function testToWhereSucceedsForNotExistsWithoutQualifierForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $this->assertEquals("data->>'a_field' IS NULL", Field::notExists('a_field')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for between without qualifier for SQLite')] public function testToWhereSucceedsForBetweenWithoutQualifierForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $this->assertEquals("data->>'age' BETWEEN @agemin AND @agemax", Field::between('age', 13, 17, '@age')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for between without qualifier for PostgreSQL with numeric range')] public function testToWhereSucceedsForBetweenWithoutQualifierForPostgreSQLWithNumericRange(): void { Configuration::overrideMode(Mode::PgSQL); try { $this->assertEquals("(data->>'age')::numeric BETWEEN @agemin AND @agemax", Field::between('age', 13, 17, '@age')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for between without qualifier for PostgreSQL with non-numeric range')] public function testToWhereSucceedsForBetweenWithoutQualifierForPostgreSQLWithNonNumericRange(): void { Configuration::overrideMode(Mode::PgSQL); try { $this->assertEquals("data->>'city' BETWEEN :citymin AND :citymax", Field::between('city', 'Atlanta', 'Chicago', ':city')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for between with qualifier for SQLite')] public function testToWhereSucceedsForBetweenWithQualifierForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $field = Field::between('age', 13, 17, '@age'); $field->qualifier = 'me'; $this->assertEquals("me.data->>'age' BETWEEN @agemin AND @agemax", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for between with qualifier for PostgreSQL with numeric range')] public function testToWhereSucceedsForBetweenWithQualifierForPostgreSQLWithNumericRange(): void { Configuration::overrideMode(Mode::PgSQL); try { $field = Field::between('age', 13, 17, '@age'); $field->qualifier = 'me'; $this->assertEquals("(me.data->>'age')::numeric BETWEEN @agemin AND @agemax", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for between with qualifier for PostgreSQL with non-numeric range')] public function testToWhereSucceedsForBetweenWithQualifierForPostgreSQLWithNonNumericRange(): void { Configuration::overrideMode(Mode::PgSQL); try { $field = Field::between('city', 'Atlanta', 'Chicago', ':city'); $field->qualifier = 'me'; $this->assertEquals("me.data->>'city' BETWEEN :citymin AND :citymax", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for others without qualifier for PostgreSQL')] public function testToWhereSucceedsForOthersWithoutQualifierForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL); try { $this->assertEquals("data->>'some_field' = @value", Field::equal('some_field', '', '@value')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds for others without qualifier for SQLite')] public function testToWhereSucceedsForOthersWithoutQualifierForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $this->assertEquals("data->>'some_field' = @value", Field::equal('some_field', '', '@value')->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds with qualifier no parameter for PostgreSQL')] public function testToWhereSucceedsWithQualifierNoParameterForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL); try { $field = Field::exists('no_field'); $field->qualifier = 'test'; $this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds with qualifier no parameter for SQLite')] public function testToWhereSucceedsWithQualifierNoParameterForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $field = Field::exists('no_field'); $field->qualifier = 'test'; $this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds with qualifier and parameter for PostgreSQL')] public function testToWhereSucceedsWithQualifierAndParameterForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL); try { $field = Field::lessOrEqual('le_field', 18, '@it'); $field->qualifier = 'q'; $this->assertEquals("(q.data->>'le_field')::numeric <= @it", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds with qualifier and parameter for SQLite')] public function testToWhereSucceedsWithQualifierAndParameterForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $field = Field::lessOrEqual('le_field', 18, '@it'); $field->qualifier = 'q'; $this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds with sub-document for PostgreSQL')] public function testToWhereSucceedsWithSubDocumentForPostgreSQL(): void { Configuration::overrideMode(Mode::PgSQL); try { $field = Field::equal('sub.foo.bar', 22, '@it'); $this->assertEquals("(data#>>'{sub,foo,bar}')::numeric = @it", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('To where succeeds with sub-document for SQLite')] public function testToWhereSucceedsWithSubDocumentForSQLite(): void { Configuration::overrideMode(Mode::SQLite); try { $field = Field::equal('sub.foo.bar', 22, '@it'); $this->assertEquals("data->>'sub'->>'foo'->>'bar' = @it", $field->toWhere(), 'WHERE fragment not generated correctly'); } finally { Configuration::overrideMode(null); } } #[TestDox('equal succeeds without parameter')] public function testEqualSucceedsWithoutParameter(): void { $field = Field::equal('my_test', 9); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('my_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Equal, $field->op, 'Operation not filled correctly'); $this->assertEquals(9, $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('equal succeeds with parameter')] public function testEqualSucceedsWithParameter(): void { $field = Field::equal('another_test', 'turkey', '@test'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('another_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Equal, $field->op, 'Operation not filled correctly'); $this->assertEquals('turkey', $field->value, 'Value not filled correctly'); $this->assertEquals('@test', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('greater succeeds without parameter')] public function testGreaterSucceedsWithoutParameter(): void { $field = Field::greater('your_test', 4); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('your_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Greater, $field->op, 'Operation not filled correctly'); $this->assertEquals(4, $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('greater succeeds with parameter')] public function testGreaterSucceedsWithParameter(): void { $field = Field::greater('more_test', 'chicken', '@value'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('more_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Greater, $field->op, 'Operation not filled correctly'); $this->assertEquals('chicken', $field->value, 'Value not filled correctly'); $this->assertEquals('@value', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('greaterOrEqual succeeds without parameter')] public function testGreaterOrEqualSucceedsWithoutParameter(): void { $field = Field::greaterOrEqual('their_test', 6); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('their_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::GreaterOrEqual, $field->op, 'Operation not filled correctly'); $this->assertEquals(6, $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('greaterOrEqual succeeds with parameter')] public function testGreaterOrEqualSucceedsWithParameter(): void { $field = Field::greaterOrEqual('greater_test', 'poultry', '@cluck'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('greater_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::GreaterOrEqual, $field->op, 'Operation not filled correctly'); $this->assertEquals('poultry', $field->value, 'Value not filled correctly'); $this->assertEquals('@cluck', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('less succeeds without parameter')] public function testLessSucceedsWithoutParameter(): void { $field = Field::less('z', 32); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('z', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Less, $field->op, 'Operation not filled correctly'); $this->assertEquals(32, $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('less succeeds with parameter')] public function testLessSucceedsWithParameter(): void { $field = Field::less('additional_test', 'fowl', '@boo'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('additional_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Less, $field->op, 'Operation not filled correctly'); $this->assertEquals('fowl', $field->value, 'Value not filled correctly'); $this->assertEquals('@boo', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('lessOrEqual succeeds without parameter')] public function testLessOrEqualSucceedsWithoutParameter(): void { $field = Field::lessOrEqual('g', 87); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('g', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::LessOrEqual, $field->op, 'Operation not filled correctly'); $this->assertEquals(87, $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('lessOrEqual succeeds with parameter')] public function testLessOrEqualSucceedsWithParameter(): void { $field = Field::lessOrEqual('lesser_test', 'hen', '@woo'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('lesser_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::LessOrEqual, $field->op, 'Operation not filled correctly'); $this->assertEquals('hen', $field->value, 'Value not filled correctly'); $this->assertEquals('@woo', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('notEqual succeeds without parameter')] public function testNotEqualSucceedsWithoutParameter(): void { $field = Field::notEqual('j', 65); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('j', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::NotEqual, $field->op, 'Operation not filled correctly'); $this->assertEquals(65, $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('notEqual succeeds with parameter')] public function testNotEqualSucceedsWithParameter(): void { $field = Field::notEqual('unequal_test', 'egg', '@zoo'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('unequal_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::NotEqual, $field->op, 'Operation not filled correctly'); $this->assertEquals('egg', $field->value, 'Value not filled correctly'); $this->assertEquals('@zoo', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('between succeeds without parameter')] public function testBetweenSucceedsWithoutParameter(): void { $field = Field::between('k', 'alpha', 'zed'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('k', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Between, $field->op, 'Operation not filled correctly'); $this->assertEquals(['alpha', 'zed'], $field->value, 'Value not filled correctly'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('between succeeds with parameter')] public function testBetweenSucceedsWithParameter(): void { $field = Field::between('between_test', 18, 49, '@count'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('between_test', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Between, $field->op, 'Operation not filled correctly'); $this->assertEquals([18, 49], $field->value, 'Value not filled correctly'); $this->assertEquals('@count', $field->paramName, 'Parameter name not filled correctly'); } #[TestDox('exists succeeds')] public function testExistsSucceeds(): void { $field = Field::exists('be_there'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('be_there', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::Exists, $field->op, 'Operation not filled correctly'); $this->assertEquals('', $field->value, 'Value should have been blank'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } #[TestDox('notExists succeeds')] public function testNotExistsSucceeds(): void { $field = Field::notExists('be_absent'); $this->assertNotNull($field, 'The field should not have been null'); $this->assertEquals('be_absent', $field->fieldName, 'Field name not filled correctly'); $this->assertEquals(Op::NotExists, $field->op, 'Operation not filled correctly'); $this->assertEquals('', $field->value, 'Value should have been blank'); $this->assertEquals('', $field->paramName, 'Parameter name should have been blank'); } }