From 79c6d6b22e836c5abef26ce86a4e79edbebada4a Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:11:24 +0100 Subject: [PATCH 1/9] [test] Unit test for the PasswordGrantExtension --- .../GrantType/PasswordGrantExtensionTest.php | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php new file mode 100644 index 0000000..4cab37c --- /dev/null +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -0,0 +1,139 @@ +prophesize(AccountLoaderInterface::class)->reveal(), + $this->prophesize(UserPasswordEncoderInterface::class)->reveal() + ); + $passwordGrantExtension->configureRequestParameters($optionsResolver); + + $actualDefinedOptions = $optionsResolver->getDefinedOptions(); + $expectedDefinedOptions = ['password', 'username']; + $this->assertEquals($expectedDefinedOptions, $actualDefinedOptions, '', $delta = 0.0, 10, true); // Not caring about keys + } + + public function testSuccessGrant() + { + // Mocking AccountInterface + /** @var AccountInterface $account */ + $account = $this->prophesize(AccountInterface::class)->reveal(); + + // Mocking AccountLoaderInterface + $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); + $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn($account)->shouldBeCalled(); + /** @var AccountLoaderInterface $accountLoader */ + $accountLoader = $accountLoaderMock->reveal(); + + // Mocking UserPasswordEncoderInterface + $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); + $userPasswordEncoderMock->isPasswordValid(Argument::type(UserInterface::class), 'password_test')->willReturn(true)->shouldBeCalled(); + /** @var UserPasswordEncoderInterface $userPasswordEncoder */ + $userPasswordEncoder = $userPasswordEncoderMock->reveal(); + + $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); + + // Mocking ApplicationInterface + $applicationMock = $this->prophesize(ApplicationInterface::class); + /** @var ApplicationInterface $application */ + $application = $applicationMock->reveal(); + + // Mocking LoginAttempt + $loginAttemptMock = $this->prophesize(LoginAttempt::class); + $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); + $loginAttemptMock->getData('password')->willReturn('password_test')->shouldBeCalled(); + /** @var LoginAttempt $loginAttempt */ + $loginAttempt = $loginAttemptMock->reveal(); + + $actualAccount = $passwordGrantExtension->grant($application, $loginAttempt); + $this->assertSame($account, $actualAccount); + } + + public function testGrantFailingAccountLoading() + { + // Mocking AccountInterface + /** @var AccountInterface $account */ + $account = $this->prophesize(AccountInterface::class)->reveal(); + + // Mocking AccountLoaderInterface + $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); + $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn(null)->shouldBeCalled(); + /** @var AccountLoaderInterface $accountLoader */ + $accountLoader = $accountLoaderMock->reveal(); + + // Mocking UserPasswordEncoderInterface + $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); + /** @var UserPasswordEncoderInterface $userPasswordEncoder */ + $userPasswordEncoder = $userPasswordEncoderMock->reveal(); + + $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); + + // Mocking ApplicationInterface + $applicationMock = $this->prophesize(ApplicationInterface::class); + /** @var ApplicationInterface $application */ + $application = $applicationMock->reveal(); + + // Mocking LoginAttempt + $loginAttemptMock = $this->prophesize(LoginAttempt::class); + $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); + /** @var LoginAttempt $loginAttempt */ + $loginAttempt = $loginAttemptMock->reveal(); + + $this->expectException(InvalidGrantException::class); + $passwordGrantExtension->grant($application, $loginAttempt); + } + + public function testGrantFailingPasswordValidation() + { + // Mocking AccountInterface + /** @var AccountInterface $account */ + $account = $this->prophesize(AccountInterface::class)->reveal(); + + // Mocking AccountLoaderInterface + $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); + $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn($account)->shouldBeCalled(); + /** @var AccountLoaderInterface $accountLoader */ + $accountLoader = $accountLoaderMock->reveal(); + + // Mocking UserPasswordEncoderInterface + $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); + $userPasswordEncoderMock->isPasswordValid(Argument::type(UserInterface::class), 'password_test')->willReturn(false)->shouldBeCalled(); + /** @var UserPasswordEncoderInterface $userPasswordEncoder */ + $userPasswordEncoder = $userPasswordEncoderMock->reveal(); + + $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); + + // Mocking ApplicationInterface + $applicationMock = $this->prophesize(ApplicationInterface::class); + /** @var ApplicationInterface $application */ + $application = $applicationMock->reveal(); + + // Mocking LoginAttempt + $loginAttemptMock = $this->prophesize(LoginAttempt::class); + $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); + $loginAttemptMock->getData('password')->willReturn('password_test')->shouldBeCalled(); + /** @var LoginAttempt $loginAttempt */ + $loginAttempt = $loginAttemptMock->reveal(); + $this->expectException(InvalidGrantException::class); + + $passwordGrantExtension->grant($application, $loginAttempt); + } +} From fbd2dd5c802ee8e97409c1024092027b056036d2 Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:15:48 +0100 Subject: [PATCH 2/9] remove unused code --- .../OAuth/Tests/GrantType/PasswordGrantExtensionTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index 4cab37c..c75811e 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -69,10 +69,6 @@ public function testSuccessGrant() public function testGrantFailingAccountLoading() { - // Mocking AccountInterface - /** @var AccountInterface $account */ - $account = $this->prophesize(AccountInterface::class)->reveal(); - // Mocking AccountLoaderInterface $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn(null)->shouldBeCalled(); @@ -133,7 +129,7 @@ public function testGrantFailingPasswordValidation() /** @var LoginAttempt $loginAttempt */ $loginAttempt = $loginAttemptMock->reveal(); $this->expectException(InvalidGrantException::class); - + $passwordGrantExtension->grant($application, $loginAttempt); } } From cc0d41e7dedd9b385de137b26972f2fbe512766a Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:18:36 +0100 Subject: [PATCH 3/9] add phpdoc --- .../Tests/GrantType/PasswordGrantExtensionTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index c75811e..b1d99e1 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -17,6 +17,9 @@ */ class PasswordGrantExtensionTest extends \PHPUnit_Framework_TestCase { + /** + * Test configureRequestParameters() method. + */ public function testConfigureRequestParameters() { $optionsResolver = new OptionsResolver(); @@ -31,6 +34,9 @@ public function testConfigureRequestParameters() $this->assertEquals($expectedDefinedOptions, $actualDefinedOptions, '', $delta = 0.0, 10, true); // Not caring about keys } + /** + * Test grant() method on success + */ public function testSuccessGrant() { // Mocking AccountInterface @@ -67,6 +73,9 @@ public function testSuccessGrant() $this->assertSame($account, $actualAccount); } + /** + * Test grant() when it fails loading an account. + */ public function testGrantFailingAccountLoading() { // Mocking AccountLoaderInterface @@ -97,6 +106,9 @@ public function testGrantFailingAccountLoading() $passwordGrantExtension->grant($application, $loginAttempt); } + /** + * Test grant() when it fails to validate the password. + */ public function testGrantFailingPasswordValidation() { // Mocking AccountInterface From d7cd2538566024faefcc9b02ff4f8e27e7d8c2d3 Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:21:11 +0100 Subject: [PATCH 4/9] fix from getting defined options to getting required options --- .../OAuth/Tests/GrantType/PasswordGrantExtensionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index b1d99e1..2c527a6 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -29,7 +29,7 @@ public function testConfigureRequestParameters() ); $passwordGrantExtension->configureRequestParameters($optionsResolver); - $actualDefinedOptions = $optionsResolver->getDefinedOptions(); + $actualDefinedOptions = $optionsResolver->getRequiredOptions(); $expectedDefinedOptions = ['password', 'username']; $this->assertEquals($expectedDefinedOptions, $actualDefinedOptions, '', $delta = 0.0, 10, true); // Not caring about keys } From bf13f0300c374627fabe2f442cf48c4074c9d089 Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:25:59 +0100 Subject: [PATCH 5/9] [typo] variable name --- .../OAuth/Tests/GrantType/PasswordGrantExtensionTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index 2c527a6..46cf1b1 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -29,9 +29,9 @@ public function testConfigureRequestParameters() ); $passwordGrantExtension->configureRequestParameters($optionsResolver); - $actualDefinedOptions = $optionsResolver->getRequiredOptions(); - $expectedDefinedOptions = ['password', 'username']; - $this->assertEquals($expectedDefinedOptions, $actualDefinedOptions, '', $delta = 0.0, 10, true); // Not caring about keys + $actualRequiredOptions = $optionsResolver->getRequiredOptions(); + $expectedRequiredOptions = ['password', 'username']; + $this->assertEquals($expectedRequiredOptions, $actualRequiredOptions, '', $delta = 0.0, 10, true); // Not caring about keys } /** From 17d73aadd60d7555438e66c28f1762ace80daec9 Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:28:10 +0100 Subject: [PATCH 6/9] [typo] indent --- .../GrantType/PasswordGrantExtensionTest.php | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index 46cf1b1..11b3ac6 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -45,13 +45,19 @@ public function testSuccessGrant() // Mocking AccountLoaderInterface $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); - $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn($account)->shouldBeCalled(); + $accountLoaderMock + ->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') + ->willReturn($account) + ->shouldBeCalled(); /** @var AccountLoaderInterface $accountLoader */ $accountLoader = $accountLoaderMock->reveal(); // Mocking UserPasswordEncoderInterface $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); - $userPasswordEncoderMock->isPasswordValid(Argument::type(UserInterface::class), 'password_test')->willReturn(true)->shouldBeCalled(); + $userPasswordEncoderMock + ->isPasswordValid(Argument::type(UserInterface::class), 'password_test') + ->willReturn(true) + ->shouldBeCalled(); /** @var UserPasswordEncoderInterface $userPasswordEncoder */ $userPasswordEncoder = $userPasswordEncoderMock->reveal(); @@ -64,8 +70,14 @@ public function testSuccessGrant() // Mocking LoginAttempt $loginAttemptMock = $this->prophesize(LoginAttempt::class); - $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); - $loginAttemptMock->getData('password')->willReturn('password_test')->shouldBeCalled(); + $loginAttemptMock + ->getData('username') + ->willReturn('username_test') + ->shouldBeCalled(); + $loginAttemptMock + ->getData('password') + ->willReturn('password_test') + ->shouldBeCalled(); /** @var LoginAttempt $loginAttempt */ $loginAttempt = $loginAttemptMock->reveal(); @@ -80,7 +92,10 @@ public function testGrantFailingAccountLoading() { // Mocking AccountLoaderInterface $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); - $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn(null)->shouldBeCalled(); + $accountLoaderMock + ->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') + ->willReturn(null) + ->shouldBeCalled(); /** @var AccountLoaderInterface $accountLoader */ $accountLoader = $accountLoaderMock->reveal(); @@ -98,7 +113,10 @@ public function testGrantFailingAccountLoading() // Mocking LoginAttempt $loginAttemptMock = $this->prophesize(LoginAttempt::class); - $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); + $loginAttemptMock + ->getData('username') + ->willReturn('username_test') + ->shouldBeCalled(); /** @var LoginAttempt $loginAttempt */ $loginAttempt = $loginAttemptMock->reveal(); @@ -117,13 +135,19 @@ public function testGrantFailingPasswordValidation() // Mocking AccountLoaderInterface $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); - $accountLoaderMock->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test')->willReturn($account)->shouldBeCalled(); + $accountLoaderMock + ->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') + ->willReturn($account) + ->shouldBeCalled(); /** @var AccountLoaderInterface $accountLoader */ $accountLoader = $accountLoaderMock->reveal(); // Mocking UserPasswordEncoderInterface $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); - $userPasswordEncoderMock->isPasswordValid(Argument::type(UserInterface::class), 'password_test')->willReturn(false)->shouldBeCalled(); + $userPasswordEncoderMock + ->isPasswordValid(Argument::type(UserInterface::class), 'password_test') + ->willReturn(false) + ->shouldBeCalled(); /** @var UserPasswordEncoderInterface $userPasswordEncoder */ $userPasswordEncoder = $userPasswordEncoderMock->reveal(); @@ -136,8 +160,14 @@ public function testGrantFailingPasswordValidation() // Mocking LoginAttempt $loginAttemptMock = $this->prophesize(LoginAttempt::class); - $loginAttemptMock->getData('username')->willReturn('username_test')->shouldBeCalled(); - $loginAttemptMock->getData('password')->willReturn('password_test')->shouldBeCalled(); + $loginAttemptMock + ->getData('username') + ->willReturn('username_test') + ->shouldBeCalled(); + $loginAttemptMock + ->getData('password') + ->willReturn('password_test') + ->shouldBeCalled(); /** @var LoginAttempt $loginAttempt */ $loginAttempt = $loginAttemptMock->reveal(); $this->expectException(InvalidGrantException::class); From 55a0842f04f042522f6c9bc618321b68cf68d17e Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:43:23 +0100 Subject: [PATCH 7/9] better test of resolver with the required/optional options --- .../OAuth/Tests/GrantType/PasswordGrantExtensionTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index 11b3ac6..8738d9b 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -29,9 +29,15 @@ public function testConfigureRequestParameters() ); $passwordGrantExtension->configureRequestParameters($optionsResolver); + // Testing the required options $actualRequiredOptions = $optionsResolver->getRequiredOptions(); $expectedRequiredOptions = ['password', 'username']; $this->assertEquals($expectedRequiredOptions, $actualRequiredOptions, '', $delta = 0.0, 10, true); // Not caring about keys + + // Testing the optional options + $actualOptionalOptions = array_diff($optionsResolver->getDefinedOptions(), $actualRequiredOptions); + $expectedOptionalOptions = []; + $this->assertCount(0, array_diff($expectedOptionalOptions, $actualOptionalOptions)); } /** From d23c85188cb78a6eed7fe0d17aaa5c1bc8c80349 Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:56:17 +0100 Subject: [PATCH 8/9] optimization of memory --- .../GrantType/PasswordGrantExtensionTest.php | 129 +++++++----------- 1 file changed, 50 insertions(+), 79 deletions(-) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index 8738d9b..1b052b2 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -46,48 +46,39 @@ public function testConfigureRequestParameters() public function testSuccessGrant() { // Mocking AccountInterface - /** @var AccountInterface $account */ $account = $this->prophesize(AccountInterface::class)->reveal(); - // Mocking AccountLoaderInterface - $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); - $accountLoaderMock - ->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') + // Mocking AccountLoader + $accountLoader = $this->prophesize(AccountLoaderInterface::class); + $accountLoader->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') ->willReturn($account) ->shouldBeCalled(); - /** @var AccountLoaderInterface $accountLoader */ - $accountLoader = $accountLoaderMock->reveal(); // Mocking UserPasswordEncoderInterface - $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); - $userPasswordEncoderMock - ->isPasswordValid(Argument::type(UserInterface::class), 'password_test') + $userPasswordEncoder = $this->prophesize(UserPasswordEncoderInterface::class); + $userPasswordEncoder->isPasswordValid(Argument::type(UserInterface::class), 'password_test') ->willReturn(true) ->shouldBeCalled(); - /** @var UserPasswordEncoderInterface $userPasswordEncoder */ - $userPasswordEncoder = $userPasswordEncoderMock->reveal(); - $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); - - // Mocking ApplicationInterface - $applicationMock = $this->prophesize(ApplicationInterface::class); - /** @var ApplicationInterface $application */ - $application = $applicationMock->reveal(); + $passwordGrantExtension = new PasswordGrantExtension( + $accountLoader->reveal(), + $userPasswordEncoder->reveal() + ); // Mocking LoginAttempt - $loginAttemptMock = $this->prophesize(LoginAttempt::class); - $loginAttemptMock - ->getData('username') + $loginAttempt = $this->prophesize(LoginAttempt::class); + $loginAttempt->getData('username') ->willReturn('username_test') ->shouldBeCalled(); - $loginAttemptMock - ->getData('password') + $loginAttempt->getData('password') ->willReturn('password_test') ->shouldBeCalled(); - /** @var LoginAttempt $loginAttempt */ - $loginAttempt = $loginAttemptMock->reveal(); - $actualAccount = $passwordGrantExtension->grant($application, $loginAttempt); + $actualAccount = $passwordGrantExtension->grant( + $this->prophesize(ApplicationInterface::class)->reveal(), + $loginAttempt->reveal() + ); + $this->assertSame($account, $actualAccount); } @@ -96,38 +87,29 @@ public function testSuccessGrant() */ public function testGrantFailingAccountLoading() { - // Mocking AccountLoaderInterface - $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); - $accountLoaderMock - ->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') + // Mocking AccountLoader + $accountLoader = $this->prophesize(AccountLoaderInterface::class); + $accountLoader->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') ->willReturn(null) ->shouldBeCalled(); - /** @var AccountLoaderInterface $accountLoader */ - $accountLoader = $accountLoaderMock->reveal(); - - // Mocking UserPasswordEncoderInterface - $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); - /** @var UserPasswordEncoderInterface $userPasswordEncoder */ - $userPasswordEncoder = $userPasswordEncoderMock->reveal(); - - $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); - // Mocking ApplicationInterface - $applicationMock = $this->prophesize(ApplicationInterface::class); - /** @var ApplicationInterface $application */ - $application = $applicationMock->reveal(); + $passwordGrantExtension = new PasswordGrantExtension( + $accountLoader->reveal(), + $this->prophesize(UserPasswordEncoderInterface::class)->reveal() + ); // Mocking LoginAttempt - $loginAttemptMock = $this->prophesize(LoginAttempt::class); - $loginAttemptMock - ->getData('username') + $loginAttempt = $this->prophesize(LoginAttempt::class); + $loginAttempt->getData('username') ->willReturn('username_test') ->shouldBeCalled(); - /** @var LoginAttempt $loginAttempt */ - $loginAttempt = $loginAttemptMock->reveal(); $this->expectException(InvalidGrantException::class); - $passwordGrantExtension->grant($application, $loginAttempt); + + $passwordGrantExtension->grant( + $this->prophesize(ApplicationInterface::class)->reveal(), + $loginAttempt->reveal() + ); } /** @@ -135,49 +117,38 @@ public function testGrantFailingAccountLoading() */ public function testGrantFailingPasswordValidation() { - // Mocking AccountInterface - /** @var AccountInterface $account */ - $account = $this->prophesize(AccountInterface::class)->reveal(); - - // Mocking AccountLoaderInterface - $accountLoaderMock = $this->prophesize(AccountLoaderInterface::class); - $accountLoaderMock - ->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') - ->willReturn($account) + // Mocking AccountLoader + $accountLoader = $this->prophesize(AccountLoaderInterface::class); + $accountLoader->retrieveOnApplicationByUsername(Argument::type(ApplicationInterface::class), 'username_test') + ->willReturn($this->prophesize(AccountInterface::class)->reveal()) ->shouldBeCalled(); - /** @var AccountLoaderInterface $accountLoader */ - $accountLoader = $accountLoaderMock->reveal(); // Mocking UserPasswordEncoderInterface - $userPasswordEncoderMock = $this->prophesize(UserPasswordEncoderInterface::class); - $userPasswordEncoderMock - ->isPasswordValid(Argument::type(UserInterface::class), 'password_test') + $userPasswordEncoder = $this->prophesize(UserPasswordEncoderInterface::class); + $userPasswordEncoder->isPasswordValid(Argument::type(UserInterface::class), 'password_test') ->willReturn(false) ->shouldBeCalled(); - /** @var UserPasswordEncoderInterface $userPasswordEncoder */ - $userPasswordEncoder = $userPasswordEncoderMock->reveal(); - - $passwordGrantExtension = new PasswordGrantExtension($accountLoader, $userPasswordEncoder); - // Mocking ApplicationInterface - $applicationMock = $this->prophesize(ApplicationInterface::class); - /** @var ApplicationInterface $application */ - $application = $applicationMock->reveal(); + $passwordGrantExtension = new PasswordGrantExtension( + $accountLoader->reveal(), + $userPasswordEncoder->reveal() + ); // Mocking LoginAttempt - $loginAttemptMock = $this->prophesize(LoginAttempt::class); - $loginAttemptMock - ->getData('username') + $loginAttempt = $this->prophesize(LoginAttempt::class); + $loginAttempt->getData('username') ->willReturn('username_test') ->shouldBeCalled(); - $loginAttemptMock - ->getData('password') + $loginAttempt->getData('password') ->willReturn('password_test') ->shouldBeCalled(); - /** @var LoginAttempt $loginAttempt */ - $loginAttempt = $loginAttemptMock->reveal(); + + $this->expectException(InvalidGrantException::class); - $passwordGrantExtension->grant($application, $loginAttempt); + $passwordGrantExtension->grant( + $this->prophesize(ApplicationInterface::class)->reveal(), + $loginAttempt->reveal() + ); } } From 534700b63015d890de6610b33f70fbc91484d7d9 Mon Sep 17 00:00:00 2001 From: Raphael De Freitas Date: Fri, 18 Mar 2016 11:57:44 +0100 Subject: [PATCH 9/9] test the exception messages --- .../OAuth/Tests/GrantType/PasswordGrantExtensionTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php index 1b052b2..0b9c5ae 100644 --- a/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php +++ b/src/Majora/Component/OAuth/Tests/GrantType/PasswordGrantExtensionTest.php @@ -105,6 +105,7 @@ public function testGrantFailingAccountLoading() ->shouldBeCalled(); $this->expectException(InvalidGrantException::class); + $this->expectExceptionMessage('Username not found on loaded application.'); $passwordGrantExtension->grant( $this->prophesize(ApplicationInterface::class)->reveal(), @@ -145,6 +146,7 @@ public function testGrantFailingPasswordValidation() $this->expectException(InvalidGrantException::class); + $this->expectExceptionMessage('Invalid password for loaded account.'); $passwordGrantExtension->grant( $this->prophesize(ApplicationInterface::class)->reveal(),