Skip to content

Commit 4684bdd

Browse files
committed
breaking change to improve message date handling
1 parent e8f5e15 commit 4684bdd

File tree

8 files changed

+90
-16
lines changed

8 files changed

+90
-16
lines changed

.scrutinizer.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
filter:
22
paths: [src/*]
3-
4-
tools:
5-
external_code_coverage: true
6-
php_mess_detector: true
7-
php_code_sniffer: true
8-
php_pdepend: true

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ kodus/mail
55

66
[Simple](#objectives) services for sending UTF-8 e-mail.
77

8+
See [UPGRADING.md](UPGRADING.md) for upgrade instructions.
9+
810

911
## Features
1012

@@ -116,7 +118,8 @@ To run the integration tests, you will need to set up a local SMTP server for te
116118

117119
On Windows, try [Papercut](https://papercut.codeplex.com/) or [SMTP4Dev](http://smtp4dev.codeplex.com/).
118120

119-
On Linux, you will likely just have to set up an actual, local SMTP daemon. (Have fun with that!)
121+
On Linux, you will likely just have to set up an actual, local SMTP daemon. (see `.travis.yml` for an
122+
example of installing and starting `smtp-sink` which comes with `postfix`.)
120123

121124
You may need to copy `integration.suite.dist.yml` to `integration.suite.yml` to customize the
122125
SMTP host-name, port-number, etc.

UPGRADING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADING
2+
=========
3+
4+
## 0.1.x to 0.2.x
5+
6+
`Message::getDate()` now returns `DateTimeInterface` - it previously returned a UNIX (integer) timestamp.
7+
8+
`Message::setDate()` still accepts UNIX timestamps and strings, and now also `DateTime` and `DateTimeImmutable`.

src/MIMEWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function writeMessage(Message $message)
3535
*/
3636
public function writeMessageHeaders(Message $message)
3737
{
38-
$this->writeHeader("Date", date("r", $message->getDate()));
38+
$this->writeHeader("Date", $message->getDate()->format("r"));
3939

4040
$this->writeAddressHeader("To", $message->getTo());
4141
$this->writeAddressHeader("From", $message->getFrom());

src/Message.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Kodus\Mail;
44

5+
use DateTimeImmutable;
6+
use DateTimeInterface;
57
use InvalidArgumentException;
68

79
/**
@@ -45,7 +47,7 @@ class Message
4547
private $subject;
4648

4749
/**
48-
* @var int timestamp
50+
* @var DateTimeInterface
4951
*/
5052
private $date;
5153

@@ -224,7 +226,7 @@ public function getBCC()
224226
*
225227
* Some systems may choose to include the text of the "BCC" field only in the author's copy,
226228
* while others may also include it in the text sent to all those indicated in the "BCC" list.
227-
*
229+
*
228230
* @param Address|Address[] $address
229231
*/
230232
public function setBCC($address)
@@ -281,21 +283,30 @@ public function setSubject($subject)
281283
}
282284

283285
/**
284-
* @return int timestamp
286+
* @return DateTimeInterface
285287
*/
286288
public function getDate()
287289
{
288290
return $this->date;
289291
}
290292

291293
/**
292-
* @param int|string $date integer timestamp, or a string compatible with the strtotime() function
294+
* @param int|string|DateTimeInterface $date DateTime in Sender's timezone (or a UNIX integer timestamp;
295+
* or a string that is compatible with the strtotime() function)
293296
*/
294297
public function setDate($date)
295298
{
296-
$this->date = is_int($date)
297-
? $date
298-
: strtotime($date);
299+
if ($date instanceof DateTimeInterface) {
300+
$this->date = $date;
301+
} elseif (is_int($date)) {
302+
$this->date = DateTimeImmutable::createFromFormat("U", $date)
303+
->setTimezone(timezone_open(date_default_timezone_get()));
304+
} elseif (is_string($date)) {
305+
$this->date = DateTimeImmutable::createFromFormat("U", strtotime($date))
306+
->setTimezone(timezone_open(date_default_timezone_get()));
307+
} else {
308+
throw new InvalidArgumentException("invalid date given: " . var_export($date, true));
309+
}
299310
}
300311

301312
/**

tests/_support/Helper/Integration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class Integration extends \Codeception\Module
1010
{
11+
public function _before()
12+
{
13+
date_default_timezone_set("Europe/Copenhagen");
14+
}
15+
1116
/**
1217
* @return \Kodus\Mail\SMTP\Connector\SocketConnector
1318
*/

tests/_support/Helper/Unit.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
class Unit extends \Codeception\Module
88
{
9-
9+
public function _before()
10+
{
11+
date_default_timezone_set("Europe/Copenhagen");
12+
}
1013
}

tests/unit/MessageCest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Kodus\Mail\Test\Unit;
44

5+
use DateTime;
6+
use DateTimeZone;
57
use InvalidArgumentException;
68
use Kodus\Mail\Address;
79
use Kodus\Mail\Header;
@@ -63,4 +65,52 @@ function () use ($message) {
6365
}
6466
);
6567
}
68+
69+
public function setDate(UnitTester $I)
70+
{
71+
$message = new Message(
72+
new Address("[email protected]"),
73+
new Address("[email protected]"),
74+
"Hello, Bob",
75+
null
76+
);
77+
78+
// try setting a Date from a string in two different system timezones:
79+
80+
date_default_timezone_set("Europe/Copenhagen");
81+
82+
$message->setDate("Thu, 15 Sep 2016 17:20:54");
83+
84+
$I->assertSame("Thu, 15 Sep 2016 17:20:54 +0200", $message->getDate()->format("r"));
85+
86+
date_default_timezone_set("UTC");
87+
88+
$message->setDate("Thu, 15 Sep 2016 17:20:54");
89+
90+
$I->assertSame("Thu, 15 Sep 2016 17:20:54 +0000", $message->getDate()->format("r"));
91+
92+
// try setting a Date from an UNIX timestamp in two different system timezones:
93+
94+
date_default_timezone_set("Europe/Copenhagen");
95+
96+
$message->setDate(1473952854);
97+
98+
$I->assertSame("Thu, 15 Sep 2016 17:20:54 +0200", $message->getDate()->format("r"));
99+
100+
date_default_timezone_set("UTC");
101+
102+
$message->setDate(1473952854);
103+
104+
$I->assertSame("Thu, 15 Sep 2016 15:20:54 +0000", $message->getDate()->format("r"));
105+
106+
// try setting a Date from a DateTime instance:
107+
108+
$date = new DateTime("@" . strtotime("Thu, 15 Sep 2016 17:20:54 +0200"));
109+
110+
$date->setTimezone(new DateTimeZone("Europe/Copenhagen"));
111+
112+
$message->setDate($date);
113+
114+
$I->assertSame("Thu, 15 Sep 2016 17:20:54 +0200", $message->getDate()->format("r"));
115+
}
66116
}

0 commit comments

Comments
 (0)