Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Nov 19, 2021
1 parent c4fb047 commit 44f46ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ class Report
/** @var array<int, Line> */
protected array $lines;

public static function fromJson(string $json): Report
{
$properties = json_decode($json, true);

$lines = array_map(
fn(array $lineProperties) => new Line(...$lineProperties),
$properties['lines'],
);

return new static(
finishedAt: new DateTime($properties['finishedAt']),
lines: $lines,
);
}

/**
* @param \DateTimeInterface|null $finishedAt
* @param array<int, Line> $lines
Expand All @@ -23,9 +38,11 @@ public function __construct(DateTimeInterface $finishedAt = null, array $lines =
$this->lines = $lines;
}

public function addLine(Line $lines)
public function addLine(Line $line): self
{
$this->lines[] = $lines;
$this->lines[] = $line;

return $this;
}

public function toJson(): string
Expand Down
21 changes: 17 additions & 4 deletions tests/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
use function Spatie\Snapshots\assertMatchesSnapshot;

it('can create create report', function () {
$report = getReport();

assertMatchesSnapshot($report->toJson());
});

it('can be created from json', function() {
$json = getReport()->toJson();

$newReport = Report::fromJson($json);

expect($newReport->toJson())->toBe($json);
});

function getReport(): Report
{
$lines = [
new Line(
'name',
Expand All @@ -14,10 +29,8 @@
)
];

$report = new Report(
return new Report(
finishedAt: new DateTimeImmutable('2001-01-01 00:00:00'),
lines: $lines,
);

assertMatchesSnapshot($report->toJson());
});
}

0 comments on commit 44f46ba

Please sign in to comment.