Fixed inconsistency in calculating date difference between platforms#3108
Fixed inconsistency in calculating date difference between platforms#3108morozov merged 1 commit intodoctrine:masterfrom
Conversation
aa5e1c8 to
deba5e7
Compare
…mySelectSQL() For testing purposes and not only, it may be needed to evaluate an arbitrary SQL expression (see doctrine#3013, doctrine#3108). Instead of doing `str_replace()` on an expected string, one could specify the expression explicitly.
509ab71 to
3752823
Compare
| $row = array_change_key_case($row, CASE_LOWER); | ||
|
|
||
| $diff = (strtotime('2010-01-01') - strtotime(date('Y-m-d'))) / 3600 / 24; | ||
| self::assertEquals($diff, $row['diff'], "Date difference should be approx. ".$diff." days.", 1); |
There was a problem hiding this comment.
This "approx." and $round = 1 hide the existing issue. Basically, even if the expected and actual values are different by one, the test would still pass. The assertion is reworked in DateExpressionTest.
| class DateExpressionTest extends DbalFunctionalTestCase | ||
| { | ||
| /** | ||
| * @test |
There was a problem hiding this comment.
Imho we should keep it consistent and use testDifference or so.
| * @test | ||
| * @dataProvider dateDiffProvider | ||
| */ | ||
| public function diff(string $date1, string $date2, int $expected) |
| self::assertEquals($expected, $diff); | ||
| } | ||
|
|
||
| public static function dateDiffProvider() |
There was a problem hiding this comment.
It feels like a grey area — not that you're asking to add a return type but our current capabilities of specifying them. Adding array or iterable requires specifying a collection element type which is always a tuple which we don't have a type for. The closest compatible specification will be string[][]|int[][] which is re-e-eally not what this method is supposed to return.
There was a problem hiding this comment.
Well we can disable specifying array/iterable/collection types in tests/, but return type should still be in place...
There was a problem hiding this comment.
I think it's a good idea. The primary reason to require the specification is to see if the method is compatible with what the caller expects. But providers are not part of any interface, they are "magically" annotated methods which PHPUnit calls. So we shouldn't care that much.
| $date2->modify('midnight') | ||
| )->days; | ||
|
|
||
| return [ |
There was a problem hiding this comment.
I don't know. It won't save any intermediate variable which would collect what's supposed to be returned, so I'd leave it as is.
There was a problem hiding this comment.
i quite like it, it saves one nesting level and nested arrays of arrays (of arrays). But functionally it's irrelevant.
3752823 to
ede6aef
Compare
The SQLite and Oracle platforms produces a `ROUND()`'ed or `TRUNC()`'ated difference between the datetimes. However it should be the difference between dates.
ede6aef to
eb523ed
Compare
…mySelectSQL() For testing purposes and not only, it may be needed to evaluate an arbitrary SQL expression (see doctrine#3013, doctrine#3108). Instead of doing `str_replace()` on an expected string, one could specify the expression explicitly.
…mySelectSQL() For testing purposes and not only, it may be needed to evaluate an arbitrary SQL expression (see doctrine#3013, doctrine#3108). Instead of doing `str_replace()` on an expected string, one could specify the expression explicitly.
Original symptom: the test fails on SQLite depending on the current time and timezone:
Reason:
The test relies upon
date('Y-m-d')andAbstractPlatform:::getCurrentTimestampSQL()always producing dates in the same day. It's not true in case of SQLite whoseCURRENT_TIMESTAMPis always in UTC or in general if the DB timezone is different from the PHPs.Additional issue:
Reason:
The Oracle and SQLite platforms, instead of comparing days of the dates, compare the datetimes and round the result. Instead, they should extract days from the datetimes and compare them.