mirror of
https://github.com/ambieco/laravel-lift.git
synced 2026-01-12 14:13:43 +08:00
Fix issues with Timestamps
This commit is contained in:
@@ -49,7 +49,7 @@
|
||||
"lint": "pint",
|
||||
"test:lint": "pint --test",
|
||||
"test:unit": "./vendor/bin/pest --order-by random",
|
||||
"test:static": "phpstan analyse",
|
||||
"test:static": "phpstan analyse --memory-limit 1G",
|
||||
"test": [
|
||||
"@test:lint",
|
||||
"@test:unit",
|
||||
|
||||
38
phpunit.xml
38
phpunit.xml
@@ -1,23 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Test Suite">
|
||||
<directory suffix="Test.php">./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<coverage>
|
||||
<include>
|
||||
<directory suffix=".php">./app</directory>
|
||||
<directory suffix=".php">./src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
|
||||
<php>
|
||||
<env name="DB_CONNECTION" value="sqlite"/>
|
||||
<env name="DB_DATABASE" value=":memory:"/>
|
||||
</php>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="Test Suite">
|
||||
<directory suffix="Test.php">./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<coverage/>
|
||||
<php>
|
||||
<env name="DB_CONNECTION" value="sqlite"/>
|
||||
<env name="DB_DATABASE" value=":memory:"/>
|
||||
</php>
|
||||
<source>
|
||||
<include>
|
||||
<directory suffix=".php">./app</directory>
|
||||
<directory suffix=".php">./src</directory>
|
||||
</include>
|
||||
</source>
|
||||
</phpunit>
|
||||
|
||||
20
src/Lift.php
20
src/Lift.php
@@ -150,6 +150,26 @@ trait Lift
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setCreatedAt($value)
|
||||
{
|
||||
$createdAtColumn = $this->getCreatedAtColumn();
|
||||
|
||||
$this->{$createdAtColumn} = $value;
|
||||
$this->setAttribute($createdAtColumn, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setUpdatedAt($value)
|
||||
{
|
||||
$updatedAtColumn = $this->getUpdatedAtColumn();
|
||||
|
||||
$this->{$updatedAtColumn} = $value;
|
||||
$this->setAttribute($updatedAtColumn, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected static function ignoredProperties(): array
|
||||
{
|
||||
return [
|
||||
|
||||
37
tests/Datasets/PostTimestamps.php
Normal file
37
tests/Datasets/PostTimestamps.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Datasets;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use WendellAdriel\Lift\Attributes\Cast;
|
||||
use WendellAdriel\Lift\Attributes\DB;
|
||||
use WendellAdriel\Lift\Attributes\Fillable;
|
||||
use WendellAdriel\Lift\Attributes\PrimaryKey;
|
||||
use WendellAdriel\Lift\Lift;
|
||||
|
||||
#[DB(table: 'posts')]
|
||||
class PostTimestamps extends Model
|
||||
{
|
||||
use Lift;
|
||||
|
||||
#[PrimaryKey]
|
||||
public int $id;
|
||||
|
||||
#[Fillable]
|
||||
public string $title;
|
||||
|
||||
#[Fillable]
|
||||
public string $content;
|
||||
|
||||
#[Fillable]
|
||||
public ?int $user_id;
|
||||
|
||||
#[Cast('datetime')]
|
||||
public Carbon $created_at;
|
||||
|
||||
#[Cast('datetime')]
|
||||
public Carbon $updated_at;
|
||||
}
|
||||
33
tests/Feature/TimestampsTest.php
Normal file
33
tests/Feature/TimestampsTest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Tests\Datasets\PostTimestamps;
|
||||
|
||||
it('should set timestamps on create', function () {
|
||||
PostTimestamps::create([
|
||||
'title' => 'Test',
|
||||
'content' => 'Test',
|
||||
]);
|
||||
|
||||
$post = PostTimestamps::first();
|
||||
expect($post->created_at)->not->toBeNull();
|
||||
expect($post->updated_at)->not->toBeNull();
|
||||
});
|
||||
|
||||
it('should set timestamps on update', function () {
|
||||
$post = PostTimestamps::create([
|
||||
'title' => 'Test',
|
||||
'content' => 'Test',
|
||||
]);
|
||||
sleep(2); // Wait 2 seconds to update
|
||||
$post->title = 'Test 2';
|
||||
$post->save();
|
||||
$post = $post->fresh();
|
||||
|
||||
expect($post->created_at)->not->toBeNull();
|
||||
expect($post->updated_at)->not->toBeNull();
|
||||
expect($post->created_at)->not->toEqual($post->updated_at);
|
||||
expect($post->updated_at)->toBeGreaterThan($post->created_at);
|
||||
expect($post->title)->toBe('Test 2');
|
||||
});
|
||||
Reference in New Issue
Block a user