Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/AbstractDbValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getTable(): string
/**
* Run query and returns matches, or null if no matches are found.
*
* @return mixed when matches are found; null when there are none (PDO reports no rows as false).
* @return mixed when matches are found; null or false when there are none (PDO reports no rows as false).
*/
protected function query(mixed $value): mixed
{
Expand All @@ -179,8 +179,6 @@ protected function query(mixed $value): mixed
$parameters['where1'] = $value;
}

$row = $statement->execute()?->current();

return false === $row ? null : $row;
return $statement->execute()?->current();
}
}
2 changes: 1 addition & 1 deletion src/NoRecordExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function isValid(mixed $value): bool
$valid = true;
$this->setValue($value);

if ($this->query($value) !== null) {
if ($this->query($value)) {
$valid = false;
$this->error(self::ERROR_RECORD_FOUND);
}
Expand Down
18 changes: 15 additions & 3 deletions test/unit/NoRecordExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpDb\Adapter\ParameterContainer;
use PhpDb\Adapter\Platform\Sql92;
use PhpDb\Validator\NoRecordExists;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\Exception;
Expand All @@ -22,6 +23,16 @@
#[Group('unit')]
final class NoRecordExistsTest extends TestCase
{
/** @return array<string, array{mixed}> */
public static function noRowShapes(): array
{
return [
'null' => [null],
'false (PDO)' => [false],
'empty array' => [[]],
];
}

/**
* Test basic function of RecordExists (no exclusion)
*
Expand Down Expand Up @@ -138,17 +149,18 @@ public function excludeWithStringNoRecord()
}

/**
* PDO reports "no rows" as false rather than null; that must still count as no record.
* Every shape a driver uses for "no row" must count as no record.
*
* @throws Exception
*/
#[Test]
public function noRowsReportedAsFalseFindsNoRecord(): void
#[DataProvider('noRowShapes')]
public function noRowShapeFindsNoRecord(mixed $current): void
{
$validator = new NoRecordExists([
'table' => 'users',
'field' => 'field1',
'adapter' => $this->getMockNoResult(false),
'adapter' => $this->getMockNoResult($current),
]);
static::assertTrue($validator->isValid('nosuchvalue'));
}
Expand Down
32 changes: 30 additions & 2 deletions test/unit/RecordExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpDb\Sql\TableIdentifier;
use PhpDb\Validator\RecordExists;
use PhpDbTestAsset\Validator\TrustingSql92Platform;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
Expand All @@ -27,6 +28,16 @@
#[Group('unit')]
final class RecordExistsTest extends TestCase
{
/** @return array<string, array{mixed}> */
public static function noRowShapes(): array
{
return [
'null' => [null],
'false (PDO)' => [false],
'empty array' => [[]],
];
}

/**
* Test basic function of RecordExists (no exclusion)
*
Expand Down Expand Up @@ -214,6 +225,23 @@ public function getSelectWithSameValidatorTwice(): void
);
}

/**
* Every shape a driver uses for "no row" must count as no record.
*
* @throws Exception
*/
#[Test]
#[DataProvider('noRowShapes')]
public function noRowShapeFindsNoRecord(mixed $current): void
{
$validator = new RecordExists([
'table' => 'users',
'field' => 'field1',
'adapter' => $this->getMockNoResult($current),
]);
static::assertFalse($validator->isValid('nosuchvalue'));
}

/**
* @throws Exception
*/
Expand Down Expand Up @@ -358,13 +386,13 @@ protected function getMockHasResult(): Adapter
*
* @throws Exception
*/
protected function getMockNoResult(): Adapter
protected function getMockNoResult(mixed $current = null): Adapter
{
// mock the adapter, driver, and parts
$mockConnection = $this->createMock(ConnectionInterface::class);

$mockNoResult = $this->createMock(ResultInterface::class);
$mockNoResult->method('current')->willReturn(null);
$mockNoResult->method('current')->willReturn($current);

$mockNoResultStatement = $this->createMock(StatementInterface::class);
$mockNoResultStatement->method('execute')->willReturn($mockNoResult);
Expand Down
Loading