132 lines
4.6 KiB
PHP
132 lines
4.6 KiB
PHP
<?php
|
|
namespace Tests\Unit\admin\Controllers;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use admin\Controllers\UsersController;
|
|
use Domain\User\UserRepository;
|
|
|
|
class UsersControllerTest extends TestCase
|
|
{
|
|
private $mockRepository;
|
|
private $controller;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->mockRepository = $this->createMock(UserRepository::class);
|
|
$this->controller = new UsersController($this->mockRepository);
|
|
}
|
|
|
|
public function testConstructorAcceptsRepository(): void
|
|
{
|
|
$controller = new UsersController($this->mockRepository);
|
|
$this->assertInstanceOf(UsersController::class, $controller);
|
|
}
|
|
|
|
public function testHasViewListMethod(): void
|
|
{
|
|
$this->assertTrue(method_exists($this->controller, 'view_list'));
|
|
}
|
|
|
|
public function testHasUserEditMethod(): void
|
|
{
|
|
$this->assertTrue(method_exists($this->controller, 'user_edit'));
|
|
}
|
|
|
|
public function testHasUserSaveMethod(): void
|
|
{
|
|
$this->assertTrue(method_exists($this->controller, 'user_save'));
|
|
}
|
|
|
|
public function testHasUserDeleteMethod(): void
|
|
{
|
|
$this->assertTrue(method_exists($this->controller, 'user_delete'));
|
|
}
|
|
|
|
public function testHasTwofaMethod(): void
|
|
{
|
|
$this->assertTrue(method_exists($this->controller, 'twofa'));
|
|
}
|
|
|
|
public function testHasLoginFormMethod(): void
|
|
{
|
|
$this->assertTrue(method_exists($this->controller, 'login_form'));
|
|
}
|
|
|
|
public function testActionMethodReturnTypes(): void
|
|
{
|
|
$reflection = new \ReflectionClass($this->controller);
|
|
|
|
$this->assertEquals('string', (string)$reflection->getMethod('view_list')->getReturnType());
|
|
$this->assertEquals('string', (string)$reflection->getMethod('user_edit')->getReturnType());
|
|
$this->assertEquals('string', (string)$reflection->getMethod('login_form')->getReturnType());
|
|
$this->assertEquals('string', (string)$reflection->getMethod('twofa')->getReturnType());
|
|
$this->assertEquals('void', (string)$reflection->getMethod('user_save')->getReturnType());
|
|
$this->assertEquals('void', (string)$reflection->getMethod('user_delete')->getReturnType());
|
|
}
|
|
|
|
public function testConstructorRequiresUserRepository(): void
|
|
{
|
|
$reflection = new \ReflectionClass(UsersController::class);
|
|
$constructor = $reflection->getConstructor();
|
|
$params = $constructor->getParameters();
|
|
|
|
$this->assertCount(1, $params);
|
|
$this->assertEquals('Domain\User\UserRepository', $params[0]->getType()->getName());
|
|
}
|
|
|
|
public function testNormalizeUserReturnsDefaultsForNull(): void
|
|
{
|
|
$reflection = new \ReflectionClass($this->controller);
|
|
$method = $reflection->getMethod('normalizeUser');
|
|
$method->setAccessible(true);
|
|
|
|
$result = $method->invoke($this->controller, null);
|
|
|
|
$this->assertSame(0, $result['id']);
|
|
$this->assertSame('', $result['login']);
|
|
$this->assertSame(1, $result['status']);
|
|
$this->assertSame(1, $result['admin']);
|
|
$this->assertSame(0, $result['twofa_enabled']);
|
|
$this->assertSame('', $result['twofa_email']);
|
|
}
|
|
|
|
public function testNormalizeUserCastsTypes(): void
|
|
{
|
|
$reflection = new \ReflectionClass($this->controller);
|
|
$method = $reflection->getMethod('normalizeUser');
|
|
$method->setAccessible(true);
|
|
|
|
$result = $method->invoke($this->controller, [
|
|
'id' => '15',
|
|
'login' => 'admin@test.com',
|
|
'status' => '0',
|
|
'admin' => '1',
|
|
'twofa_enabled' => '1',
|
|
'twofa_email' => 'twofa@test.com',
|
|
]);
|
|
|
|
$this->assertSame(15, $result['id']);
|
|
$this->assertSame('admin@test.com', $result['login']);
|
|
$this->assertSame(0, $result['status']);
|
|
$this->assertSame(1, $result['admin']);
|
|
$this->assertSame(1, $result['twofa_enabled']);
|
|
$this->assertSame('twofa@test.com', $result['twofa_email']);
|
|
}
|
|
|
|
public function testNormalizeUserHandlesPartialData(): void
|
|
{
|
|
$reflection = new \ReflectionClass($this->controller);
|
|
$method = $reflection->getMethod('normalizeUser');
|
|
$method->setAccessible(true);
|
|
|
|
$result = $method->invoke($this->controller, ['id' => 3, 'login' => 'partial@test.com']);
|
|
|
|
$this->assertSame(3, $result['id']);
|
|
$this->assertSame('partial@test.com', $result['login']);
|
|
$this->assertSame(1, $result['status']);
|
|
$this->assertSame(1, $result['admin']);
|
|
$this->assertSame(0, $result['twofa_enabled']);
|
|
$this->assertSame('', $result['twofa_email']);
|
|
}
|
|
}
|