diff --git a/tests/AssignmentTest.php b/tests/AssignmentTest.php index 971fe5be..ef08b2d0 100644 --- a/tests/AssignmentTest.php +++ b/tests/AssignmentTest.php @@ -23,7 +23,7 @@ public function testGetAttributes(): void $this->assertSame([ 'item_name' => 'test1', 'user_id' => '42', - 'created_at' => 1642029084, + 'created_at' => 1_642_029_084, ], $assignment->getAttributes()); } } diff --git a/tests/Common/AssignmentsStorageTestTrait.php b/tests/Common/AssignmentsStorageTestTrait.php index 80761015..ded1aa85 100644 --- a/tests/Common/AssignmentsStorageTestTrait.php +++ b/tests/Common/AssignmentsStorageTestTrait.php @@ -24,7 +24,7 @@ trait AssignmentsStorageTestTrait protected function setUp(): void { - if ($this->name() === 'testAdd') { + if ($this->name() === 'testAddWithCurrentTimestamp') { ClockMock::freeze(new DateTime('2023-05-10 08:24:39')); } @@ -34,7 +34,7 @@ protected function setUp(): void protected function tearDown(): void { - if ($this->name() === 'testAdd') { + if ($this->name() === 'testAddWithCurrentTimestamp') { ClockMock::reset(); } @@ -241,7 +241,7 @@ public function testFilterUserItemNames(string $userId, array $itemNames, array ); } - public function testAdd(): void + public function testAddWithCurrentTimestamp(): void { $storage = $this->getAssignmentsStorage(); $storage->add(new Assignment(userId: 'john', itemName: 'Operator', createdAt: time())); @@ -253,7 +253,7 @@ public function testAdd(): void ); } - public function testAddWithCreatedAt(): void + public function testAddWithPastTimestamp(): void { $storage = $this->getAssignmentsStorage(); $storage->add(new Assignment(userId: 'john', itemName: 'Operator', createdAt: 1_694_508_008)); diff --git a/tests/Common/ItemsStorageTestTrait.php b/tests/Common/ItemsStorageTestTrait.php index 351cdc11..1ccb1dbe 100644 --- a/tests/Common/ItemsStorageTestTrait.php +++ b/tests/Common/ItemsStorageTestTrait.php @@ -4,6 +4,8 @@ namespace Yiisoft\Rbac\Tests\Common; +use DateTime; +use SlopeIt\ClockMock\ClockMock; use Yiisoft\Rbac\Item; use Yiisoft\Rbac\ItemsStorageInterface; use Yiisoft\Rbac\Permission; @@ -22,11 +24,19 @@ trait ItemsStorageTestTrait protected function setUp(): void { + if ($this->name() === 'testAddWithCurrentTimestamps') { + ClockMock::freeze(new DateTime('2023-05-10 08:24:39')); + } + $this->populateItemsStorage(); } protected function tearDown(): void { + if ($this->name() === 'testAddWithCurrentTimestamps') { + ClockMock::reset(); + } + $this->getItemsStorage()->clear(); } @@ -402,7 +412,7 @@ public function testGetRole(): void $this->assertSame('Parent 1', $role->getName()); } - public function testAdd(): void + public function testAddWithCurrentTimestamps(): void { $time = time(); $newItem = (new Permission('Delete post'))->withCreatedAt($time)->withUpdatedAt($time); @@ -411,7 +421,25 @@ public function testAdd(): void $storage->add($newItem); $storage = $this->getItemsStorageForModificationAssertions(); - $this->assertInstanceOf(Permission::class, $storage->get('Delete post')); + $this->assertEquals( + (new Permission('Delete post'))->withCreatedAt(1_683_707_079)->withUpdatedAt(1_683_707_079), + $storage->get('Delete post'), + ); + } + + public function testAddWithPastTimestamps(): void + { + $time = 1_694_508_008; + $newItem = (new Permission('Delete post'))->withCreatedAt($time)->withUpdatedAt($time); + + $storage = $this->getItemsStorage(); + $storage->add($newItem); + + $storage = $this->getItemsStorageForModificationAssertions(); + $this->assertEquals( + (new Permission('Delete post'))->withCreatedAt($time)->withUpdatedAt($time), + $storage->get('Delete post'), + ); } public function testRemoveChild(): void