1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 13:54:04 +00:00
This commit is contained in:
Samuel Štancl 2019-11-04 12:24:20 +01:00
parent 3174256bd2
commit 6ed68dfd7f
4 changed files with 69 additions and 6 deletions

View file

@ -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
);
}
}

View file

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
namespace Stancl\Tenancy\Tests\Feature;
class TenantConfigTest extends TestCase
{

View file

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
namespace Stancl\Tenancy\Tests\Feature;
use Route;
use Stancl\Tenancy\Tenant;

View file

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Feature;
use Stancl\Tenancy\Features\Timestamps;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\Tests\TestCase;
class TimestampTest extends TestCase
{
public $autoCreateTenant = false;
public $autoInitTenancy = false;
public function setUp(): void
{
parent::setUp();
config(['tenancy.features' => [
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();
}
}