mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 14:54:03 +00:00
wip
This commit is contained in:
parent
3174256bd2
commit
6ed68dfd7f
4 changed files with 69 additions and 6 deletions
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Features;
|
namespace Stancl\Tenancy\Features;
|
||||||
|
|
||||||
|
use Illuminate\Config\Repository;
|
||||||
use Illuminate\Support\Facades\Date;
|
use Illuminate\Support\Facades\Date;
|
||||||
use Stancl\Tenancy\Contracts\Feature;
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
@ -11,19 +12,35 @@ use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
class Timestamps implements Feature
|
class Timestamps implements Feature
|
||||||
{
|
{
|
||||||
|
/** @var Repository */
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
public function __construct(Repository $config)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
public function bootstrap(TenantManager $tenantManager): void
|
public function bootstrap(TenantManager $tenantManager): void
|
||||||
{
|
{
|
||||||
$tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) {
|
$tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) {
|
||||||
$tenant->with('created_at', Date::now());
|
$tenant->with('created_at', $this->now());
|
||||||
$tenant->with('updated_at', Date::now());
|
$tenant->with('updated_at', $this->now());
|
||||||
});
|
});
|
||||||
|
|
||||||
$tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) {
|
$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) {
|
$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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests\Feature;
|
||||||
|
|
||||||
class TenantConfigTest extends TestCase
|
class TenantConfigTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests\Feature;
|
||||||
|
|
||||||
use Route;
|
use Route;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
46
tests/Features/TimestampTest.php
Normal file
46
tests/Features/TimestampTest.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue