File tree 3 files changed +74
-2
lines changed
3 files changed +74
-2
lines changed Original file line number Diff line number Diff line change 5
5
namespace MongoDB \Laravel \Eloquent ;
6
6
7
7
use BackedEnum ;
8
- use Carbon \Carbon ;
9
8
use Carbon \CarbonInterface ;
10
9
use DateTimeInterface ;
11
10
use DateTimeZone ;
@@ -128,7 +127,7 @@ public function fromDateTime($value): UTCDateTime
128
127
*
129
128
* @param mixed $value
130
129
*/
131
- protected function asDateTime ($ value ): Carbon
130
+ protected function asDateTime ($ value ): DateTimeInterface
132
131
{
133
132
// Convert UTCDateTime instances to Carbon.
134
133
if ($ value instanceof UTCDateTime) {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace MongoDB \Laravel \Tests \Eloquent ;
6
+
7
+ use Carbon \CarbonImmutable ;
8
+ use Illuminate \Support \Facades \Date ;
9
+ use MongoDB \Laravel \Tests \Models \Anniversary ;
10
+ use MongoDB \Laravel \Tests \TestCase ;
11
+
12
+ use function assert ;
13
+
14
+ final class DateTimeImmutableTest extends TestCase
15
+ {
16
+ protected function setUp (): void
17
+ {
18
+ parent ::setUp ();
19
+
20
+ Anniversary::truncate ();
21
+ }
22
+
23
+ protected function tearDown (): void
24
+ {
25
+ Date::useDefault ();
26
+
27
+ parent ::tearDown ();
28
+ }
29
+
30
+ public function testCanReturnCarbonImmutableObject (): void
31
+ {
32
+ Date::use (CarbonImmutable::class);
33
+
34
+ Anniversary::create ([
35
+ 'name ' => 'John ' ,
36
+ 'anniversary ' => new CarbonImmutable ('2020-01-01 00:00:00 ' ),
37
+ ]);
38
+
39
+ $ anniversary = Anniversary::sole ();
40
+ assert ($ anniversary instanceof Anniversary);
41
+ self ::assertInstanceOf (CarbonImmutable::class, $ anniversary ->anniversary );
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace MongoDB \Laravel \Tests \Models ;
6
+
7
+ use Illuminate \Database \Eloquent \Model ;
8
+ use MongoDB \Laravel \Eloquent \DocumentModel ;
9
+ use MongoDB \Laravel \Eloquent \Model as Eloquent ;
10
+ use MongoDB \Laravel \Query \Builder ;
11
+
12
+ /**
13
+ * @property string $name
14
+ * @property string $anniversary
15
+ * @mixin Eloquent
16
+ * @method static Builder create(...$values)
17
+ * @method static Builder truncate()
18
+ * @method static Eloquent sole(...$parameters)
19
+ */
20
+ class Anniversary extends Model
21
+ {
22
+ use DocumentModel;
23
+
24
+ protected $ keyType = 'string ' ;
25
+ protected $ connection = 'mongodb ' ;
26
+ protected $ table = 'anniversary ' ;
27
+ protected $ fillable = ['name ' , 'anniversary ' ];
28
+
29
+ protected $ casts = ['anniversary ' => 'immutable_datetime ' ];
30
+ }
You can’t perform that action at this time.
0 commit comments