Skip to content

Commit

Permalink
Merge pull request #13 from LelineaITManufaktur/nullable-datetime-wil…
Browse files Browse the repository at this point in the history
…dcard

Add wildcard type for date time or null
  • Loading branch information
hnnnglmbrg authored Nov 19, 2019
2 parents e7caf09 + c567518 commit cdae9f3
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ The library currently supports the following wildcards:
- IntegerWildcard
- UuidWildcard
- DateTimeWildcard
- DateTimeOrNullWildcard
- StringWildcard
42 changes: 42 additions & 0 deletions src/Wildcard/BaseDateTimeWildcard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace KigaRoo\SnapshotTesting\Wildcard;

use DateTime;
use Throwable;

abstract class BaseDateTimeWildcard implements Wildcard
{
/** @var string */
private $path;

/** @var string */
private $format;

public function __construct(string $path, string $format = DateTime::ATOM)
{
$this->path = $path;
$this->format = $format;
}

public function atPath() : string
{
return $this->path;
}

/**
* @param mixed $mixed
*/
public function match($mixed) : bool
{
try {
$dateTime = DateTime::createFromFormat($this->format, $mixed);

return $dateTime !== false;
} catch (Throwable $exception) {
return false;
}
}
}
20 changes: 20 additions & 0 deletions src/Wildcard/DateTimeOrNullWildcard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace KigaRoo\SnapshotTesting\Wildcard;

final class DateTimeOrNullWildcard extends BaseDateTimeWildcard
{
/**
* @param mixed $mixed
*/
public function match($mixed) : bool
{
if ($mixed === null) {
return true;
}

return parent::match($mixed);
}
}
35 changes: 1 addition & 34 deletions src/Wildcard/DateTimeWildcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,6 @@

namespace KigaRoo\SnapshotTesting\Wildcard;

use DateTime;
use Throwable;

final class DateTimeWildcard implements Wildcard
final class DateTimeWildcard extends BaseDateTimeWildcard
{
/** @var string */
private $path;

/** @var string */
private $format;

public function __construct(string $path, string $format = DateTime::ATOM)
{
$this->path = $path;
$this->format = $format;
}

public function atPath() : string
{
return $this->path;
}

/**
* @param mixed $mixed
*/
public function match($mixed) : bool
{
try {
$dateTime = DateTime::createFromFormat($this->format, $mixed);

return $dateTime !== false;
} catch (Throwable $exception) {
return false;
}
}
}
28 changes: 17 additions & 11 deletions tests/unit/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use KigaRoo\SnapshotTesting\Exception\InvalidMappingPath;
use KigaRoo\SnapshotTesting\MatchesSnapshots;
use KigaRoo\SnapshotTesting\Wildcard\DateTimeOrNullWildcard;
use KigaRoo\SnapshotTesting\Wildcard\DateTimeWildcard;
use KigaRoo\SnapshotTesting\Wildcard\IntegerWildcard;
use KigaRoo\SnapshotTesting\Wildcard\UuidWildcard;
Expand All @@ -26,17 +27,17 @@ public function testJson() : void
'id' => 'b84c9b7f-1ebb-49b6-9d18-4305932b2dd1',
'nested1' => [
0 => [
'nested2' => [
'nested2' => [
0 => ['int' => 2],
1 => ['int' => 3],
]
],
],
1 => [
'nested2' => [
'nested2' => [
0 => ['int' => 4],
1 => ['int' => 5],
]
]
],
],
],
'multiple' => [
0 => 'a',
Expand Down Expand Up @@ -68,6 +69,10 @@ public function testJson() : void
'01.01.2011 11:11',
],
],
'dateTimeOrNull' => [
'2019-07-10T12:50:20+00:00',
null,
],
],
]
);
Expand All @@ -81,6 +86,7 @@ public function testJson() : void
new IntegerWildcard('tests.arrays[*][2]'),
new DateTimeWildcard('tests.dateTimes[0][*]'),
new DateTimeWildcard('tests.dateTimes[1][*]', 'd.m.Y H:i'),
new DateTimeOrNullWildcard('tests.dateTimeOrNull[*]'),
];

$this->assertMatchesJsonSnapshot($data, $wildcards);
Expand All @@ -98,17 +104,17 @@ public function testFailOnInvalidMapping(Wildcard $wildcard) : void
'id' => 'b84c9b7f-1ebb-49b6-9d18-4305932b2dd1',
'nested1' => [
0 => [
'nested2' => [
'nested2' => [
0 => ['int' => 2],
1 => ['int' => 3],
]
],
],
1 => [
'nested2' => [
'nested2' => [
0 => ['int' => 4],
1 => ['int' => 5],
]
]
],
],
],
'multiple' => [
0 => 'a',
Expand Down Expand Up @@ -147,7 +153,7 @@ public function testFailOnInvalidMapping(Wildcard $wildcard) : void
$this->assertMatchesJsonSnapshot($data, [$wildcard]);
}

public function provideFail(): array
public function provideFail() : array
{
return [
[new IntegerWildcard('tests.nested0[*].nested2[*].int')],
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/__snapshots__/JsonTest__testJson__1.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
"02.02.2012 12:12",
"01.01.2011 11:11"
]
],
"dateTimeOrNull": [
"2019-07-10T12:50:20+00:00",
null
]
}
}

0 comments on commit cdae9f3

Please sign in to comment.