Skip to content

Commit

Permalink
Add test for adding item with past timestamps (#233)
Browse files Browse the repository at this point in the history
* Add test for adding item with past timestamps

* Apply Rector changes (CI)

---------

Co-authored-by: arogachev <[email protected]>
  • Loading branch information
arogachev and arogachev authored Jan 25, 2024
1 parent 966ec24 commit 53718a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/AssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
8 changes: 4 additions & 4 deletions tests/Common/AssignmentsStorageTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand All @@ -34,7 +34,7 @@ protected function setUp(): void

protected function tearDown(): void
{
if ($this->name() === 'testAdd') {
if ($this->name() === 'testAddWithCurrentTimestamp') {
ClockMock::reset();
}

Expand Down Expand Up @@ -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()));
Expand All @@ -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));
Expand Down
32 changes: 30 additions & 2 deletions tests/Common/ItemsStorageTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 53718a7

Please sign in to comment.