From 6ed68dfd7f2ccc0e8238885c83891a09ed17e29b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 4 Nov 2019 12:24:20 +0100 Subject: [PATCH] wip --- src/Features/Timestamps.php | 25 ++++++++-- tests/{ => Features}/TenantConfigTest.php | 2 +- .../TenantRedirectMacroTest.php | 2 +- tests/Features/TimestampTest.php | 46 +++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) rename tests/{ => Features}/TenantConfigTest.php (96%) rename tests/{ => Features}/TenantRedirectMacroTest.php (95%) create mode 100644 tests/Features/TimestampTest.php diff --git a/src/Features/Timestamps.php b/src/Features/Timestamps.php index 4eb4bb5f..6a2fdac6 100644 --- a/src/Features/Timestamps.php +++ b/src/Features/Timestamps.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Features; +use Illuminate\Config\Repository; use Illuminate\Support\Facades\Date; use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Tenant; @@ -11,19 +12,35 @@ use Stancl\Tenancy\TenantManager; class Timestamps implements Feature { + /** @var Repository */ + protected $config; + + public function __construct(Repository $config) + { + $this->config = $config; + } + public function bootstrap(TenantManager $tenantManager): void { $tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) { - $tenant->with('created_at', Date::now()); - $tenant->with('updated_at', Date::now()); + $tenant->with('created_at', $this->now()); + $tenant->with('updated_at', $this->now()); }); $tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) { - $tenant->with('updated_at', Date::now()); + $tenant->with('updated_at', $this->now()); }); $tenantManager->hook('tenant.softDeleting', function ($tm, Tenant $tenant) { - $tenant->with('deleted_at', Date::now()); + $tenant->with('deleted_at', $this->now()); }); } + + public function now(): string + { + // Add this key to your tenancy.php config if you need to change the format. + return Date::now()->format( + $this->config->get('tenancy.features.timestamps.format') ?? 'c' // ISO 8601 + ); + } } diff --git a/tests/TenantConfigTest.php b/tests/Features/TenantConfigTest.php similarity index 96% rename from tests/TenantConfigTest.php rename to tests/Features/TenantConfigTest.php index 68c2cf4a..b546ce21 100644 --- a/tests/TenantConfigTest.php +++ b/tests/Features/TenantConfigTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests; +namespace Stancl\Tenancy\Tests\Feature; class TenantConfigTest extends TestCase { diff --git a/tests/TenantRedirectMacroTest.php b/tests/Features/TenantRedirectMacroTest.php similarity index 95% rename from tests/TenantRedirectMacroTest.php rename to tests/Features/TenantRedirectMacroTest.php index 970579ec..5aeceb0b 100644 --- a/tests/TenantRedirectMacroTest.php +++ b/tests/Features/TenantRedirectMacroTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests; +namespace Stancl\Tenancy\Tests\Feature; use Route; use Stancl\Tenancy\Tenant; diff --git a/tests/Features/TimestampTest.php b/tests/Features/TimestampTest.php new file mode 100644 index 00000000..1a75bb37 --- /dev/null +++ b/tests/Features/TimestampTest.php @@ -0,0 +1,46 @@ + [ + Timestamps::class, + ]]); + } + + /** @test */ + public function create_and_update_timestamps_are_added_on_create() + { + $tenant = Tenant::new()->save(); + $this->assertArraySubset(['created_at', 'updated_at'], $tenant->data); + } + + /** @test */ + public function update_timestamps_are_added() + { + $tenant = Tenant::new()->save(); + $this->assertSame($tenant->created_at, $tenant->updated_at); + + $this->assert($tenant->updated_at > $tenant->created_at); + } + + /** @test */ + public function softdelete_timestamps_are_added() + { + $this->assertSame(); + } +}