Skip to content

Commit 0ae4287

Browse files
committed
Make functions and tests compatible
Make functions and tests compatible with laravel 7 Date serialization changes Laravel 7 uses a new date serialization format when using the toArray or toJson method on Eloquent models. To format dates for serialization, the framework now uses Carbon's toJSON method, which produces an ISO-8601 https://laravel.com/docs/7.x/upgrade#date-serialization laravel_7_comptability $model->getOriginal() was moved to getRawOriginal method See https://laravel.com/docs/7.x/upgrade#factory-types
1 parent cbdce4f commit 0ae4287

File tree

4 files changed

+109
-23
lines changed

4 files changed

+109
-23
lines changed

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,36 +223,37 @@ public function getCasts()
223223
/**
224224
* {@inheritdoc}
225225
*/
226-
public function originalIsEquivalent($key, $current)
226+
public function originalIsEquivalent($key)
227227
{
228228
if (! array_key_exists($key, $this->original)) {
229229
return false;
230230
}
231231

232-
$original = $this->getOriginal($key);
232+
$attribute = Arr::get($this->attributes, $key);
233+
$original = Arr::get($this->original, $key);
233234

234-
if ($current === $original) {
235+
if ($attribute === $original) {
235236
return true;
236237
}
237238

238-
if (null === $current) {
239+
if (null === $attribute) {
239240
return false;
240241
}
241242

242243
if ($this->isDateAttribute($key)) {
243-
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
244+
$attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute;
244245
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
245246

246-
return $current == $original;
247+
return $attribute == $original;
247248
}
248249

249-
if ($this->hasCast($key)) {
250-
return $this->castAttribute($key, $current) ===
250+
if ($this->hasCast($key, static::$primitiveCastTypes)) {
251+
return $this->castAttribute($key, $attribute) ===
251252
$this->castAttribute($key, $original);
252253
}
253254

254-
return is_numeric($current) && is_numeric($original)
255-
&& strcmp((string) $current, (string) $original) === 0;
255+
return is_numeric($attribute) && is_numeric($original)
256+
&& strcmp((string) $attribute, (string) $original) === 0;
256257
}
257258

258259
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Support\Carbon;
6+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
7+
8+
class DatabaseEloquentTimestampsTest extends TestCase
9+
{
10+
/**
11+
* Tear down the database schema.
12+
*
13+
* @return void
14+
*/
15+
protected function tearDown(): void
16+
{
17+
User::truncate();
18+
}
19+
20+
/**
21+
* Tests...
22+
*/
23+
public function testUserWithCreatedAtAndUpdatedAt()
24+
{
25+
$now = Carbon::now();
26+
$user = UserWithCreatedAndUpdated::create([
27+
'email' => 'test@test.com',
28+
]);
29+
30+
$this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString());
31+
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
32+
}
33+
34+
public function testUserWithCreatedAt()
35+
{
36+
$now = Carbon::now();
37+
$user = UserWithCreated::create([
38+
'email' => 'test@test.com',
39+
]);
40+
41+
$this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString());
42+
}
43+
44+
public function testUserWithUpdatedAt()
45+
{
46+
$now = Carbon::now();
47+
$user = UserWithUpdated::create([
48+
'email' => 'test@test.com',
49+
]);
50+
51+
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
52+
}
53+
}
54+
55+
/**
56+
* Eloquent Models...
57+
*/
58+
class UserWithCreatedAndUpdated extends Eloquent
59+
{
60+
protected $collection = 'users';
61+
62+
protected $guarded = [];
63+
}
64+
65+
class UserWithCreated extends Eloquent
66+
{
67+
public const UPDATED_AT = null;
68+
69+
protected $collection = 'users_created_at';
70+
71+
protected $guarded = [];
72+
73+
protected $dateFormat = 'U';
74+
}
75+
76+
class UserWithUpdated extends Eloquent
77+
{
78+
public const CREATED_AT = null;
79+
80+
protected $collection = 'users_updated_at';
81+
82+
protected $guarded = [];
83+
84+
protected $dateFormat = 'U';
85+
}

tests/ModelTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,25 +397,14 @@ public function testDates(): void
397397
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
398398
$this->assertEquals('John Doe', $user->name);
399399

400-
// test custom date format for json output
401-
$json = $user->toArray();
402-
$this->assertEquals($user->birthday->format('l jS \of F Y h:i:s A'), $json['birthday']);
403-
$this->assertEquals($user->created_at->format('l jS \of F Y h:i:s A'), $json['created_at']);
404-
405400
// test created_at
406401
$item = Item::create(['name' => 'sword']);
407-
$this->assertInstanceOf(UTCDateTime::class, $item->getOriginal('created_at'));
408-
$this->assertEquals($item->getOriginal('created_at')
402+
$this->assertInstanceOf(UTCDateTime::class, $item->getRawOriginal('created_at'));
403+
$this->assertEquals($item->getRawOriginal('created_at')
409404
->toDateTime()
410405
->getTimestamp(), $item->created_at->getTimestamp());
411406
$this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp()));
412407

413-
// test default date format for json output
414-
/** @var Item $item */
415-
$item = Item::create(['name' => 'sword']);
416-
$json = $item->toArray();
417-
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
418-
419408
/** @var User $user */
420409
$user = User::create(['name' => 'Jane Doe', 'birthday' => time()]);
421410
$this->assertInstanceOf(Carbon::class, $user->birthday);

tests/QueueTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Carbon\Carbon;
6+
use Illuminate\Support\Str;
67
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
78

89
class QueueTest extends TestCase
@@ -18,6 +19,12 @@ public function setUp(): void
1819

1920
public function testQueueJobLifeCycle(): void
2021
{
22+
$uuid = Str::uuid();
23+
24+
Str::createUuidsUsing(function () use ($uuid) {
25+
return $uuid;
26+
});
27+
2128
$id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test');
2229
$this->assertNotNull($id);
2330

@@ -26,9 +33,11 @@ public function testQueueJobLifeCycle(): void
2633
$this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job);
2734
$this->assertEquals(1, $job->isReserved());
2835
$this->assertEquals(json_encode([
36+
'uuid' => $uuid,
2937
'displayName' => 'test',
3038
'job' => 'test',
3139
'maxTries' => null,
40+
'maxExceptions' => null,
3241
'delay' => null,
3342
'timeout' => null,
3443
'data' => ['action' => 'QueueJobLifeCycle'],
@@ -37,6 +46,8 @@ public function testQueueJobLifeCycle(): void
3746
// Remove reserved job
3847
$job->delete();
3948
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
49+
50+
Str::createUuidsNormally();
4051
}
4152

4253
public function testQueueJobExpired(): void

0 commit comments

Comments
 (0)