| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Users\User; |
| 6 | use Liuch\DmarcSrg\Users\AdminUser; |
| 7 | |
| 8 | class AdminUserTest extends \PHPUnit\Framework\TestCase |
| 9 | { |
| 10 | private $user = null; |
| 11 | |
| 12 | public function setUp(): void |
| 13 | { |
| 14 | $this->user = new AdminUser($this->getCore()); |
| 15 | } |
| 16 | |
| 17 | public function testIfExist(): void |
| 18 | { |
| 19 | $this->assertTrue($this->user->exists()); |
| 20 | } |
| 21 | |
| 22 | public function testId(): void |
| 23 | { |
| 24 | $this->assertSame($this->user->id(), 0); |
| 25 | } |
| 26 | |
| 27 | public function testName(): void |
| 28 | { |
| 29 | $this->assertSame($this->user->name(), 'admin'); |
| 30 | } |
| 31 | |
| 32 | public function testLevel(): void |
| 33 | { |
| 34 | $this->assertSame($this->user->level(), User::LEVEL_ADMIN); |
| 35 | } |
| 36 | |
| 37 | public function testIsEnabled(): void |
| 38 | { |
| 39 | $this->assertTrue($this->user->isEnabled()); |
| 40 | } |
| 41 | |
| 42 | public function testToArray(): void |
| 43 | { |
| 44 | $this->assertSame( |
| 45 | [ 'id' => 0, 'name' => 'admin', 'level' => User::LEVEL_ADMIN, 'enabled' => true ], |
| 46 | $this->user->toArray() |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function testWrongPassword(): void |
| 51 | { |
| 52 | $this->assertFalse( |
| 53 | (new AdminUser($this->getCoreWithConfigValue('admin/password', 'some')))->verifyPassword('fake') |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public function testEmptyPassword(): void |
| 58 | { |
| 59 | $this->assertFalse( |
| 60 | (new AdminUser($this->getCoreWithConfigValue('admin/password', 'some')))->verifyPassword('') |
| 61 | ); |
| 62 | $this->assertFalse( |
| 63 | (new AdminUser($this->getCoreWithConfigValue('admin/password', '')))->verifyPassword('') |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | public function testCorrectPassword(): void |
| 68 | { |
| 69 | $this->assertTrue( |
| 70 | (new AdminUser($this->getCoreWithConfigValue('admin/password', 'some')))->verifyPassword('some') |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | private function getCore(): object |
| 75 | { |
| 76 | return $this->getMockBuilder(Core::class)->disableOriginalConstructor()->onlyMethods([ 'config' ])->getMock(); |
| 77 | } |
| 78 | |
| 79 | private function getCoreWithConfigValue(string $key, $value) |
| 80 | { |
| 81 | $core = $this->getCore(); |
| 82 | $core->expects($this->once())->method('config')->with($this->equalTo($key))->willReturn($value); |
| 83 | return $core; |
| 84 | } |
| 85 | } |