Skip to content

Commit 7d9af08

Browse files
authored
Merge pull request #484 from cakephp/add-rfc7231
Add toRfc7231String() helper
2 parents a74745c + 0a4b94c commit 7d9af08

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"require-dev": {
3232
"cakephp/cakephp-codesniffer": "^5.0",
33-
"phpunit/phpunit": "^10.1.0 || ^11.1.3"
33+
"phpunit/phpunit": "^10.5.58 || ^11.1.3"
3434
},
3535
"provide": {
3636
"psr/clock-implementation": "1.0"

phpunit.xml.dist

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
44
bootstrap="tests/bootstrap.php"
5-
colors="true">
5+
colors="true"
6+
failOnDeprecation="true"
7+
displayDetailsOnTestsThatTriggerDeprecations="true">
68
<testsuites>
79
<testsuite name="Chronos Test Suite">
810
<directory>tests</directory>
@@ -13,4 +15,9 @@
1315
<directory suffix=".php">src/</directory>
1416
</include>
1517
</source>
18+
19+
<php>
20+
<!-- E_ALL (32767) -->
21+
<ini name="error_reporting" value="32767"/>
22+
</php>
1623
</phpunit>

src/Chronos.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,17 @@ public function diffForHumans(?DateTimeInterface $other = null, bool $absolute =
25872587
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
25882588
}
25892589

2590+
/**
2591+
* Converts the time zone to UTC and returns a string in RFC7231 format.
2592+
* This replaced the deprecated and broken ``DATE_RFC7231`` formatting constant.
2593+
*
2594+
* @return string
2595+
*/
2596+
public function toRfc7231String(): string
2597+
{
2598+
return $this->setTimezone('UTC')->format('D, d M Y H:i:s \G\M\T');
2599+
}
2600+
25902601
/**
25912602
* Returns a DateTimeImmutable instance
25922603
*

tests/TestCase/DateTime/StringsTest.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ public function testToUnixString()
160160
$this->assertSame('1639224001', $time->toUnixString());
161161
}
162162

163+
public function testToRfc7231String()
164+
{
165+
$time = Chronos::parse('2014-04-20 08:00:00', 'America/Toronto');
166+
$this->assertSame('Sun, 20 Apr 2014 12:00:00 GMT', $time->toRfc7231String());
167+
}
168+
163169
/**
164170
* Provides values and expectations for the toQuarter method
165171
*
@@ -171,10 +177,6 @@ public static function toQuarterProvider()
171177
['2007-12-25', 4],
172178
['2007-9-25', 3],
173179
['2007-3-25', 1],
174-
['2007-3-25', ['2007-01-01', '2007-03-31'], true],
175-
['2007-5-25', ['2007-04-01', '2007-06-30'], true],
176-
['2007-8-25', ['2007-07-01', '2007-09-30'], true],
177-
['2007-12-25', ['2007-10-01', '2007-12-31'], true],
178180
];
179181
}
180182

@@ -186,7 +188,26 @@ public static function toQuarterProvider()
186188
#[DataProvider('toQuarterProvider')]
187189
public function testToQuarter($date, $expected, $range = false)
188190
{
189-
$this->assertSame($expected, (new Chronos($date))->toQuarter($range));
191+
$this->assertSame($expected, (new Chronos($date))->toQuarter());
192+
}
193+
194+
public static function toQuarterRangeProvider()
195+
{
196+
return [
197+
['2007-3-25', ['2007-01-01', '2007-03-31']],
198+
['2007-5-25', ['2007-04-01', '2007-06-30']],
199+
['2007-8-25', ['2007-07-01', '2007-09-30']],
200+
['2007-12-25', ['2007-10-01', '2007-12-31']],
201+
];
202+
}
203+
204+
#[DataProvider('toQuarterRangeProvider')]
205+
public function testToQuarterRange($date, $expected)
206+
{
207+
$this->assertSame($expected, (new Chronos($date))->toQuarterRange());
208+
$this->deprecated(function() use ($date, $expected) {
209+
$this->assertSame($expected, (new Chronos($date))->toQuarter(true));
210+
});
190211
}
191212

192213
/**

0 commit comments

Comments
 (0)