Skip to content

Commit 572642d

Browse files
committed
Added output value tests. Added cast tests. Fixed value casting
1 parent 54b82f4 commit 572642d

File tree

4 files changed

+112
-9
lines changed

4 files changed

+112
-9
lines changed

src/Casts/TimezonedDatetime.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function __construct(?string $format = null)
3737
*/
3838
public function get($model, $key, $value, $attributes)
3939
{
40-
return Timezone::date($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model));
40+
$original = Timezone::store($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model));
41+
42+
return Timezone::date($original);
4143
}
4244

4345
/**
@@ -51,8 +53,9 @@ public function get($model, $key, $value, $attributes)
5153
*/
5254
public function set($model, $key, $value, $attributes)
5355
{
54-
return Timezone::store($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model))
55-
->format($this->format ?? $model->getDateFormat());
56+
$requested = Timezone::date($value, fn($raw, $tz) => $this->asDateTime($raw, $tz, $model));
57+
58+
return Timezone::store($requested)->format($this->format ?? $model->getDateFormat());
5659
}
5760

5861
/**

tests/CastTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
use Whitecube\LaravelTimezones\Casts\TimezonedDatetime;
4+
5+
it('can access UTC database date with application timezone', function() {
6+
setupFacade();
7+
8+
$cast = new TimezonedDatetime();
9+
10+
$input = '2022-12-15 09:00:00';
11+
12+
$output = $cast->get(fakeModel(), 'id', $input, []);
13+
14+
expect($output->getTimezone()->getName())->toBe('Europe/Brussels');
15+
expect($output->format('Y-m-d H:i:s'))->toBe('2022-12-15 10:00:00');
16+
});
17+
18+
it('can access UTC database date with application timezone and specific format', function() {
19+
setupFacade();
20+
21+
$cast = new TimezonedDatetime('d/m/Y H:i');
22+
23+
$input = '15/12/2022 09:00';
24+
25+
$output = $cast->get(fakeModel(), 'id', $input, []);
26+
27+
expect($output->getTimezone()->getName())->toBe('Europe/Brussels');
28+
expect($output->format('Y-m-d H:i:s'))->toBe('2022-12-15 10:00:00');
29+
});
30+
31+
it('can mutate application timezone datetime string to UTC database date string', function() {
32+
setupFacade();
33+
34+
$cast = new TimezonedDatetime();
35+
36+
$input = '2022-12-15 10:00:00';
37+
38+
$output = $cast->set(fakeModel(), 'id', $input, []);
39+
40+
expect($output)->toBe('2022-12-15 09:00:00');
41+
});
42+
43+
it('can mutate application timezone date instance to UTC database date string', function() {
44+
setupFacade();
45+
46+
$cast = new TimezonedDatetime();
47+
48+
$input = new \Carbon\Carbon('2022-12-15 10:00:00', 'Europe/Brussels');
49+
50+
$output = $cast->set(fakeModel(), 'id', $input, []);
51+
52+
expect($output)->toBe('2022-12-15 09:00:00');
53+
});
54+
55+
it('can mutate UTC date instance to UTC database date string', function() {
56+
setupFacade();
57+
58+
$cast = new TimezonedDatetime();
59+
60+
$input = new \Carbon\Carbon('2022-12-15 09:00:00', 'UTC');
61+
62+
$output = $cast->set(fakeModel(), 'id', $input, []);
63+
64+
expect($output)->toBe('2022-12-15 09:00:00');
65+
});
66+
67+
it('can mutate date instance with exotic timezone to UTC database date string', function() {
68+
setupFacade();
69+
70+
$cast = new TimezonedDatetime();
71+
72+
$input = new \Carbon\Carbon('2022-12-15 04:00:00', 'America/Toronto');
73+
74+
$output = $cast->set(fakeModel(), 'id', $input, []);
75+
76+
expect($output)->toBe('2022-12-15 09:00:00');
77+
});

tests/Pest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
use Whitecube\LaravelTimezones\Timezone;
4+
use Whitecube\LaravelTimezones\Facades\Timezone as Facade;
5+
use Illuminate\Database\Eloquent\Model;
6+
37
/*
48
|--------------------------------------------------------------------------
59
| Test Case
@@ -39,7 +43,20 @@
3943
|
4044
*/
4145

42-
// function something()
43-
// {
44-
// // ..
45-
// }
46+
function setupFacade(string $storage = 'UTC', string $current = 'Europe/Brussels')
47+
{
48+
$instance = new Timezone($storage);
49+
$instance->set($current);
50+
51+
Facade::swap($instance);
52+
}
53+
54+
function fakeModel()
55+
{
56+
return new class() extends Model {
57+
public function getDateFormat()
58+
{
59+
return 'Y-m-d H:i:s';
60+
}
61+
};
62+
}

tests/TimezoneSingletonTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@
5252
$instance->set('Europe/Brussels');
5353

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

5858
expect($string)->toBeInstanceOf(CarbonInterface::class);
5959
expect($string->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
60+
expect($string->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
6061
expect($date)->toBeInstanceOf(CarbonInterface::class);
6162
expect($date->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
63+
expect($date->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
6264
expect($custom)->toBeInstanceOf(CarbonImmutable::class);
6365
expect($custom->getTimezone()->getName() ?? null)->toBe('Europe/Brussels');
66+
expect($custom->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
6467
});
6568

6669

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

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

7578
expect($string)->toBeInstanceOf(CarbonInterface::class);
7679
expect($string->getTimezone()->getName() ?? null)->toBe('UTC');
80+
expect($string->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
7781
expect($date)->toBeInstanceOf(CarbonInterface::class);
7882
expect($date->getTimezone()->getName() ?? null)->toBe('UTC');
83+
expect($date->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
7984
expect($custom)->toBeInstanceOf(CarbonImmutable::class);
8085
expect($custom->getTimezone()->getName() ?? null)->toBe('UTC');
86+
expect($custom->format('Y-m-d H:i:s'))->toBe('1993-03-16 03:00:00');
8187
});
8288

0 commit comments

Comments
 (0)