From ce2e68faf6cbcee37a2975b24d1b727d9731455b Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Tue, 10 May 2016 15:07:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D1=91?= =?UTF-8?q?=D0=BD=20Codeception=20=D0=B4=D0=BE=20=D0=B2=D0=B5=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D0=B8=202.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +-- composer.json | 2 +- tests/codeception/api/.gitignore | 3 +- .../api/_support/FunctionalTester.php | 38 +++++++++++++++++++ tests/codeception/api/_support/UnitTester.php | 26 +++++++++++++ tests/codeception/api/functional.suite.yml | 13 ++++--- .../functional/AccountsChangePasswordCest.php | 7 +--- .../functional/AccountsChangeUsernameCest.php | 10 ++--- .../api/functional/AccountsCurrentCest.php | 5 +-- .../api/functional/OauthAccessTokenCest.php | 7 +--- .../api/functional/OauthAuthCodeCest.php | 18 +++------ .../api/functional/OauthRefreshTokenCest.php | 10 ++--- .../api/functional/_steps/AccountSteps.php | 23 ----------- .../api/functional/_steps/OauthSteps.php | 2 +- .../unit/models/ChangePasswordFormTest.php | 3 +- tests/codeception/common/.gitignore | 3 +- .../common/_support/UnitTester.php | 25 ++++++++++++ tests/codeception/console/.gitignore | 2 +- .../console/_support/UnitTester.php | 26 +++++++++++++ 19 files changed, 149 insertions(+), 80 deletions(-) create mode 100644 tests/codeception/api/_support/FunctionalTester.php create mode 100644 tests/codeception/api/_support/UnitTester.php delete mode 100644 tests/codeception/api/functional/_steps/AccountSteps.php create mode 100644 tests/codeception/common/_support/UnitTester.php create mode 100644 tests/codeception/console/_support/UnitTester.php diff --git a/README.md b/README.md index 8c93eba..1f2257d 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,9 @@ docker-compose restart ```sh # Прежде чем тестировать, необходимо накатить миграции -docker exec -it db6366f120ee php tests/codeception/bin/yii migrate --interactive=0 +docker exec -it app php tests/codeception/bin/yii migrate --interactive=0 # Собрать все тестовые окружения -docker exec -it db6366f120ee /bin/sh -c 'cd tests; ./../vendor/bin/codecept build' +docker exec -it app ./vendor/bin/codecept build -c tests # И запустить собственно процесс тестирования -docker exec -it db6366f120ee /bin/sh -c 'cd tests; ./../vendor/bin/codecept run' +docker exec -it app ./vendor/bin/codecept run -c tests ``` diff --git a/composer.json b/composer.json index e3b2455..4abc32e 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "yiisoft/yii2-gii": "*", "yiisoft/yii2-faker": "*", "flow/jsonpath": "^0.3.1", - "codeception/codeception": "2.0.*", + "codeception/codeception": "2.1.*", "codeception/specify": "*", "codeception/verify": "*" }, diff --git a/tests/codeception/api/.gitignore b/tests/codeception/api/.gitignore index b5226cf..3217386 100644 --- a/tests/codeception/api/.gitignore +++ b/tests/codeception/api/.gitignore @@ -1,3 +1,2 @@ # these files are auto generated by codeception build -/unit/UnitTester.php -/functional/FunctionalTester.php +_support/_generated diff --git a/tests/codeception/api/_support/FunctionalTester.php b/tests/codeception/api/_support/FunctionalTester.php new file mode 100644 index 0000000..3ec280b --- /dev/null +++ b/tests/codeception/api/_support/FunctionalTester.php @@ -0,0 +1,38 @@ +login('Admin', 'password_0'); + $I->canSeeResponseIsJson(); + $I->canSeeResponseJsonMatchesJsonPath('$.jwt'); + $jwt = $I->grabDataFromResponseByJsonPath('$.jwt')[0]; + $I->amBearerAuthenticated($jwt); + } + + public function notLoggedIn() { + $this->haveHttpHeader('Authorization', null); + } + +} diff --git a/tests/codeception/api/_support/UnitTester.php b/tests/codeception/api/_support/UnitTester.php new file mode 100644 index 0000000..1919072 --- /dev/null +++ b/tests/codeception/api/_support/UnitTester.php @@ -0,0 +1,26 @@ +route = new AccountsRoute($I); } - public function _after(FunctionalTester $I) { + public function _after() { /** @var Account $account */ $account = Account::findOne(1); $account->setPassword('password_0'); $account->save(); } - public function testChangePassword(FunctionalTester $I, Scenario $scenario) { + public function testChangePassword(FunctionalTester $I) { $I->wantTo('change my password'); - $I = new AccountSteps($scenario); $I->loggedInAsActiveAccount(); $this->route->changePassword('password_0', 'new-password', 'new-password'); diff --git a/tests/codeception/api/functional/AccountsChangeUsernameCest.php b/tests/codeception/api/functional/AccountsChangeUsernameCest.php index 37a02d9..73c54e6 100644 --- a/tests/codeception/api/functional/AccountsChangeUsernameCest.php +++ b/tests/codeception/api/functional/AccountsChangeUsernameCest.php @@ -1,11 +1,9 @@ route = new AccountsRoute($I); } - public function _after(FunctionalTester $I) { + public function _after() { /** @var Account $account */ $account = Account::findOne(1); $account->username = 'Admin'; $account->save(); } - public function testChangeUsername(FunctionalTester $I, Scenario $scenario) { + public function testChangeUsername(FunctionalTester $I) { $I->wantTo('change my nickname'); - $I = new AccountSteps($scenario); $I->loggedInAsActiveAccount(); $this->route->changeUsername('password_0', 'bruce_wayne'); @@ -39,9 +36,8 @@ class AccountsChangeUsernameCest { ]); } - public function testChangeUsernameNotAvailable(FunctionalTester $I, Scenario $scenario) { + public function testChangeUsernameNotAvailable(FunctionalTester $I) { $I->wantTo('see, that nickname "in use" is not available'); - $I = new AccountSteps($scenario); $I->loggedInAsActiveAccount(); $this->route->changeUsername('password_0', 'Jon'); diff --git a/tests/codeception/api/functional/AccountsCurrentCest.php b/tests/codeception/api/functional/AccountsCurrentCest.php index 6812557..c49e4cb 100644 --- a/tests/codeception/api/functional/AccountsCurrentCest.php +++ b/tests/codeception/api/functional/AccountsCurrentCest.php @@ -1,10 +1,8 @@ route = new AccountsRoute($I); } - public function testCurrent(FunctionalTester $I, Scenario $scenario) { - $I = new AccountSteps($scenario); + public function testCurrent(FunctionalTester $I) { $I->loggedInAsActiveAccount(); $this->route->current(); diff --git a/tests/codeception/api/functional/OauthAccessTokenCest.php b/tests/codeception/api/functional/OauthAccessTokenCest.php index 3d92717..a2c1275 100644 --- a/tests/codeception/api/functional/OauthAccessTokenCest.php +++ b/tests/codeception/api/functional/OauthAccessTokenCest.php @@ -1,7 +1,6 @@ getAuthCode(); $this->route->issueToken($this->buildParams( $authCode, @@ -56,8 +54,7 @@ class OauthAccessTokenCest { $I->canSeeResponseJsonMatchesJsonPath('$.expires_in'); } - public function testIssueTokenWithRefreshToken(FunctionalTester $I, Scenario $scenario) { - $I = new OauthSteps($scenario); + public function testIssueTokenWithRefreshToken(OauthSteps $I) { $authCode = $I->getAuthCode(false); $this->route->issueToken($this->buildParams( $authCode, diff --git a/tests/codeception/api/functional/OauthAuthCodeCest.php b/tests/codeception/api/functional/OauthAuthCodeCest.php index 34d0001..84baf07 100644 --- a/tests/codeception/api/functional/OauthAuthCodeCest.php +++ b/tests/codeception/api/functional/OauthAuthCodeCest.php @@ -2,7 +2,6 @@ namespace tests\codeception\api; use tests\codeception\api\_pages\OauthRoute; -use tests\codeception\api\functional\_steps\AccountSteps; use Yii; class OauthAuthCodeCest { @@ -54,6 +53,7 @@ class OauthAuthCodeCest { } public function testValidateWithDescriptionReplaceRequest(FunctionalTester $I) { + $I->loggedInAsActiveAccount(); $I->wantTo('validate and get information with description replacement'); $this->route->validate($this->buildQueryParams( 'ely', @@ -74,15 +74,13 @@ class OauthAuthCodeCest { ]); } - public function testCompleteValidationAction($I, $scenario) { - $I = new AccountSteps($scenario); + public function testCompleteValidationAction(FunctionalTester $I) { $I->loggedInAsActiveAccount(); $I->wantTo('validate all oAuth params on complete request'); $this->testOauthParamsValidation($I, 'complete'); } - public function testCompleteActionOnWrongConditions($I, $scenario) { - $I = new AccountSteps($scenario); + public function testCompleteActionOnWrongConditions(FunctionalTester $I) { $I->loggedInAsActiveAccount(); $I->wantTo('get accept_required if I dom\'t require any scope, but this is first time request'); @@ -116,10 +114,8 @@ class OauthAuthCodeCest { ]); } - public function testCompleteActionSuccess($I, $scenario) { - $I = new AccountSteps($scenario); + public function testCompleteActionSuccess(FunctionalTester $I) { $I->loggedInAsActiveAccount(); - $I->wantTo('get auth code if I require some scope and pass accept field'); $this->route->complete($this->buildQueryParams( 'ely', @@ -161,8 +157,7 @@ class OauthAuthCodeCest { $I->canSeeResponseJsonMatchesJsonPath('$.redirectUri'); } - public function testAcceptRequiredOnNewScope($I, $scenario) { - $I = new AccountSteps($scenario); + public function testAcceptRequiredOnNewScope(FunctionalTester $I) { $I->loggedInAsActiveAccount(); $I->wantTo('get accept_required if I have previous successful request, but now require some new scope'); $this->route->complete($this->buildQueryParams( @@ -186,8 +181,7 @@ class OauthAuthCodeCest { ]); } - public function testCompleteActionWithDismissState($I, $scenario) { - $I = new AccountSteps($scenario); + public function testCompleteActionWithDismissState(FunctionalTester $I) { $I->loggedInAsActiveAccount(); $I->wantTo('get access_denied error if I pass accept in false state'); $this->route->complete($this->buildQueryParams( diff --git a/tests/codeception/api/functional/OauthRefreshTokenCest.php b/tests/codeception/api/functional/OauthRefreshTokenCest.php index 7068587..4c29bad 100644 --- a/tests/codeception/api/functional/OauthRefreshTokenCest.php +++ b/tests/codeception/api/functional/OauthRefreshTokenCest.php @@ -1,7 +1,6 @@ route = new OauthRoute($I); } - public function testRefreshToken(FunctionalTester $I, Scenario $scenario) { - $I = new OauthSteps($scenario); + public function testRefreshToken(OauthSteps $I) { $refreshToken = $I->getRefreshToken(); $this->route->issueToken($this->buildParams( $refreshToken, @@ -36,8 +34,7 @@ class OauthRefreshTokenCest { $I->canSeeResponseJsonMatchesJsonPath('$.expires_in'); } - public function testRefreshTokenWithSameScopes(FunctionalTester $I, Scenario $scenario) { - $I = new OauthSteps($scenario); + public function testRefreshTokenWithSameScopes(OauthSteps $I) { $refreshToken = $I->getRefreshToken(); $this->route->issueToken($this->buildParams( $refreshToken, @@ -55,8 +52,7 @@ class OauthRefreshTokenCest { $I->canSeeResponseJsonMatchesJsonPath('$.expires_in'); } - public function testRefreshTokenWithNewScopes(FunctionalTester $I, Scenario $scenario) { - $I = new OauthSteps($scenario); + public function testRefreshTokenWithNewScopes(OauthSteps $I) { $refreshToken = $I->getRefreshToken(); $this->route->issueToken($this->buildParams( $refreshToken, diff --git a/tests/codeception/api/functional/_steps/AccountSteps.php b/tests/codeception/api/functional/_steps/AccountSteps.php deleted file mode 100644 index ec25886..0000000 --- a/tests/codeception/api/functional/_steps/AccountSteps.php +++ /dev/null @@ -1,23 +0,0 @@ -login('Admin', 'password_0'); - $I->canSeeResponseIsJson(); - $I->canSeeResponseJsonMatchesJsonPath('$.jwt'); - $jwt = $I->grabDataFromResponseByJsonPath('$.jwt')[0]; - $I->amBearerAuthenticated($jwt); - } - - public function notLoggedIn() { - $this->haveHttpHeader('Authorization', null); - } - -} diff --git a/tests/codeception/api/functional/_steps/OauthSteps.php b/tests/codeception/api/functional/_steps/OauthSteps.php index d303c63..802c779 100644 --- a/tests/codeception/api/functional/_steps/OauthSteps.php +++ b/tests/codeception/api/functional/_steps/OauthSteps.php @@ -3,7 +3,7 @@ namespace tests\codeception\api\functional\_steps; use tests\codeception\api\_pages\OauthRoute; -class OauthSteps extends AccountSteps { +class OauthSteps extends \tests\codeception\api\FunctionalTester { public function getAuthCode($online = true) { // TODO: по идее можно напрямую сделать зпись в базу, что ускорит процесс тестирования diff --git a/tests/codeception/api/unit/models/ChangePasswordFormTest.php b/tests/codeception/api/unit/models/ChangePasswordFormTest.php index dedb750..af8f986 100644 --- a/tests/codeception/api/unit/models/ChangePasswordFormTest.php +++ b/tests/codeception/api/unit/models/ChangePasswordFormTest.php @@ -71,9 +71,10 @@ class ChangePasswordFormTest extends DbTestCase { 'newRePassword' => 'my-new-password', ]); $this->specify('successfully change password with modern hash strategy', function() use ($model, $account) { + $callTime = time(); expect('form should return true', $model->changePassword())->true(); expect('new password should be successfully stored into account', $account->validatePassword('my-new-password'))->true(); - expect('password change time updated', $account->password_changed_at)->greaterOrEquals(time() - 2); + expect('password change time updated', $account->password_changed_at)->greaterOrEquals($callTime); }); /** @var Account $account */ diff --git a/tests/codeception/common/.gitignore b/tests/codeception/common/.gitignore index b5226cf..3217386 100644 --- a/tests/codeception/common/.gitignore +++ b/tests/codeception/common/.gitignore @@ -1,3 +1,2 @@ # these files are auto generated by codeception build -/unit/UnitTester.php -/functional/FunctionalTester.php +_support/_generated diff --git a/tests/codeception/common/_support/UnitTester.php b/tests/codeception/common/_support/UnitTester.php new file mode 100644 index 0000000..14fd6ae --- /dev/null +++ b/tests/codeception/common/_support/UnitTester.php @@ -0,0 +1,25 @@ +