Skip to content

Commit

Permalink
Added output value tests. Added cast tests. Fixed value casting
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvandenbos committed Dec 15, 2022
1 parent 54b82f4 commit 572642d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/Casts/TimezonedDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function __construct(?string $format = null)
*/
public function get($model, $key, $value, $attributes)
{
return Timezone::date($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model));
$original = Timezone::store($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model));

return Timezone::date($original);
}

/**
Expand All @@ -51,8 +53,9 @@ public function get($model, $key, $value, $attributes)
*/
public function set($model, $key, $value, $attributes)
{
return Timezone::store($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model))
->format($this->format ?? $model->getDateFormat());
$requested = Timezone::date($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model));

return Timezone::store($requested)->format($this->format ?? $model->getDateFormat());
}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/CastTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

use Whitecube\LaravelTimezones\Casts\TimezonedDatetime;

it('can access UTC database date with application timezone', function() {
setupFacade();

$cast = new TimezonedDatetime();

$input = '2022-12-15 09:00:00';

$output = $cast->get(fakeModel(), 'id', $input, []);

expect($output->getTimezone()->getName())->toBe('Europe/Brussels');
expect($output->format('Y-m-d H:i:s'))->toBe('2022-12-15 10:00:00');
});

it('can access UTC database date with application timezone and specific format', function() {
setupFacade();

$cast = new TimezonedDatetime('d/m/Y H:i');

$input = '15/12/2022 09:00';

$output = $cast->get(fakeModel(), 'id', $input, []);

expect($output->getTimezone()->getName())->toBe('Europe/Brussels');
expect($output->format('Y-m-d H:i:s'))->toBe('2022-12-15 10:00:00');
});

it('can mutate application timezone datetime string to UTC database date string', function() {
setupFacade();

$cast = new TimezonedDatetime();

$input = '2022-12-15 10:00:00';

$output = $cast->set(fakeModel(), 'id', $input, []);

expect($output)->toBe('2022-12-15 09:00:00');
});

it('can mutate application timezone date instance to UTC database date string', function() {
setupFacade();

$cast = new TimezonedDatetime();

$input = new \Carbon\Carbon('2022-12-15 10:00:00', 'Europe/Brussels');

$output = $cast->set(fakeModel(), 'id', $input, []);

expect($output)->toBe('2022-12-15 09:00:00');
});

it('can mutate UTC date instance to UTC database date string', function() {
setupFacade();

$cast = new TimezonedDatetime();

$input = new \Carbon\Carbon('2022-12-15 09:00:00', 'UTC');

$output = $cast->set(fakeModel(), 'id', $input, []);

expect($output)->toBe('2022-12-15 09:00:00');
});

it('can mutate date instance with exotic timezone to UTC database date string', function() {
setupFacade();

$cast = new TimezonedDatetime();

$input = new \Carbon\Carbon('2022-12-15 04:00:00', 'America/Toronto');

$output = $cast->set(fakeModel(), 'id', $input, []);

expect($output)->toBe('2022-12-15 09:00:00');
});
25 changes: 21 additions & 4 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Whitecube\LaravelTimezones\Timezone;
use Whitecube\LaravelTimezones\Facades\Timezone as Facade;
use Illuminate\Database\Eloquent\Model;

/*
|--------------------------------------------------------------------------
| Test Case
Expand Down Expand Up @@ -39,7 +43,20 @@
|
*/

// function something()
// {
// // ..
// }
function setupFacade(string $storage = 'UTC', string $current = 'Europe/Brussels')
{
$instance = new Timezone($storage);
$instance->set($current);

Facade::swap($instance);
}

function fakeModel()
{
return new class() extends Model {
public function getDateFormat()
{
return 'Y-m-d H:i:s';
}
};
}
10 changes: 8 additions & 2 deletions tests/TimezoneSingletonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@
$instance->set('Europe/Brussels');

$string = $instance->date('1993-03-16 03:00:00');
$date = $instance->date(new Carbon('1993-03-16 03:00:00', 'UTC'));
$date = $instance->date(new Carbon('1993-03-16 02:00:00', 'UTC'));
$custom = $instance->date('1993-03-16 03:00:00', fn($value, $tz) => new CarbonImmutable($value, $tz));

expect($string)->toBeInstanceOf(CarbonInterface::class);
expect($string->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
expect($string->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
expect($date)->toBeInstanceOf(CarbonInterface::class);
expect($date->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
expect($date->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
expect($custom)->toBeInstanceOf(CarbonImmutable::class);
expect($custom->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
expect($custom->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
});


Expand All @@ -69,14 +72,17 @@
$instance->setCurrent('Europe/Brussels');

$string = $instance->store('1993-03-16 03:00:00');
$date = $instance->store(new Carbon('1993-03-16 03:00:00', 'Europe/Brussels'));
$date = $instance->store(new Carbon('1993-03-16 04:00:00', 'Europe/Brussels'));
$custom = $instance->store('1993-03-16 03:00:00', fn($value, $tz) => new CarbonImmutable($value, $tz));

expect($string)->toBeInstanceOf(CarbonInterface::class);
expect($string->getTimezone()->getName() ?? null)->toBe('UTC');
expect($string->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
expect($date)->toBeInstanceOf(CarbonInterface::class);
expect($date->getTimezone()->getName() ?? null)->toBe('UTC');
expect($date->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
expect($custom)->toBeInstanceOf(CarbonImmutable::class);
expect($custom->getTimezone()->getName() ?? null)->toBe('UTC');
expect($custom->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
});

0 comments on commit 572642d

Please sign in to comment.