|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Identity\Model; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use PhpList\Core\Domain\Identity\Model\Privileges; |
| 9 | +use PhpList\Core\Domain\Identity\Model\PrivilegeFlag; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * Testcase for the Privileges class. |
| 14 | + */ |
| 15 | +class PrivilegesTest extends TestCase |
| 16 | +{ |
| 17 | + private Privileges $subject; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->subject = Privileges::fromSerialized(''); |
| 22 | + } |
| 23 | + |
| 24 | + public function testFromSerializedWithInvalidDataThrowsError(): void |
| 25 | + { |
| 26 | + $this->expectException(InvalidArgumentException::class); |
| 27 | + $this->expectExceptionMessage('Invalid serialized privileges string.'); |
| 28 | + |
| 29 | + Privileges::fromSerialized('invalid data'); |
| 30 | + } |
| 31 | + |
| 32 | + public function testFromSerializedWithValidDataReturnsPopulatedPrivileges(): void |
| 33 | + { |
| 34 | + $data = [PrivilegeFlag::Subscribers->value => true]; |
| 35 | + $serialized = serialize($data); |
| 36 | + |
| 37 | + $privileges = Privileges::fromSerialized($serialized); |
| 38 | + |
| 39 | + self::assertTrue($privileges->has(PrivilegeFlag::Subscribers)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testToSerializedReturnsSerializedData(): void |
| 43 | + { |
| 44 | + $privileges = Privileges::fromSerialized(''); |
| 45 | + $privileges = $privileges->grant(PrivilegeFlag::Subscribers); |
| 46 | + |
| 47 | + $serialized = $privileges->toSerialized(); |
| 48 | + $data = unserialize($serialized); |
| 49 | + |
| 50 | + self::assertTrue($data[PrivilegeFlag::Subscribers->value]); |
| 51 | + } |
| 52 | + |
| 53 | + public function testHasReturnsFalseForUnsetPrivilege(): void |
| 54 | + { |
| 55 | + self::assertFalse($this->subject->has(PrivilegeFlag::Subscribers)); |
| 56 | + } |
| 57 | + |
| 58 | + public function testHasReturnsTrueForSetPrivilege(): void |
| 59 | + { |
| 60 | + $this->subject = $this->subject->grant(PrivilegeFlag::Subscribers); |
| 61 | + |
| 62 | + self::assertTrue($this->subject->has(PrivilegeFlag::Subscribers)); |
| 63 | + } |
| 64 | + |
| 65 | + public function testGrantSetsPrivilege(): void |
| 66 | + { |
| 67 | + $result = $this->subject->grant(PrivilegeFlag::Subscribers); |
| 68 | + |
| 69 | + self::assertTrue($result->has(PrivilegeFlag::Subscribers)); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGrantReturnsNewInstance(): void |
| 73 | + { |
| 74 | + $result = $this->subject->grant(PrivilegeFlag::Subscribers); |
| 75 | + |
| 76 | + self::assertNotSame($this->subject, $result); |
| 77 | + } |
| 78 | + |
| 79 | + public function testRevokeClearsPrivilege(): void |
| 80 | + { |
| 81 | + $this->subject = $this->subject->grant(PrivilegeFlag::Subscribers); |
| 82 | + $result = $this->subject->revoke(PrivilegeFlag::Subscribers); |
| 83 | + |
| 84 | + self::assertFalse($result->has(PrivilegeFlag::Subscribers)); |
| 85 | + } |
| 86 | + |
| 87 | + public function testRevokeReturnsNewInstance(): void |
| 88 | + { |
| 89 | + $result = $this->subject->revoke(PrivilegeFlag::Subscribers); |
| 90 | + |
| 91 | + self::assertNotSame($this->subject, $result); |
| 92 | + } |
| 93 | + |
| 94 | + public function testAllReturnsAllPrivileges(): void |
| 95 | + { |
| 96 | + $this->subject = $this->subject->grant(PrivilegeFlag::Subscribers); |
| 97 | + $all = $this->subject->all(); |
| 98 | + |
| 99 | + self::assertTrue($all[PrivilegeFlag::Subscribers->value]); |
| 100 | + self::assertFalse($all[PrivilegeFlag::Campaigns->value]); |
| 101 | + } |
| 102 | +} |
0 commit comments