Skip to content

Commit 00a57d8

Browse files
authored
Daterange with times has quotes (#31)
* added quotes to stringify * remove autoformat * stringify * removed unused use * removed unused use
1 parent f47009b commit 00a57d8

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/Ranges/TimestampRange.php

+14
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ public function forSql(): string
3535
{
3636
return "'$this'::tsrange";
3737
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function __toString()
43+
{
44+
return sprintf(
45+
'%s%s,%s%s',
46+
$this->fromBound,
47+
$this->from ? "\"{$this->from}\"" : '',
48+
$this->to ? "\"{$this->to}\"" : '',
49+
$this->toBound
50+
);
51+
}
3852
}

tests/Feature/RangesCastingTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ public function it_casts_timestamp_range_column(): void
3737
$this->assertEquals($to, $model->timestamp_range->to()->toDateTimeString());
3838
}
3939

40+
/** @test */
41+
public function it_does_not_detect_changes_when_updating_with_same_timestamp_range(): void
42+
{
43+
$from = '2010-01-01 14:30:30';
44+
$to = '2010-01-01 15:30:30';
45+
$timestampRange = new TimestampRange($from, $to, '[', ']');
46+
$model = $this->createModel(
47+
[
48+
'timestamp_range' => $timestampRange,
49+
]
50+
);
51+
52+
$model = $model->fresh();
53+
$model->update(
54+
[
55+
'timestamp_range' => $timestampRange,
56+
]
57+
);
58+
59+
$this->assertEmpty($model->getChanges());
60+
}
61+
4062
/** @test */
4163
public function it_casts_timestamptz_range_column(): void
4264
{

tests/Unit/RangesSerializationTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ class RangesSerializationTest extends TestCase
1616
public function timestamp_range_serializes_correctly(): void
1717
{
1818
$range = new TimestampRange('2010-01-01 14:30:30', '2010-01-01 15:30:30', '[', ']');
19-
$this->assertEquals('[2010-01-01 14:30:30,2010-01-01 15:30:30]', (string) $range);
19+
$this->assertEquals('["2010-01-01 14:30:30","2010-01-01 15:30:30"]', (string) $range);
2020

2121
$range = new TimestampRange('2010-01-01 14:30:30', '2010-01-01 15:30:30', '(', ']');
22-
$this->assertEquals('(2010-01-01 14:30:30,2010-01-01 15:30:30]', (string) $range);
22+
$this->assertEquals('("2010-01-01 14:30:30","2010-01-01 15:30:30"]', (string) $range);
2323

2424
$range = new TimestampRange('2010-01-01 14:30:30', '2010-01-01 15:30:30', '(', ')');
25-
$this->assertEquals('(2010-01-01 14:30:30,2010-01-01 15:30:30)', (string) $range);
25+
$this->assertEquals('("2010-01-01 14:30:30","2010-01-01 15:30:30")', (string) $range);
2626

2727
$range = new TimestampRange(null, '2010-01-01 15:30:30', '[', ']');
28-
$this->assertEquals('[,2010-01-01 15:30:30]', (string) $range);
28+
$this->assertEquals('[,"2010-01-01 15:30:30"]', (string) $range);
2929

3030
$range = new TimestampRange('2010-01-01 14:30:30', null, '[', ']');
31-
$this->assertEquals('[2010-01-01 14:30:30,]', (string) $range);
31+
$this->assertEquals('["2010-01-01 14:30:30",]', (string) $range);
3232

3333
$range = new TimestampRange(null, null, '[', ']');
3434
$this->assertEquals('[,]', (string) $range);

0 commit comments

Comments
 (0)