Skip to content

Commit

Permalink
Add date unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 20, 2021
1 parent 0c49286 commit 88e886c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Common/Date.php
Expand Up @@ -18,6 +18,13 @@ private function __construct(DateTimeImmutable $start, DateTimeImmutable|null $e
$this->end = $end;
}

public static function create(
DateTimeImmutable $start,
DateTimeImmutable $end = null,
): self {
return new self($start, $end);
}

/**
* @param DateJson $array
*
Expand Down Expand Up @@ -53,4 +60,19 @@ public function isRange(): bool
{
return $this->end !== null;
}

public function withStart(DateTimeImmutable $start): self
{
return new self($start, $this->end);
}

public function withEnd(DateTimeImmutable $end): self
{
return new self($this->start, $end);
}

public function withoutEnd(): self
{
return new self($this->start, null);
}
}
72 changes: 72 additions & 0 deletions tests/Unit/Common/DateTest.php
@@ -0,0 +1,72 @@
<?php

namespace Notion\Test\Common;

use DateTimeImmutable;
use Notion\Common\Date;
use PHPUnit\Framework\TestCase;

class DateTest extends TestCase
{
public function test_array_conversion(): void
{
$array = [
"start" => "2021-01-01",
"end" => "2021-12-31",
];

$date = Date::fromArray($array);
$this->assertEquals($array, $date->toArray());
}

public function test_create_date(): void
{
$start = new DateTimeImmutable("2021-01-01");
$date = Date::create($start);

$this->assertSame($start, $date->start());
$this->assertNull($date->end());
$this->assertFalse($date->isRange());
}

public function test_create_range(): void
{
$start = new DateTimeImmutable("2021-01-01");
$end = new DateTimeImmutable("2021-12-31");
$date = Date::create($start, $end);

$this->assertSame($start, $date->start());
$this->assertSame($end, $date->end());
$this->assertTrue($date->isRange());
}

public function test_change_start(): void
{
$oldStart = new DateTimeImmutable("2021-01-01");
$newStart = new DateTimeImmutable("2022-01-01");

$date = Date::create($oldStart)->withStart($newStart);

$this->assertSame($newStart, $date->start());
}

public function test_change_end(): void
{
$start = new DateTimeImmutable("2021-01-01");
$end = new DateTimeImmutable("2022-01-01");

$date = Date::create($start)->withEnd($end);

$this->assertSame($end, $date->end());
}

public function test_remove_end(): void
{
$start = new DateTimeImmutable("2021-01-01");
$end = new DateTimeImmutable("2021-12-31");
$date = Date::create($start, $end)->withoutEnd();

$this->assertNull($date->end());
$this->assertFalse($date->isRange());
}
}

0 comments on commit 88e886c

Please sign in to comment.