From 7d33b824d7b9ad441afb9932268dc64181074a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Wed, 16 Dec 2015 17:10:31 +0100 Subject: [PATCH] Some refactor to add ApiAccount version --- .../Phrasea/Functional/UserDeletionTest.php | 1 + .../ApiApplicationManipulatorTest.php | 19 +++-- .../ApiOauthCodeManipulatorTest.php | 70 ++++++++++++++---- .../ApiOauthTokenManipulatorTest.php | 71 +++++++++++++++---- 4 files changed, 128 insertions(+), 33 deletions(-) diff --git a/tests/Alchemy/Tests/Phrasea/Functional/UserDeletionTest.php b/tests/Alchemy/Tests/Phrasea/Functional/UserDeletionTest.php index 2b9be214d2..5485400d4b 100644 --- a/tests/Alchemy/Tests/Phrasea/Functional/UserDeletionTest.php +++ b/tests/Alchemy/Tests/Phrasea/Functional/UserDeletionTest.php @@ -8,6 +8,7 @@ * file that was distributed with this source code. */ namespace Alchemy\Tests\Phrasea\Functional; +use Alchemy\Phrasea\ControllerProvider\Api\V2; use Alchemy\Phrasea\Model\Entities\ApiAccount; use Alchemy\Phrasea\Model\Entities\ApiApplication; use Alchemy\Phrasea\Model\Entities\User; diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php index a2c1e538b5..a355a0f007 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiApplicationManipulatorTest.php @@ -2,7 +2,9 @@ namespace Alchemy\Tests\Phrasea\Model\Manipulator; +use Alchemy\Phrasea\ControllerProvider\Api\V2; use Alchemy\Phrasea\Exception\InvalidArgumentException; +use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator; use Alchemy\Phrasea\Model\Manipulator\ApiApplicationManipulator; use Alchemy\Phrasea\Model\Entities\ApiApplication; @@ -57,7 +59,8 @@ class ApiApplicationManipulatorTest extends \PhraseanetTestCase public function testDelete() { - $manipulator = new ApiApplicationManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-applications'], self::$DI['app']['random.medium']); + $app = $this->getApplication(); + $manipulator = new ApiApplicationManipulator($app['orm.em'], $app['repo.api-applications'], $app['random.medium']); $application = $manipulator->create( 'desktop-app2', ApiApplication::DESKTOP_TYPE, @@ -65,15 +68,17 @@ class ApiApplicationManipulatorTest extends \PhraseanetTestCase 'http://desktop-app2-url.net' ); $applicationSave = clone $application; - $countBefore = count(self::$DI['app']['repo.api-applications']->findAll()); - $account = self::$DI['app']['manipulator.api-account']->create($application, self::$DI['user']); + $countBefore = count($app['repo.api-applications']->findAll()); + /** @var ApiAccountManipulator $apiAccountManipulator */ + $apiAccountManipulator = $app['manipulator.api-account']; + $account = $apiAccountManipulator->create($application, self::$DI['user'], V2::VERSION); $accountMem = clone $account; - self::$DI['app']['manipulator.api-oauth-token']->create($account); + $app['manipulator.api-oauth-token']->create($account); $manipulator->delete($application); - $this->assertGreaterThan(count(self::$DI['app']['repo.api-applications']->findAll()), $countBefore); - $accounts = self::$DI['app']['repo.api-accounts']->findByUserAndApplication(self::$DI['user'], $applicationSave); + $this->assertGreaterThan(count($app['repo.api-applications']->findAll()), $countBefore); + $accounts = $app['repo.api-accounts']->findByUserAndApplication(self::$DI['user'], $applicationSave); $this->assertEquals(0, count($accounts)); - $tokens = self::$DI['app']['repo.api-oauth-tokens']->findOauthTokens($accountMem); + $tokens = $app['repo.api-oauth-tokens']->findOauthTokens($accountMem); $this->assertEquals(0, count($tokens)); } diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php index ab6b610f7e..7f23255668 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthCodeManipulatorTest.php @@ -2,8 +2,13 @@ namespace Alchemy\Tests\Phrasea\Model\Manipulator; +use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\ControllerProvider\Api\V2; use Alchemy\Phrasea\Exception\InvalidArgumentException; +use Alchemy\Phrasea\Model\Entities\ApiAccount; +use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator; use Alchemy\Phrasea\Model\Manipulator\ApiOauthCodeManipulator; +use Alchemy\Phrasea\Model\Repositories\ApiOauthCodeRepository; /** * @group functional @@ -13,32 +18,40 @@ class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase { public function testCreate() { - $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); - $nbCodes = count(self::$DI['app']['repo.api-oauth-codes']->findAll()); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $app = $this->getApplication(); + $oauthCodesRepository = $this->getOAuthCodesRepository($app); + + $manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']); + $nbCodes = count($oauthCodesRepository->findAll()); + $account = $this->getApiAccount($app); $manipulator->create($account, 'http://www.redirect.url', time() + 30); - $this->assertGreaterThan($nbCodes, count(self::$DI['app']['repo.api-oauth-codes']->findAll())); + $this->assertGreaterThan($nbCodes, count($oauthCodesRepository->findAll())); } public function testDelete() { - $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $app = $this->getApplication(); + $oauthCodesRepository = $this->getOAuthCodesRepository($app); + + $manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']); + $account = $this->getApiAccount($app); $code = $manipulator->create($account, 'http://www.redirect.url', time() + 30); - $countBefore = count(self::$DI['app']['repo.api-oauth-codes']->findAll()); + $countBefore = count($oauthCodesRepository->findAll()); $manipulator->delete($code); - $this->assertGreaterThan(count(self::$DI['app']['repo.api-oauth-codes']->findAll()), $countBefore); + $this->assertGreaterThan(count($oauthCodesRepository->findAll()), $countBefore); } public function testUpdate() { + $app = $this->getApplication(); + $oauthCodesRepository = $this->getOAuthCodesRepository($app); - $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']); + $account = $this->getApiAccount($app); $code = $manipulator->create($account, 'http://www.redirect.url', $t = time() + 30); $code->setExpires(time() + 40); $manipulator->update($code); - $code = self::$DI['app']['repo.api-oauth-codes']->find($code->getCode()); + $code = $oauthCodesRepository->find($code->getCode()); $this->assertGreaterThan($t, $code->getExpires()); } @@ -47,8 +60,11 @@ class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase */ public function testSetRedirectUriBadArgumentException() { - $manipulator = new ApiOauthCodeManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-codes'], self::$DI['app']['random.medium']); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $app = $this->getApplication(); + $oauthCodesRepository = $this->getOAuthCodesRepository($app); + + $manipulator = new ApiOauthCodeManipulator($app['orm.em'], $oauthCodesRepository, $app['random.medium']); + $account = $this->getApiAccount($app); $code = $manipulator->create($account, 'http://www.redirect.url', time() + 30); try { $manipulator->setRedirectUri($code, 'bad-url'); @@ -57,4 +73,32 @@ class ApiOauthCodeManipulatorTest extends \PhraseanetTestCase } } + + /** + * @param Application $app + * @return ApiOauthCodeRepository + */ + private function getOAuthCodesRepository(Application $app) + { + return $app['repo.api-oauth-codes']; + } + + /** + * @param Application $app + * @return ApiAccountManipulator + */ + private function getApiAccountManipulator(Application $app) + { + return $app['manipulator.api-account']; + } + + /** + * @param Application $app + * @return ApiAccount + */ + private function getApiAccount(Application $app) + { + return $this->getApiAccountManipulator($app) + ->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION); + } } diff --git a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php index 116e7dd377..3b530bf48c 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Manipulator/ApiOauthTokenManipulatorTest.php @@ -2,7 +2,12 @@ namespace Alchemy\Tests\Phrasea\Model\Manipulator; +use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\ControllerProvider\Api\V2; +use Alchemy\Phrasea\Model\Entities\ApiAccount; +use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator; use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator; +use Alchemy\Phrasea\Model\Repositories\ApiOauthTokenRepository; /** * @group functional @@ -12,42 +17,82 @@ class ApiOauthTokenManipulatorTest extends \PhraseanetTestCase { public function testCreate() { - $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); - $nbTokens = count(self::$DI['app']['repo.api-oauth-tokens']->findAll()); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $app = $this->getApplication(); + $tokenRepository = $this->getTokenRepository($app); + + $manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']); + $nbTokens = count($tokenRepository->findAll()); + $account = $this->createAccount($app); $manipulator->create($account); - $this->assertGreaterThan($nbTokens, count(self::$DI['app']['repo.api-oauth-tokens']->findAll())); + $this->assertGreaterThan($nbTokens, count($tokenRepository->findAll())); } public function testDelete() { - $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $app = $this->getApplication(); + $tokenRepository = $this->getTokenRepository($app); + + $manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']); + $account = $this->createAccount($app); $token = $manipulator->create($account); - $countBefore = count(self::$DI['app']['repo.api-oauth-tokens']->findAll()); + $countBefore = count($tokenRepository->findAll()); $manipulator->delete($token); - $this->assertGreaterThan(count(self::$DI['app']['repo.api-oauth-tokens']->findAll()), $countBefore); + $this->assertGreaterThan(count($tokenRepository->findAll()), $countBefore); } public function testUpdate() { + $app = $this->getApplication(); + $tokenRepository = $this->getTokenRepository($app); - $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']); + $account = $this->createAccount($app); $token = $manipulator->create($account); $token->setSessionId(123456); $manipulator->update($token); - $token = self::$DI['app']['repo.api-oauth-tokens']->find($token->getOauthToken()); + $token = $tokenRepository->find($token->getOauthToken()); $this->assertEquals(123456, $token->getSessionId()); } public function testRenew() { - $manipulator = new ApiOauthTokenManipulator(self::$DI['app']['orm.em'], self::$DI['app']['repo.api-oauth-tokens'], self::$DI['app']['random.medium']); - $account = self::$DI['app']['manipulator.api-account']->create(self::$DI['oauth2-app-user'], self::$DI['user']); + $app = $this->getApplication(); + $tokenRepository = $this->getTokenRepository($app); + + $manipulator = new ApiOauthTokenManipulator($app['orm.em'], $tokenRepository, $app['random.medium']); + $account = $this->createAccount($app); $token = $manipulator->create($account); $oauthTokenBefore = $token->getOauthToken(); $manipulator->renew($token); $this->assertNotEquals($oauthTokenBefore, $token->getOauthToken()); } + + /** + * @param Application $app + * @return ApiAccountManipulator + */ + private function getApiAccountManipulator(Application $app) + { + return $app['manipulator.api-account']; + } + + /** + * @param Application $app + * @return ApiOauthTokenRepository + */ + private function getTokenRepository(Application $app) + { + $tokenRepository = $app['repo.api-oauth-tokens']; + return $tokenRepository; + } + + /** + * @param Application $app + * @return ApiAccount + */ + private function createAccount(Application $app) + { + return $this->getApiAccountManipulator($app) + ->create(self::$DI['oauth2-app-user'], self::$DI['user'], V2::VERSION); + } }