-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cfaf27
commit f4b916d
Showing
13 changed files
with
292 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Domain\Ranges; | ||
|
||
class CurrentMonth extends Range | ||
{ | ||
public function start() | ||
{ | ||
return now()->startOfMonth()->format("Y-m-d"); | ||
} | ||
|
||
public function end() | ||
{ | ||
return now()->endOfMonth()->format("Y-m-d"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Domain\Ranges; | ||
|
||
class CurrentYear extends Range | ||
{ | ||
public function start() | ||
{ | ||
return now()->startOfYear()->format("Y-m-d"); | ||
} | ||
|
||
public function end() | ||
{ | ||
return now()->endOfYear()->format("Y-m-d"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Domain\Ranges; | ||
|
||
class LastMonth extends Range | ||
{ | ||
public function start() | ||
{ | ||
return now()->subMonth()->startOfMonth()->format("Y-m-d"); | ||
} | ||
|
||
public function end() | ||
{ | ||
return now()->subMonth()->endOfMonth()->format("Y-m-d"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Domain\Ranges; | ||
|
||
class LastYear extends Range | ||
{ | ||
public function start() | ||
{ | ||
return now()->subYear()->startOfYear()->format("Y-m-d"); | ||
} | ||
|
||
public function end() | ||
{ | ||
return now()->subYear()->endOfYear()->format("Y-m-d"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Domain\Ranges; | ||
|
||
use JsonSerializable; | ||
use Illuminate\Support\Str; | ||
|
||
abstract class Range implements JsonSerializable | ||
{ | ||
protected $key; | ||
protected $name; | ||
protected $start; | ||
protected $end; | ||
|
||
public function humanizedName() | ||
{ | ||
return Str::title(Str::snake(class_basename(get_class($this)), ' ')); | ||
} | ||
|
||
public function key() | ||
{ | ||
return Str::slug($this->name(), '-', null); | ||
} | ||
|
||
public function name() | ||
{ | ||
return $this->name ?: $this->humanizedName(); | ||
} | ||
|
||
abstract public function start(); | ||
|
||
abstract public function end(); | ||
|
||
/** | ||
* Prepare the element for JSON serialization. | ||
* | ||
* @return array | ||
*/ | ||
public function jsonSerialize() | ||
{ | ||
return [ | ||
'key' => $this->key(), | ||
'name' => $this->name(), | ||
'start' => $this->start(), | ||
'end' => $this->end(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Carbon\Carbon; | ||
use Tests\TestCase; | ||
use App\Domain\Ranges\CurrentMonth; | ||
|
||
class CurrentMonthTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_has_corrent_json_serializeable() | ||
{ | ||
// mock app date | ||
Carbon::setTestNow(Carbon::create(2021, 1, 18)); | ||
|
||
$sut = new CurrentMonth; | ||
|
||
$this->assertEquals([ | ||
'key' => 'current-month', | ||
'name' => 'Current Month', | ||
'start' => '2021-01-01', | ||
'end' => '2021-01-31', | ||
], $sut->jsonSerialize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Carbon\Carbon; | ||
use Tests\TestCase; | ||
use App\Domain\Ranges\CurrentYear; | ||
|
||
class CurrentYearTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_has_corrent_json_serializeable() | ||
{ | ||
// mock app date | ||
Carbon::setTestNow(Carbon::create(2021, 1, 18)); | ||
|
||
$sut = new CurrentYear; | ||
|
||
$this->assertEquals([ | ||
'key' => 'current-year', | ||
'name' => 'Current Year', | ||
'start' => '2021-01-01', | ||
'end' => '2021-12-31', | ||
], $sut->jsonSerialize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Carbon\Carbon; | ||
use Tests\TestCase; | ||
use App\Domain\Ranges\LastMonth; | ||
|
||
class LastMonthTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_has_corrent_json_serializeable() | ||
{ | ||
// mock app date | ||
Carbon::setTestNow(Carbon::create(2021, 1, 18)); | ||
|
||
$sut = new LastMonth; | ||
|
||
$this->assertEquals([ | ||
'key' => 'last-month', | ||
'name' => 'Last Month', | ||
'start' => '2020-12-01', | ||
'end' => '2020-12-31', | ||
], $sut->jsonSerialize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Carbon\Carbon; | ||
use Tests\TestCase; | ||
use App\Domain\Ranges\LastYear; | ||
|
||
class LastYearTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_has_corrent_json_serializeable() | ||
{ | ||
// mock app date | ||
Carbon::setTestNow(Carbon::create(2021, 5, 18)); | ||
|
||
$sut = new LastYear; | ||
|
||
$this->assertEquals([ | ||
'key' => 'last-year', | ||
'name' => 'Last Year', | ||
'start' => '2020-01-01', | ||
'end' => '2020-12-31', | ||
], $sut->jsonSerialize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Tests\Unit; | ||
|
||
use Tests\TestCase; | ||
use App\Domain\Ranges\Range; | ||
|
||
class RangeTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_is_abstract_class() | ||
{ | ||
$this->expectException(\Error::class); | ||
|
||
new Range; | ||
} | ||
|
||
/** @test */ | ||
public function it_has_default_name() | ||
{ | ||
$sut = new FakeRange; | ||
|
||
$this->assertEquals('Fake Range', $sut->name()); | ||
} | ||
|
||
/** @test */ | ||
public function it_has_default_key() | ||
{ | ||
$sut = new FakeRange; | ||
|
||
$this->assertEquals('fake-range', $sut->key()); | ||
} | ||
|
||
/** @test */ | ||
public function it_is_json_serializeable() | ||
{ | ||
$sut = new FakeRange; | ||
|
||
$this->assertEquals([ | ||
'key' => 'fake-range', | ||
'name' => 'Fake Range', | ||
'start' => 'startTime', | ||
'end' => 'endTime' | ||
], $sut->jsonSerialize()); | ||
} | ||
} | ||
|
||
|
||
class FakeRange extends Range | ||
{ | ||
public function start() { | ||
return "startTime"; | ||
} | ||
|
||
public function end() { | ||
return "endTime"; | ||
} | ||
} |