55namespace Yiisoft \Rbac \Tests \Common ;
66
77use Closure ;
8- use DateTime ;
8+ use DateTimeImmutable ;
99use InvalidArgumentException ;
1010use RuntimeException ;
11- use SlopeIt \ClockMock \ClockMock ;
1211use Yiisoft \Rbac \Assignment ;
1312use Yiisoft \Rbac \Exception \DefaultRolesNotFoundException ;
1413use Yiisoft \Rbac \Exception \ItemAlreadyExistsException ;
3029
3130trait ManagerLogicTestTrait
3231{
33- private static array $ frozenTimeTests = ['testAssign ' , 'testDataPersistency ' ];
34-
35- protected function setUp (): void
36- {
37- if (in_array ($ this ->name (), self ::$ frozenTimeTests , strict: true )) {
38- ClockMock::freeze (new DateTime ('2023-05-10 08:24:39 ' ));
39- }
40- }
41-
42- protected function tearDown (): void
43- {
44- if (in_array ($ this ->name (), self ::$ frozenTimeTests , strict: true )) {
45- ClockMock::reset ();
46- }
47- }
48-
4932 public static function dataUserHasPermissionGeneric (): array
5033 {
5134 return [
@@ -543,15 +526,15 @@ public function testHasChild(): void
543526 $ this ->assertFalse ($ manager ->hasChild ('reader ' , 'createPost ' ));
544527 }
545528
546- /**
547- * Relies on {@see ClockMock} for testing timestamp. When using with other PHPUnit classes / traits, make sure to
548- * call {@see setUp} and {@see tearDown} methods explicitly.
549- */
550529 public function testAssign (): void
551530 {
552531 $ itemsStorage = $ this ->createItemsStorage ();
553532 $ assignmentsStorage = $ this ->createAssignmentsStorage ();
554- $ manager = $ this ->createManager ($ itemsStorage , $ assignmentsStorage )
533+ $ manager = $ this ->createManager (
534+ $ itemsStorage ,
535+ $ assignmentsStorage ,
536+ currentDateTime: new DateTimeImmutable ('2023-05-10 08:24:39 ' )
537+ )
555538 ->addRole (new Role ('author ' ))
556539 ->addRole (new Role ('reader ' ))
557540 ->addRole (new Role ('writer ' ))
@@ -895,12 +878,12 @@ public static function dataSetDefaultRoleNamesException(): array
895878 return [
896879 [['test1 ' , 2 , 'test3 ' ], InvalidArgumentException::class, 'Each role name must be a string. ' ],
897880 [
898- static fn (): string => 'test ' ,
881+ static fn (): string => 'test ' ,
899882 InvalidArgumentException::class,
900883 'Default role names closure must return an array. ' ,
901884 ],
902885 [
903- static fn (): array => ['test1 ' , 2 , 'test3 ' ],
886+ static fn (): array => ['test1 ' , 2 , 'test3 ' ],
904887 InvalidArgumentException::class,
905888 'Each role name must be a string. ' ,
906889 ],
@@ -925,10 +908,10 @@ public static function dataSetDefaultRoleNames(): array
925908 return [
926909 [['defaultRole1 ' ], ['defaultRole1 ' ]],
927910 [['defaultRole1 ' , 'defaultRole2 ' ], ['defaultRole1 ' , 'defaultRole2 ' ]],
928- [static fn (): array => ['defaultRole1 ' ], ['defaultRole1 ' ]],
929- [static fn (): array => ['defaultRole1 ' , 'defaultRole2 ' ], ['defaultRole1 ' , 'defaultRole2 ' ]],
911+ [static fn (): array => ['defaultRole1 ' ], ['defaultRole1 ' ]],
912+ [static fn (): array => ['defaultRole1 ' , 'defaultRole2 ' ], ['defaultRole1 ' , 'defaultRole2 ' ]],
930913 [[], []],
931- [static fn (): array => [], []],
914+ [static fn (): array => [], []],
932915 ];
933916 }
934917
@@ -1111,7 +1094,11 @@ public function testDataPersistency(): void
11111094 {
11121095 $ itemsStorage = new FakeItemsStorage ();
11131096 $ assignmentsStorage = new FakeAssignmentsStorage ();
1114- $ manager = $ this ->createManager ($ itemsStorage , $ assignmentsStorage );
1097+ $ manager = $ this ->createManager (
1098+ $ itemsStorage ,
1099+ $ assignmentsStorage ,
1100+ currentDateTime: new DateTimeImmutable ('2023-05-10 08:24:39 ' ),
1101+ );
11151102 $ manager
11161103 ->addRole ((new Role ('role1 ' ))->withCreatedAt (1_694_502_936 )->withUpdatedAt (1_694_502_936 ))
11171104 ->addRole ((new Role ('role2 ' ))->withCreatedAt (1_694_502_976 )->withUpdatedAt (1_694_502_976 ))
0 commit comments