Skip to content
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

Strengthen tests for datetime mapping #44

Closed
romm opened this issue Dec 19, 2021 · 1 comment
Closed

Strengthen tests for datetime mapping #44

romm opened this issue Dec 19, 2021 · 1 comment
Labels
good first issue Good for newcomers

Comments

@romm
Copy link
Member

romm commented Dec 19, 2021

As stated by @keichinger in #40 (comment), the test-cases below can lead to false-positive, mainly because every case is mapping to the exact same datetime.

public function test_datetime_properties_are_converted_properly(): void
{
$dateTimeInterface = new DateTimeImmutable('@1356097062');
$dateTimeImmutable = new DateTimeImmutable('@1356097062');
$dateTimeFromTimestamp = 1356097062;
$dateTimeFromTimestampWithFormat = [
'datetime' => 1356097062,
'format' => 'U',
];
$dateTimeFromAtomFormat = '2012-12-21T13:37:42+00:00';
$dateTimeFromArray = [
'datetime' => '2012-12-21 13:37:42',
'format' => 'Y-m-d H:i:s',
];
$mysqlDate = '2012-12-21 13:37:42';
$pgsqlDate = '2012-12-21 13:37:42.123456';
try {
$result = $this->mapperBuilder->mapper()->map(AllDateTimeValues::class, [
'dateTimeInterface' => $dateTimeInterface,
'dateTimeImmutable' => $dateTimeImmutable,
'dateTimeFromTimestamp' => $dateTimeFromTimestamp,
'dateTimeFromTimestampWithFormat' => $dateTimeFromTimestampWithFormat,
'dateTimeFromAtomFormat' => $dateTimeFromAtomFormat,
'dateTimeFromArray' => $dateTimeFromArray,
'mysqlDate' => $mysqlDate,
'pgsqlDate' => $pgsqlDate,
]);
} catch (MappingError $error) {
$this->mappingFail($error);
}
self::assertInstanceOf(DateTimeImmutable::class, $result->dateTimeInterface);
self::assertEquals($dateTimeInterface, $result->dateTimeInterface);
self::assertEquals($dateTimeImmutable, $result->dateTimeImmutable);
self::assertEquals(new DateTimeImmutable("@$dateTimeFromTimestamp"), $result->dateTimeFromTimestamp);
self::assertEquals(new DateTimeImmutable("@{$dateTimeFromTimestampWithFormat['datetime']}"), $result->dateTimeFromTimestampWithFormat);
self::assertEquals(DateTimeImmutable::createFromFormat(DATE_ATOM, $dateTimeFromAtomFormat), $result->dateTimeFromAtomFormat);
self::assertEquals(DateTimeImmutable::createFromFormat($dateTimeFromArray['format'], $dateTimeFromArray['datetime']), $result->dateTimeFromArray);
self::assertEquals(DateTimeImmutable::createFromFormat(DateTimeObjectBuilder::DATE_MYSQL, $mysqlDate), $result->mysqlDate);
self::assertEquals(DateTimeImmutable::createFromFormat(DateTimeObjectBuilder::DATE_PGSQL, $pgsqlDate), $result->pgsqlDate);
}

Changing each case to a different datetime everytime would strengthen the tests and ensure the expected behaviour is correct.

@romm
Copy link
Member Author

romm commented Mar 3, 2022

Fixed in #84, thanks @Lucian-Olariu!

@romm romm closed this as completed Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant