From 31c9930c9356f5697bf57b3f0ca750d5ec402e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 4 Nov 2019 17:56:08 +0100 Subject: [PATCH] [2.2.0] Timestamps (#213) * Timestamps * Apply fixes from StyleCI * Add void typehint * wip * Fix tests * Apply fixes from StyleCI --- assets/config.php | 1 + src/Features/Timestamps.php | 46 +++++++++++++++ src/Tenant.php | 18 +++++- tests/{ => Features}/TenantConfigTest.php | 4 +- .../TenantRedirectMacroTest.php | 3 +- tests/Features/TimestampTest.php | 56 +++++++++++++++++++ 6 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 src/Features/Timestamps.php rename tests/{ => Features}/TenantConfigTest.php (93%) rename tests/{ => Features}/TenantRedirectMacroTest.php (91%) create mode 100644 tests/Features/TimestampTest.php diff --git a/assets/config.php b/assets/config.php index d5989b33..27c8aad6 100644 --- a/assets/config.php +++ b/assets/config.php @@ -82,6 +82,7 @@ return [ // not needed for tenancy to be bootstrapped. They are run // regardless of whether tenancy has been initialized. + // Stancl\Tenancy\Features\Timestamps::class, // Stancl\Tenancy\Features\TenantConfig::class, // Stancl\Tenancy\Features\TelescopeTags::class, // Stancl\Tenancy\Features\TenantRedirect::class, diff --git a/src/Features/Timestamps.php b/src/Features/Timestamps.php new file mode 100644 index 00000000..6a2fdac6 --- /dev/null +++ b/src/Features/Timestamps.php @@ -0,0 +1,46 @@ +config = $config; + } + + public function bootstrap(TenantManager $tenantManager): void + { + $tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) { + $tenant->with('created_at', $this->now()); + $tenant->with('updated_at', $this->now()); + }); + + $tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) { + $tenant->with('updated_at', $this->now()); + }); + + $tenantManager->hook('tenant.softDeleting', function ($tm, Tenant $tenant) { + $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/src/Tenant.php b/src/Tenant.php index 124e3412..8e6a900b 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -260,10 +260,16 @@ class Tenant implements ArrayAccess */ public function softDelete(): self { - $this->put('_tenancy_original_domains', $this->domains); + $this->manager->event('tenant.softDeleting', $this); + + $this->put([ + '_tenancy_original_domains' => $this->domains, + ]); $this->clearDomains(); $this->save(); + $this->manager->event('tenant.softDeleted', $this); + return $this; } @@ -328,6 +334,8 @@ class Tenant implements ArrayAccess */ public function put($key, $value = null): self { + $this->manager->event('tenant.updating', $this); + if ($key === 'id') { throw new TenantStorageException("Tenant ids can't be changed."); } @@ -348,6 +356,8 @@ class Tenant implements ArrayAccess $this->data[$key] = $value; } + $this->manager->event('tenant.updated', $this); + return $this; } @@ -376,6 +386,8 @@ class Tenant implements ArrayAccess */ public function deleteKeys(array $keys): self { + $this->manager->event('tenant.updating', $this); + if (! $this->storage instanceof CanDeleteKeys) { throw new NotImplementedException(get_class($this->storage), 'deleteMany', 'This method was added to storage drivers provided by the package in 2.2.0 and will be part of the StorageDriver contract in 3.0.0.' @@ -387,11 +399,13 @@ class Tenant implements ArrayAccess } } + $this->manager->event('tenant.updated', $this); + return $this; } /** - * Set a value. + * Set a value in the data array without saving into storage. * * @param string $key * @param mixed $value diff --git a/tests/TenantConfigTest.php b/tests/Features/TenantConfigTest.php similarity index 93% rename from tests/TenantConfigTest.php rename to tests/Features/TenantConfigTest.php index 68c2cf4a..4e2405be 100644 --- a/tests/TenantConfigTest.php +++ b/tests/Features/TenantConfigTest.php @@ -2,7 +2,9 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests; +namespace Stancl\Tenancy\Tests\Feature; + +use Stancl\Tenancy\Tests\TestCase; class TenantConfigTest extends TestCase { diff --git a/tests/TenantRedirectMacroTest.php b/tests/Features/TenantRedirectMacroTest.php similarity index 91% rename from tests/TenantRedirectMacroTest.php rename to tests/Features/TenantRedirectMacroTest.php index 970579ec..8ae8cbe5 100644 --- a/tests/TenantRedirectMacroTest.php +++ b/tests/Features/TenantRedirectMacroTest.php @@ -2,10 +2,11 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests; +namespace Stancl\Tenancy\Tests\Feature; use Route; use Stancl\Tenancy\Tenant; +use Stancl\Tenancy\Tests\TestCase; class TenantRedirectMacroTest extends TestCase { diff --git a/tests/Features/TimestampTest.php b/tests/Features/TimestampTest.php new file mode 100644 index 00000000..1b6c1a81 --- /dev/null +++ b/tests/Features/TimestampTest.php @@ -0,0 +1,56 @@ + [ + Timestamps::class, + ]]); + } + + /** @test */ + public function create_and_update_timestamps_are_added_on_create() + { + $tenant = Tenant::new()->save(); + $this->assertArrayHasKey('created_at', $tenant->data); + $this->assertArrayHasKey('updated_at', $tenant->data); + } + + /** @test */ + public function update_timestamps_are_added() + { + $tenant = Tenant::new()->save(); + $this->assertSame($tenant->created_at, $tenant->updated_at); + $this->assertSame('string', gettype($tenant->created_at)); + + sleep(1); + + $tenant->put('abc', 'def'); + + $this->assertTrue($tenant->updated_at > $tenant->created_at); + } + + /** @test */ + public function softdelete_timestamps_are_added() + { + $tenant = Tenant::new()->save(); + $this->assertNull($tenant->deleted_at); + + $tenant->softDelete(); + $this->assertNotNull($tenant->deleted_at); + } +}