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
11 changes: 6 additions & 5 deletions src/AbstractDbValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@
/**
* Error constants
*/
public const ERROR_NO_RECORD_FOUND = 'noRecordFound';
public const ERROR_RECORD_FOUND = 'recordFound';
public const string ERROR_NO_RECORD_FOUND = 'noRecordFound';
public const string ERROR_RECORD_FOUND = 'recordFound';

/**
* Message templates.
*
* @var array<string, string>
*/
// @mago-ignore analysis:incompatible-property-type
public array $messageTemplates = [
self::ERROR_NO_RECORD_FOUND => 'No record matching the input was found',
self::ERROR_RECORD_FOUND => 'A record matching the input was found',
Expand Down Expand Up @@ -165,7 +164,7 @@
/**
* Run query and returns matches, or null if no matches are found.
*
* @return mixed when matches are found.
* @return mixed when matches are found; null when there are none (PDO reports no rows as false).
*/
protected function query(mixed $value): mixed
{
Expand All @@ -180,6 +179,8 @@
$parameters['where1'] = $value;
}

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

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 | lowest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 | locked

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 | lowest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 | latest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 | locked

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 | latest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 | latest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 | locked

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 | latest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 | lowest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 | locked

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 | locked

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 | latest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.5 | locked

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 | lowest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 | latest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 | lowest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

Check warning on line 182 in src/AbstractDbValidator.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 | lowest

mixed-assignment

Assigning `mixed` type to a variable may lead to unexpected behavior. >Assigning `mixed` type here. Using `mixed` can lead to runtime errors if the variable is used in a way that assumes a specific type. Help: Consider using a more specific type to avoid potential issues.

return false === $row ? null : $row;
}
}
20 changes: 18 additions & 2 deletions test/unit/NoRecordExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ public function excludeWithStringNoRecord()
static::assertTrue($validator->isValid('nosuchvalue'));
}

/**
* PDO reports "no rows" as false rather than null; that must still count as no record.
*
* @throws Exception
*/
#[Test]
public function noRowsReportedAsFalseFindsNoRecord(): void
{
$validator = new NoRecordExists([
'table' => 'users',
'field' => 'field1',
'adapter' => $this->getMockNoResult(false),
]);
static::assertTrue($validator->isValid('nosuchvalue'));
}

/**
* @throws Exception
*/
Expand Down Expand Up @@ -241,13 +257,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