Skip to content

Commit 3260731

Browse files
authored
Hotfix/Initialize string format when wakes DateTimeValue up
* Initialize string format when wakes DateTimeValue up * Create test to cover the case Co-authored-by: Dmytro Zasiadko <dmytro@2amigos.us>
1 parent 9870785 commit 3260731

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Domain/ValueObjects/DateTimeValue.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,20 @@ class DateTimeValue extends CarbonImmutable implements ValueObject
3131
public function __construct($time = null, $tz = null)
3232
{
3333
parent::__construct($time, $tz);
34+
$this->initializeDefaultStringFormat();
35+
}
36+
37+
private function initializeDefaultStringFormat(): void
38+
{
3439
$this->settings(['toStringFormat' => 'c']);
3540
}
3641

42+
public function __wakeup()
43+
{
44+
parent::__wakeup();
45+
$this->initializeDefaultStringFormat();
46+
}
47+
3748
/**
3849
* Return the value as string.
3950
*
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Tests\Domain\ValueObjects;
6+
7+
use Mockery\Adapter\Phpunit\MockeryTestCase;
8+
use OtherCode\ComplexHeart\Domain\ValueObjects\DateTimeValue;
9+
10+
/**
11+
* Class DateTimeValueTest
12+
* @author Dmytro <dmytro@2amigos.us>
13+
* @package OtherCode\ComplexHeart\Tests\Domain\ValueObjects
14+
*/
15+
class DateTimeValueTest extends MockeryTestCase
16+
{
17+
public function testShouldInitializeStringFormatWhenWakesUp(): void
18+
{
19+
$instance = new DateTimeValue();
20+
$serialized = serialize($instance);
21+
$wakedUp = unserialize($serialized);
22+
$this->assertSame($instance->__toString(), $wakedUp->__toString());
23+
}
24+
}

0 commit comments

Comments
 (0)