Fix issues with Timestamps

This commit is contained in:
Wendell Adriel
2023-10-02 11:17:01 +01:00
parent 0cca8066ca
commit 58de8d57f9
5 changed files with 108 additions and 22 deletions

View File

@@ -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",

View File

@@ -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>

View File

@@ -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 [

View 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;
}

View 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');
});