-
-
Notifications
You must be signed in to change notification settings - Fork 23
Add failing tests and TODOs for doctrine types #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use Doctrine\DBAL\Cache\QueryCacheProfile; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Types\Types; | ||
use function PHPStan\Testing\assertType; | ||
use staabm\PHPStanDba\Tests\Fixture\StringableObject; | ||
|
||
|
@@ -218,4 +219,49 @@ public function dateParameter(Connection $conn) | |
$fetchResult = $conn->fetchOne($query, [date('Y-m-d', strtotime('-3hour'))]); | ||
assertType('int|false', $fetchResult); | ||
} | ||
|
||
public function customTypeParameters(Connection $conn, int $adaid) | ||
{ | ||
$stmt = $conn->prepare('SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt'); | ||
$result = $stmt->execute(['dt' => new \DateTime()], ['dt' => Types::DATETIME_MUTABLE]); | ||
assertType('Doctrine\DBAL\Result', $result); | ||
|
||
$stmt = $conn->prepare('SELECT count(*) AS c FROM typemix WHERE c_datetime=:last_dt'); | ||
$result = $stmt->execute(['dt' => new \DateTime()], ['dt' => Types::DATETIME_IMMUTABLE]); | ||
Comment on lines
+229
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this test also use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DBAL supports this on all connection methods it seems https://github.com/doctrine/dbal/blob/3.3.x/src/Connection.php#L529 Also on quote https://github.com/doctrine/dbal/blob/3.3.x/src/Connection.php#L811 So I guess ideally this would be supported on all the things, but executeStatement/executeQuery would be the must-haves IMO as it's the lowest common denominator and the rest is kinda decorating this. |
||
// TODO should probably fail as DateTime is passed to a DATETIME_IMMUTABLE type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think doctrine will fail though, the distinction between datetime/datetime_immutable is only made there when deserializing data from DB back into PHP AFAIK, so maybe we can let it go through.. |
||
assertType('Doctrine\DBAL\Result', $result); | ||
} | ||
|
||
/** | ||
* @param array<int> $ids | ||
* @param array<string> $vals | ||
*/ | ||
public function boundArrays(Connection $conn, array $ids, array $vals) | ||
{ | ||
$result = $conn->executeQuery( | ||
'SELECT count(*) AS c FROM ada WHERE adaid IN (?)', | ||
[$ids], | ||
[\Doctrine\DBAL\Connection::PARAM_INT_ARRAY] | ||
); | ||
assertType('Doctrine\DBAL\Result<array{c: int, 0: int}>', $result); | ||
|
||
$result = $conn->executeQuery( | ||
'SELECT count(*) AS c FROM ada WHERE email IN (?)', | ||
[$vals], | ||
[\Doctrine\DBAL\Connection::PARAM_STR_ARRAY] | ||
); | ||
assertType('Doctrine\DBAL\Result<array{c: int, 0: int}>', $result); | ||
|
||
$result = $conn->executeQuery( | ||
'SELECT count(*) AS c FROM ada WHERE adaid IN (?)', | ||
[$vals], // TODO should error as $vals is not int array | ||
[\Doctrine\DBAL\Connection::PARAM_INT_ARRAY] | ||
); | ||
|
||
$result = $conn->executeQuery( | ||
'SELECT count(*) AS c FROM ada WHERE email IN (?)', | ||
[$ids], // TODO should error as $ids is not str array | ||
[\Doctrine\DBAL\Connection::PARAM_STR_ARRAY] | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does doctrine allow to pass in a
DateTime
object without also giving a parameter-type as a 2nd arg?or should phpstan-dba error when arg1 is
DateTime
and arg2 is either missing or not equal toTypes::DATETIME_MUTABLE
?edit: I see, there are a few types arround dates in doctrine: https://github.com/doctrine/dbal/blob/007223cfe9dc5cf37420a6a8d8ecd48d520b69d4/src/Types/Types.php#L18-L24
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if it lets you do it without the param type, but I'd think not.