Changed `src/` code and templates to use the new user model
parent
8e62c4c52c
commit
d15946df2d
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Helpers;
|
||||
|
||||
use Engelsystem\Models\BaseModel;
|
||||
use Engelsystem\Models\User\User;
|
||||
use Engelsystem\Models\User\User as UserRepository;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
|
||||
class Authenticator
|
||||
{
|
||||
/** @var UserRepository */
|
||||
protected $user = null;
|
||||
|
||||
/** @var Session */
|
||||
protected $session;
|
||||
|
||||
/** @var BaseModel */
|
||||
protected $userRepository;
|
||||
|
||||
/**
|
||||
* @param Session $session
|
||||
* @param UserRepository $userRepository
|
||||
*/
|
||||
public function __construct(Session $session, UserRepository $userRepository)
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return User|null
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
if ($this->user) {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
$userId = $this->session->get('uid');
|
||||
if (!$userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$user = $this
|
||||
->userRepository
|
||||
->find($userId);
|
||||
if (!$user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->user = $user;
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Helpers;
|
||||
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
|
||||
class AuthenticatorServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
/** @var Authenticator $authenticator */
|
||||
$authenticator = $this->app->make(Authenticator::class);
|
||||
|
||||
$this->app->instance(Authenticator::class, $authenticator);
|
||||
$this->app->instance('authenticator', $authenticator);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Helpers;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Engelsystem\Helpers\AuthenticatorServiceProvider;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
|
||||
class AuthenticatorServiceProviderTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\AuthenticatorServiceProvider::register()
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
$app = new Application();
|
||||
|
||||
$serviceProvider = new AuthenticatorServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
|
||||
$this->assertInstanceOf(Authenticator::class, $app->get(Authenticator::class));
|
||||
$this->assertInstanceOf(Authenticator::class, $app->get('authenticator'));
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Helpers;
|
||||
|
||||
use Engelsystem\Helpers\Authenticator;
|
||||
use Engelsystem\Models\User\User;
|
||||
use Engelsystem\Test\Unit\Helpers\Stub\UserModelImplementation;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
|
||||
class AuthenticatorTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\Authenticator::__construct(
|
||||
* @covers \Engelsystem\Helpers\Authenticator::user
|
||||
*/
|
||||
public function testUser()
|
||||
{
|
||||
/** @var Session|MockObject $session */
|
||||
$session = $this->createMock(Session::class);
|
||||
/** @var UserModelImplementation|MockObject $userRepository */
|
||||
$userRepository = new UserModelImplementation();
|
||||
/** @var User|MockObject $user */
|
||||
$user = $this->createMock(User::class);
|
||||
|
||||
$session->expects($this->exactly(3))
|
||||
->method('get')
|
||||
->with('uid')
|
||||
->willReturnOnConsecutiveCalls(
|
||||
null,
|
||||
42,
|
||||
1337
|
||||
);
|
||||
|
||||
$auth = new Authenticator($session, $userRepository);
|
||||
|
||||
// Not in session
|
||||
$this->assertEquals(null, $auth->user());
|
||||
|
||||
// Unknown user
|
||||
UserModelImplementation::$id = 42;
|
||||
$this->assertEquals(null, $auth->user());
|
||||
|
||||
// User found
|
||||
UserModelImplementation::$id = 1337;
|
||||
UserModelImplementation::$user = $user;
|
||||
$this->assertEquals($user, $auth->user());
|
||||
|
||||
// User cached
|
||||
UserModelImplementation::$id = null;
|
||||
UserModelImplementation::$user = null;
|
||||
$this->assertEquals($user, $auth->user());
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Helpers\Stub;
|
||||
|
||||
use Engelsystem\Models\User\User;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class UserModelImplementation extends User
|
||||
{
|
||||
/** @var User */
|
||||
public static $user = null;
|
||||
|
||||
/** @var int */
|
||||
public static $id = null;
|
||||
|
||||
/**
|
||||
* @param mixed $id
|
||||
* @param array $columns
|
||||
* @return User|null
|
||||
*/
|
||||
public static function find($id, $columns = ['*'])
|
||||
{
|
||||
if ($id != static::$id) {
|
||||
throw new InvalidArgumentException('Wrong user ID searched');
|
||||
}
|
||||
|
||||
return self::$user;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue