From 46d2da7ba7e1004d7ebac1e9f9c74c65a429d5e1 Mon Sep 17 00:00:00 2001 From: leo108 Date: Tue, 3 Apr 2018 19:45:13 +0800 Subject: [PATCH] move cas_route into namespace --- src/CASServerServiceProvider.php | 3 +- src/Http/Controllers/SecurityController.php | 1 + src/Utils/helpers.php | 22 - src/helpers.php | 22 + .../Controllers/SecurityControllerTest.php | 738 +++++++++--------- 5 files changed, 397 insertions(+), 389 deletions(-) delete mode 100644 src/Utils/helpers.php create mode 100644 src/helpers.php diff --git a/src/CASServerServiceProvider.php b/src/CASServerServiceProvider.php index ac6866f..a12f91e 100644 --- a/src/CASServerServiceProvider.php +++ b/src/CASServerServiceProvider.php @@ -21,7 +21,6 @@ class CASServerServiceProvider extends ServiceProvider */ public function register() { - // TODO: Implement register() method. } /** @@ -29,7 +28,7 @@ public function register() */ public function boot() { - require __DIR__.'/Utils/helpers.php'; + require __DIR__.'/helpers.php'; if (!$this->app->routesAreCached()) { require __DIR__.'/Http/routes.php'; diff --git a/src/Http/Controllers/SecurityController.php b/src/Http/Controllers/SecurityController.php index 9d56f9d..9e398dc 100644 --- a/src/Http/Controllers/SecurityController.php +++ b/src/Http/Controllers/SecurityController.php @@ -17,6 +17,7 @@ use Leo108\CAS\Repositories\PGTicketRepository; use Leo108\CAS\Repositories\ServiceRepository; use Leo108\CAS\Repositories\TicketRepository; +use function Leo108\CAS\cas_route; class SecurityController extends Controller { diff --git a/src/Utils/helpers.php b/src/Utils/helpers.php deleted file mode 100644 index f0432a4..0000000 --- a/src/Utils/helpers.php +++ /dev/null @@ -1,22 +0,0 @@ -cas_route($name, $query); + //mock function + function cas_route($name, $query) + { + return SecurityControllerTest::$functions->cas_route($name, $query); + } } -class SecurityControllerTest extends TestCase -{ - public static $functions; +namespace Leo108\CAS\Http\Controllers { - public function setUp() - { - parent::setUp(); - self::$functions = Mockery::mock(); - } + use Illuminate\Http\RedirectResponse; + use Illuminate\Http\Request; + use Leo108\CAS\Contracts\Interactions\UserLogin; + use Leo108\CAS\Contracts\Models\UserModel; + use Leo108\CAS\Events\CasUserLoginEvent; + use Leo108\CAS\Events\CasUserLogoutEvent; + use Leo108\CAS\Exceptions\CAS\CasException; + use Leo108\CAS\Repositories\PGTicketRepository; + use Leo108\CAS\Repositories\ServiceRepository; + use Leo108\CAS\Repositories\TicketRepository; + use TestCase; + use Mockery; - public function testShowLoginWithValidServiceUrl() + class SecurityControllerTest extends TestCase { - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('what ever') - ->once() - ->getMock(); - $serviceRepository = Mockery::mock(ServiceRepository::class) - ->shouldReceive('isUrlValid') - ->andReturn(true) - ->once() - ->getMock(); - app()->instance(ServiceRepository::class, $serviceRepository); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('showLoginPage') - ->with($request, []) - ->andReturn('show login called') - ->once() - ->shouldReceive('getCurrentUser') - ->andReturn(false) - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $this->assertEquals('show login called', app(SecurityController::class)->showLogin($request)); - } + public static $functions; - public function testShowLoginWithInvalidServiceUrl() - { - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('what ever') - ->once() - ->getMock(); - $serviceRepository = Mockery::mock(ServiceRepository::class) - ->shouldReceive('isUrlValid') - ->andReturn(false) - ->once() - ->getMock(); - app()->instance(ServiceRepository::class, $serviceRepository); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('showLoginPage') - ->with($request, [CasException::INVALID_SERVICE]) - ->andReturn('show login called') - ->once() - ->shouldReceive('getCurrentUser') - ->andReturn(false) - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); + public function setUp() + { + parent::setUp(); + self::$functions = Mockery::mock(); + } - $this->assertEquals('show login called', app(SecurityController::class)->showLogin($request)); - } + public function testShowLoginWithValidServiceUrl() + { + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->once() + ->getMock(); + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(true) + ->once() + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('showLoginPage') + ->with($request, []) + ->andReturn('show login called') + ->once() + ->shouldReceive('getCurrentUser') + ->andReturn(false) + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $this->assertEquals('show login called', app(SecurityController::class)->showLogin($request)); + } - public function testShowLoginWhenLoggedInWithValidServiceUrlWithoutWarn() - { - $serviceRepository = Mockery::mock(ServiceRepository::class) - ->shouldReceive('isUrlValid') - ->andReturn(true) - ->once() - ->getMock(); - app()->instance(ServiceRepository::class, $serviceRepository); - $user = Mockery::mock(UserModel::class); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('getCurrentUser') - ->andReturn($user) - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('what ever') - ->once() - ->shouldReceive('get') - ->withArgs(['warn']) - ->andReturn(false) - ->once() - ->getMock(); - $controller = $this->initController() - ->makePartial() - ->shouldReceive('authenticated') - ->withArgs([$request, $user]) - ->andReturn('authenticated called') - ->once() - ->getMock(); - $this->assertEquals('authenticated called', $controller->showLogin($request)); - } + public function testShowLoginWithInvalidServiceUrl() + { + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->once() + ->getMock(); + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(false) + ->once() + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('showLoginPage') + ->with($request, [CasException::INVALID_SERVICE]) + ->andReturn('show login called') + ->once() + ->shouldReceive('getCurrentUser') + ->andReturn(false) + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); - public function testShowLoginWhenLoggedInWithValidServiceUrlWithWarn() - { - $serviceRepository = Mockery::mock(ServiceRepository::class) - ->shouldReceive('isUrlValid') - ->andReturn(true) - ->once() - ->getMock(); - app()->instance(ServiceRepository::class, $serviceRepository); - $ticketRepository = Mockery::mock(TicketRepository::class); - app()->instance(TicketRepository::class, $ticketRepository); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('getCurrentUser') - ->andReturn(true)//just not false is OK - ->once() - ->shouldReceive('showLoginWarnPage') - ->andReturn('showLoginWarnPage called') - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('what ever') - ->once() - ->shouldReceive('get') - ->withArgs(['warn']) - ->andReturn('true') - ->once() - ->getMock(); - $request->query = Mockery::mock() - ->shouldReceive('all') - ->andReturn([]) - ->once() - ->getMock(); - self::$functions->shouldReceive('cas_route')->andReturn('some string')->once(); - $this->assertEquals('showLoginWarnPage called', app(SecurityController::class)->showLogin($request)); - } + $this->assertEquals('show login called', app(SecurityController::class)->showLogin($request)); + } - public function testShowLoginWhenLoggedInWithInvalidServiceUrl() - { - $serviceRepository = Mockery::mock(ServiceRepository::class) - ->shouldReceive('isUrlValid') - ->andReturn(false) - ->once() - ->getMock(); - app()->instance(ServiceRepository::class, $serviceRepository); - $ticketRepository = Mockery::mock(TicketRepository::class); - app()->instance(TicketRepository::class, $ticketRepository); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('getCurrentUser') - ->andReturn(true)//just not false is OK - ->once() - ->shouldReceive('redirectToHome') - ->with([CasException::INVALID_SERVICE]) - ->andReturn('redirectToHome called') - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('what ever') - ->once() - ->getMock(); - $this->assertEquals('redirectToHome called', app(SecurityController::class)->showLogin($request)); - } + public function testShowLoginWhenLoggedInWithValidServiceUrlWithoutWarn() + { + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(true) + ->once() + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $user = Mockery::mock(UserModel::class); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('getCurrentUser') + ->andReturn($user) + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->once() + ->shouldReceive('get') + ->withArgs(['warn']) + ->andReturn(false) + ->once() + ->getMock(); + $controller = $this->initController() + ->makePartial() + ->shouldReceive('authenticated') + ->withArgs([$request, $user]) + ->andReturn('authenticated called') + ->once() + ->getMock(); + $this->assertEquals('authenticated called', $controller->showLogin($request)); + } - public function testAuthenticatedWithoutService() - { - $user = Mockery::mock(UserModel::class); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('redirectToHome') - ->andReturn('redirectToHome called') - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('') - ->once() - ->getMock(); - $this->expectsEvents(CasUserLoginEvent::class); - $this->assertEquals('redirectToHome called', app(SecurityController::class)->authenticated($request, $user)); - } + public function testShowLoginWhenLoggedInWithValidServiceUrlWithWarn() + { + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(true) + ->once() + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $ticketRepository = Mockery::mock(TicketRepository::class); + app()->instance(TicketRepository::class, $ticketRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('getCurrentUser') + ->andReturn(true)//just not false is OK + ->once() + ->shouldReceive('showLoginWarnPage') + ->andReturn('showLoginWarnPage called') + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->once() + ->shouldReceive('get') + ->withArgs(['warn']) + ->andReturn('true') + ->once() + ->getMock(); + $request->query = Mockery::mock() + ->shouldReceive('all') + ->andReturn([]) + ->once() + ->getMock(); + self::$functions->shouldReceive('cas_route')->andReturn('some string')->once(); + $this->assertEquals('showLoginWarnPage called', app(SecurityController::class)->showLogin($request)); + } - public function testAuthenticatedWithService() - { - //with service url but apply ticket failed - $user = Mockery::mock(UserModel::class); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('redirectToHome') - ->with([CasException::INTERNAL_ERROR]) - ->andReturn('redirectToHome called') - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $ticketRepository = Mockery::mock(TicketRepository::class) - ->shouldReceive('applyTicket') - ->andThrow(new CasException(CasException::INTERNAL_ERROR)) - ->once() - ->getMock(); - app()->instance(TicketRepository::class, $ticketRepository); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('http://leo108.com') - ->once() - ->getMock(); - $this->expectsEvents(CasUserLoginEvent::class); - $this->assertEquals('redirectToHome called', app(SecurityController::class)->authenticated($request, $user)); + public function testShowLoginWhenLoggedInWithInvalidServiceUrl() + { + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(false) + ->once() + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $ticketRepository = Mockery::mock(TicketRepository::class); + app()->instance(TicketRepository::class, $ticketRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('getCurrentUser') + ->andReturn(true)//just not false is OK + ->once() + ->shouldReceive('redirectToHome') + ->with([CasException::INVALID_SERVICE]) + ->andReturn('redirectToHome called') + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('what ever') + ->once() + ->getMock(); + $this->assertEquals('redirectToHome called', app(SecurityController::class)->showLogin($request)); + } - //with service url - $ticket = Mockery::mock(); - $ticket->ticket = 'ST-abc'; - $ticketRepository = Mockery::mock(TicketRepository::class) - ->shouldReceive('applyTicket') - ->andReturn($ticket) - ->once() - ->getMock(); - app()->instance(TicketRepository::class, $ticketRepository); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service', '']) - ->andReturn('http://leo108.com') - ->once() - ->getMock(); - $this->expectsEvents(CasUserLoginEvent::class); - $resp = app(SecurityController::class)->authenticated($request, $user); - $this->assertInstanceOf(RedirectResponse::class, $resp); - $this->assertEquals($resp->getTargetUrl(), 'http://leo108.com?ticket=ST-abc'); - } + public function testAuthenticatedWithoutService() + { + $user = Mockery::mock(UserModel::class); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('redirectToHome') + ->andReturn('redirectToHome called') + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('') + ->once() + ->getMock(); + $this->expectsEvents(CasUserLoginEvent::class); + $this->assertEquals('redirectToHome called', + app(SecurityController::class)->authenticated($request, $user)); + } - public function testLogoutWhenNotLoggedInWithoutService() - { - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service']) - ->andReturn(null) - ->once() - ->getMock(); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('getCurrentUser') - ->andReturn(false) - ->once() - ->shouldReceive('showLoggedOut') - ->with($request) - ->andReturn('showLoggedOut called') - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $this->doesntExpectEvents(CasUserLogoutEvent::class); - $this->assertEquals('showLoggedOut called', app(SecurityController::class)->logout($request)); - } + public function testAuthenticatedWithService() + { + //with service url but apply ticket failed + $user = Mockery::mock(UserModel::class); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('redirectToHome') + ->with([CasException::INTERNAL_ERROR]) + ->andReturn('redirectToHome called') + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $ticketRepository = Mockery::mock(TicketRepository::class) + ->shouldReceive('applyTicket') + ->andThrow(new CasException(CasException::INTERNAL_ERROR)) + ->once() + ->getMock(); + app()->instance(TicketRepository::class, $ticketRepository); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('http://leo108.com') + ->once() + ->getMock(); + $this->expectsEvents(CasUserLoginEvent::class); + $this->assertEquals('redirectToHome called', + app(SecurityController::class)->authenticated($request, $user)); - public function testLogoutWithoutService() - { - $user = Mockery::mock(UserModel::class); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service']) - ->andReturn(null) - ->once() - ->getMock(); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('logout') - ->once() - ->shouldReceive('getCurrentUser') - ->andReturn($user) - ->once() - ->shouldReceive('showLoggedOut') - ->with($request) - ->andReturn('showLoggedOut called') - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $pgTicketRepository = Mockery::mock(PGTicketRepository::class) - ->shouldReceive('invalidTicketByUser') - ->with($user) - ->once() - ->getMock(); - app()->instance(PGTicketRepository::class, $pgTicketRepository); - $this->expectsEvents(CasUserLogoutEvent::class); - $this->assertEquals('showLoggedOut called', app(SecurityController::class)->logout($request)); - } + //with service url + $ticket = Mockery::mock(); + $ticket->ticket = 'ST-abc'; + $ticketRepository = Mockery::mock(TicketRepository::class) + ->shouldReceive('applyTicket') + ->andReturn($ticket) + ->once() + ->getMock(); + app()->instance(TicketRepository::class, $ticketRepository); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service', '']) + ->andReturn('http://leo108.com') + ->once() + ->getMock(); + $this->expectsEvents(CasUserLoginEvent::class); + $resp = app(SecurityController::class)->authenticated($request, $user); + $this->assertInstanceOf(RedirectResponse::class, $resp); + $this->assertEquals($resp->getTargetUrl(), 'http://leo108.com?ticket=ST-abc'); + } - public function testLogoutWithValidService() - { - $user = Mockery::mock(UserModel::class); - $serviceRepository = Mockery::mock(ServiceRepository::class) - ->shouldReceive('isUrlValid') - ->andReturn(true) - ->once() - ->getMock(); - app()->instance(ServiceRepository::class, $serviceRepository); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('logout') - ->once() - ->shouldReceive('getCurrentUser') - ->andReturn($user) - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $request = Mockery::mock(Request::class) - ->shouldReceive('get') - ->withArgs(['service']) - ->andReturn('http://leo108.com') - ->once() - ->getMock(); - $pgTicketRepository = Mockery::mock(PGTicketRepository::class) - ->shouldReceive('invalidTicketByUser') - ->with($user) - ->once() - ->getMock(); - app()->instance(PGTicketRepository::class, $pgTicketRepository); - $this->expectsEvents(CasUserLogoutEvent::class); - $resp = app(SecurityController::class)->logout($request); - $this->assertInstanceOf(RedirectResponse::class, $resp); - } + public function testLogoutWhenNotLoggedInWithoutService() + { + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service']) + ->andReturn(null) + ->once() + ->getMock(); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('getCurrentUser') + ->andReturn(false) + ->once() + ->shouldReceive('showLoggedOut') + ->with($request) + ->andReturn('showLoggedOut called') + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $this->doesntExpectEvents(CasUserLogoutEvent::class); + $this->assertEquals('showLoggedOut called', app(SecurityController::class)->logout($request)); + } - public function testLogin() - { - $request = Mockery::mock(Request::class); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('login') - ->andReturn(null) - ->once() - ->shouldReceive('showAuthenticateFailed') - ->andReturn('showAuthenticateFailed called') - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $this->assertEquals('showAuthenticateFailed called', app(SecurityController::class)->login($request)); + public function testLogoutWithoutService() + { + $user = Mockery::mock(UserModel::class); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service']) + ->andReturn(null) + ->once() + ->getMock(); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('logout') + ->once() + ->shouldReceive('getCurrentUser') + ->andReturn($user) + ->once() + ->shouldReceive('showLoggedOut') + ->with($request) + ->andReturn('showLoggedOut called') + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $pgTicketRepository = Mockery::mock(PGTicketRepository::class) + ->shouldReceive('invalidTicketByUser') + ->with($user) + ->once() + ->getMock(); + app()->instance(PGTicketRepository::class, $pgTicketRepository); + $this->expectsEvents(CasUserLogoutEvent::class); + $this->assertEquals('showLoggedOut called', app(SecurityController::class)->logout($request)); + } - $user = Mockery::mock(UserModel::class); - $loginInteraction = Mockery::mock(UserLogin::class) - ->shouldReceive('login') - ->andReturn($user) - ->once() - ->getMock(); - app()->instance(UserLogin::class, $loginInteraction); - $request = Mockery::mock(Request::class); - $controller = $this->initController() - ->makePartial() - ->shouldReceive('authenticated') - ->withArgs([$request, $user]) - ->andReturn('authenticated called') - ->once() - ->getMock(); - $this->assertEquals('authenticated called', $controller->login($request)); - } + public function testLogoutWithValidService() + { + $user = Mockery::mock(UserModel::class); + $serviceRepository = Mockery::mock(ServiceRepository::class) + ->shouldReceive('isUrlValid') + ->andReturn(true) + ->once() + ->getMock(); + app()->instance(ServiceRepository::class, $serviceRepository); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('logout') + ->once() + ->shouldReceive('getCurrentUser') + ->andReturn($user) + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class) + ->shouldReceive('get') + ->withArgs(['service']) + ->andReturn('http://leo108.com') + ->once() + ->getMock(); + $pgTicketRepository = Mockery::mock(PGTicketRepository::class) + ->shouldReceive('invalidTicketByUser') + ->with($user) + ->once() + ->getMock(); + app()->instance(PGTicketRepository::class, $pgTicketRepository); + $this->expectsEvents(CasUserLogoutEvent::class); + $resp = app(SecurityController::class)->logout($request); + $this->assertInstanceOf(RedirectResponse::class, $resp); + } - /** - * @return Mockery\MockInterface - */ - protected function initController() - { - return Mockery::mock( - SecurityController::class, - [ - app(ServiceRepository::class), - app(TicketRepository::class), - app(PGTicketRepository::class), - app(UserLogin::class), - ] - ); + public function testLogin() + { + $request = Mockery::mock(Request::class); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('login') + ->andReturn(null) + ->once() + ->shouldReceive('showAuthenticateFailed') + ->andReturn('showAuthenticateFailed called') + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $this->assertEquals('showAuthenticateFailed called', app(SecurityController::class)->login($request)); + + $user = Mockery::mock(UserModel::class); + $loginInteraction = Mockery::mock(UserLogin::class) + ->shouldReceive('login') + ->andReturn($user) + ->once() + ->getMock(); + app()->instance(UserLogin::class, $loginInteraction); + $request = Mockery::mock(Request::class); + $controller = $this->initController() + ->makePartial() + ->shouldReceive('authenticated') + ->withArgs([$request, $user]) + ->andReturn('authenticated called') + ->once() + ->getMock(); + $this->assertEquals('authenticated called', $controller->login($request)); + } + + /** + * @return Mockery\MockInterface + */ + protected function initController() + { + return Mockery::mock( + SecurityController::class, + [ + app(ServiceRepository::class), + app(TicketRepository::class), + app(PGTicketRepository::class), + app(UserLogin::class), + ] + ); + } } }