Commit 0ae4287e authored by Ditty's avatar Ditty

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
parent cbdce4f1
...@@ -223,36 +223,37 @@ abstract class Model extends BaseModel ...@@ -223,36 +223,37 @@ abstract class Model extends BaseModel
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function originalIsEquivalent($key, $current) public function originalIsEquivalent($key)
{ {
if (! array_key_exists($key, $this->original)) { if (! array_key_exists($key, $this->original)) {
return false; return false;
} }
$original = $this->getOriginal($key); $attribute = Arr::get($this->attributes, $key);
$original = Arr::get($this->original, $key);
if ($current === $original) { if ($attribute === $original) {
return true; return true;
} }
if (null === $current) { if (null === $attribute) {
return false; return false;
} }
if ($this->isDateAttribute($key)) { if ($this->isDateAttribute($key)) {
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current; $attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute;
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original; $original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
return $current == $original; return $attribute == $original;
} }
if ($this->hasCast($key)) { if ($this->hasCast($key, static::$primitiveCastTypes)) {
return $this->castAttribute($key, $current) === return $this->castAttribute($key, $attribute) ===
$this->castAttribute($key, $original); $this->castAttribute($key, $original);
} }
return is_numeric($current) && is_numeric($original) return is_numeric($attribute) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0; && strcmp((string) $attribute, (string) $original) === 0;
} }
/** /**
......
<?php
declare(strict_types=1);
use Illuminate\Support\Carbon;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class DatabaseEloquentTimestampsTest extends TestCase
{
/**
* Tear down the database schema.
*
* @return void
*/
protected function tearDown(): void
{
User::truncate();
}
/**
* Tests...
*/
public function testUserWithCreatedAtAndUpdatedAt()
{
$now = Carbon::now();
$user = UserWithCreatedAndUpdated::create([
'email' => 'test@test.com',
]);
$this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString());
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
}
public function testUserWithCreatedAt()
{
$now = Carbon::now();
$user = UserWithCreated::create([
'email' => 'test@test.com',
]);
$this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString());
}
public function testUserWithUpdatedAt()
{
$now = Carbon::now();
$user = UserWithUpdated::create([
'email' => 'test@test.com',
]);
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
}
}
/**
* Eloquent Models...
*/
class UserWithCreatedAndUpdated extends Eloquent
{
protected $collection = 'users';
protected $guarded = [];
}
class UserWithCreated extends Eloquent
{
public const UPDATED_AT = null;
protected $collection = 'users_created_at';
protected $guarded = [];
protected $dateFormat = 'U';
}
class UserWithUpdated extends Eloquent
{
public const CREATED_AT = null;
protected $collection = 'users_updated_at';
protected $guarded = [];
protected $dateFormat = 'U';
}
...@@ -397,25 +397,14 @@ class ModelTest extends TestCase ...@@ -397,25 +397,14 @@ class ModelTest extends TestCase
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first(); $user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
$this->assertEquals('John Doe', $user->name); $this->assertEquals('John Doe', $user->name);
// test custom date format for json output
$json = $user->toArray();
$this->assertEquals($user->birthday->format('l jS \of F Y h:i:s A'), $json['birthday']);
$this->assertEquals($user->created_at->format('l jS \of F Y h:i:s A'), $json['created_at']);
// test created_at // test created_at
$item = Item::create(['name' => 'sword']); $item = Item::create(['name' => 'sword']);
$this->assertInstanceOf(UTCDateTime::class, $item->getOriginal('created_at')); $this->assertInstanceOf(UTCDateTime::class, $item->getRawOriginal('created_at'));
$this->assertEquals($item->getOriginal('created_at') $this->assertEquals($item->getRawOriginal('created_at')
->toDateTime() ->toDateTime()
->getTimestamp(), $item->created_at->getTimestamp()); ->getTimestamp(), $item->created_at->getTimestamp());
$this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp())); $this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp()));
// test default date format for json output
/** @var Item $item */
$item = Item::create(['name' => 'sword']);
$json = $item->toArray();
$this->assertEquals($item->created_at->format('Y-m-d H:i:s'), $json['created_at']);
/** @var User $user */ /** @var User $user */
$user = User::create(['name' => 'Jane Doe', 'birthday' => time()]); $user = User::create(['name' => 'Jane Doe', 'birthday' => time()]);
$this->assertInstanceOf(Carbon::class, $user->birthday); $this->assertInstanceOf(Carbon::class, $user->birthday);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
declare(strict_types=1); declare(strict_types=1);
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider; use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
class QueueTest extends TestCase class QueueTest extends TestCase
...@@ -18,6 +19,12 @@ class QueueTest extends TestCase ...@@ -18,6 +19,12 @@ class QueueTest extends TestCase
public function testQueueJobLifeCycle(): void public function testQueueJobLifeCycle(): void
{ {
$uuid = Str::uuid();
Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
$id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test'); $id = Queue::push('test', ['action' => 'QueueJobLifeCycle'], 'test');
$this->assertNotNull($id); $this->assertNotNull($id);
...@@ -26,9 +33,11 @@ class QueueTest extends TestCase ...@@ -26,9 +33,11 @@ class QueueTest extends TestCase
$this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job); $this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job);
$this->assertEquals(1, $job->isReserved()); $this->assertEquals(1, $job->isReserved());
$this->assertEquals(json_encode([ $this->assertEquals(json_encode([
'uuid' => $uuid,
'displayName' => 'test', 'displayName' => 'test',
'job' => 'test', 'job' => 'test',
'maxTries' => null, 'maxTries' => null,
'maxExceptions' => null,
'delay' => null, 'delay' => null,
'timeout' => null, 'timeout' => null,
'data' => ['action' => 'QueueJobLifeCycle'], 'data' => ['action' => 'QueueJobLifeCycle'],
...@@ -37,6 +46,8 @@ class QueueTest extends TestCase ...@@ -37,6 +46,8 @@ class QueueTest extends TestCase
// Remove reserved job // Remove reserved job
$job->delete(); $job->delete();
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count()); $this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
Str::createUuidsNormally();
} }
public function testQueueJobExpired(): void public function testQueueJobExpired(): void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment