From 58de8d57f9e9ef7cd364f26810b6f75127622804 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Mon, 2 Oct 2023 11:17:01 +0100 Subject: [PATCH] Fix issues with Timestamps --- composer.json | 2 +- phpunit.xml | 38 ++++++++++++++----------------- src/Lift.php | 20 ++++++++++++++++ tests/Datasets/PostTimestamps.php | 37 ++++++++++++++++++++++++++++++ tests/Feature/TimestampsTest.php | 33 +++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 22 deletions(-) create mode 100644 tests/Datasets/PostTimestamps.php create mode 100644 tests/Feature/TimestampsTest.php diff --git a/composer.json b/composer.json index 8a04e45..e93f786 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/phpunit.xml b/phpunit.xml index 5ba8f37..0d569f9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,23 +1,19 @@ - - - - ./tests - - - - - ./app - ./src - - - - - - - + + + + ./tests + + + + + + + + + + ./app + ./src + + diff --git a/src/Lift.php b/src/Lift.php index 9286af4..459e05c 100644 --- a/src/Lift.php +++ b/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 [ diff --git a/tests/Datasets/PostTimestamps.php b/tests/Datasets/PostTimestamps.php new file mode 100644 index 0000000..a829079 --- /dev/null +++ b/tests/Datasets/PostTimestamps.php @@ -0,0 +1,37 @@ + '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'); +});