diff --git a/.gitattributes b/.gitattributes index 9cbc09b..6cdec7d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,3 +4,5 @@ /Robofile.php export-ignore /*.md export-ignore /*.yml export-ignore +/tests export-ignore +/.github export-ignore diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d7ef845..d9c509e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,21 +27,7 @@ jobs: - name: Install dependencies run: composer install --prefer-dist --no-progress --no-interaction --no-suggest - - name: Checkout Yii2 - uses: actions/checkout@v2 - with: - repository: Codeception/yii2-tests - path: framework-tests - - - name: Install Yii2 - run: | - composer require --no-update codeception/module-asserts - composer require --no-update codeception/module-filesystem - composer require --no-update codeception/codeception - composer update --no-dev --prefer-dist --no-interaction - working-directory: framework-tests - - name: Run test suite run: | - php vendor/bin/codecept build -c framework-tests - php vendor/bin/codecept run -c framework-tests + php vendor/bin/codecept build + php vendor/bin/codecept run diff --git a/.gitignore b/.gitignore index 5501822..241050f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /.idea/ /vendor/ /composer.lock -/framework-tests \ No newline at end of file +tests/_support +tests/_output +tests/cases/yii2-app-advanced/_data/db.sqlite diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..9143f3d --- /dev/null +++ b/codeception.yml @@ -0,0 +1,14 @@ +paths: + tests: tests + output: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +modules: + - Asserts +bootstrap: bootstrap.php +settings: + colors: true + memory_limit: 1024M +include: + - tests/cases/* diff --git a/composer.json b/composer.json index 3fb5f18..5e6d9d2 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,30 @@ "codeception/lib-innerbrowser": "^1.0", "codeception/codeception": "^4.0" }, + "require-dev": { + "yiisoft/yii2": "dev-master", + "yiisoft/yii2-app-advanced": "dev-master", + "codeception/verify": "<2", + "codemix/yii2-localeurls": "^1.7", + "codeception/module-asserts": "^1.3", + "codeception/module-filesystem": "^1.0" + }, "autoload":{ "classmap": ["src/"] }, + "autoload-dev": { + "classmap": [ + "vendor/yiisoft/yii2/Yii.php", + "tests/cases" + ] + }, "config": { "classmap-authoritative": true - } + }, + "repositories": [ + { + "type": "composer", + "url": "https://asset-packagist.org" + } + ] } diff --git a/tests/_data/.gitkeep b/tests/_data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..71170fb --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,10 @@ + 'Simple', + 'basePath' => __DIR__, + + 'components' => [ + 'db' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . \tests\helpers\SqlliteHelper::getTmpFile(), + ], + ], +]; \ No newline at end of file diff --git a/tests/cases/closeConnections/fixtures/EmptyFixture.php b/tests/cases/closeConnections/fixtures/EmptyFixture.php new file mode 100644 index 0000000..c8aeae6 --- /dev/null +++ b/tests/cases/closeConnections/fixtures/EmptyFixture.php @@ -0,0 +1,16 @@ + EmptyFixture::class, + ], + ]; + } + + protected function numberProvider() + { + return array_pad([], 5, ['count' => 0]); + } + + /** + * @param FunctionalTester $I + * @dataProvider numberProvider + */ + public function NoConnections(FunctionalTester $I, Example $example) + { + $I->assertEquals(SqlliteHelper::connectionCount(), $example['count']); + } + +} \ No newline at end of file diff --git a/tests/cases/closeConnections/functional/FixturesInBeforeCest.php b/tests/cases/closeConnections/functional/FixturesInBeforeCest.php new file mode 100644 index 0000000..21de51d --- /dev/null +++ b/tests/cases/closeConnections/functional/FixturesInBeforeCest.php @@ -0,0 +1,35 @@ +haveFixtures([ + [ + 'class' => EmptyFixture::class, + ], + ]); + } + + protected function numberProvider() + { + return array_pad([], 5, ['count' => 1]); + } + + /** + * @param FunctionalTester $I + * @dataProvider numberProvider + */ + public function NoConnections(FunctionalTester $I, Example $example) + { + $I->assertEquals(SqlliteHelper::connectionCount(), $example['count']); + } + +} \ No newline at end of file diff --git a/tests/cases/closeConnections/functional/NoFixturesCest.php b/tests/cases/closeConnections/functional/NoFixturesCest.php new file mode 100644 index 0000000..245be50 --- /dev/null +++ b/tests/cases/closeConnections/functional/NoFixturesCest.php @@ -0,0 +1,25 @@ + 0]); + } + + /** + * @param FunctionalTester $I + * @dataProvider numberProvider + */ + public function NoConnections(FunctionalTester $I, Example $example) + { + $I->assertEquals(SqlliteHelper::connectionCount(), $example['count']); + } +} \ No newline at end of file diff --git a/tests/cases/closeConnections/helpers/SqlliteHelper.php b/tests/cases/closeConnections/helpers/SqlliteHelper.php new file mode 100644 index 0000000..cf8c460 --- /dev/null +++ b/tests/cases/closeConnections/helpers/SqlliteHelper.php @@ -0,0 +1,32 @@ + 'Simple', + 'basePath' => __DIR__, + + 'components' => [ + 'db' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . \tests\helpers\SqlliteHelper::getTmpFile(), + ], + ], +]; \ No newline at end of file diff --git a/tests/cases/closeConnectionsNoCleanup/functional.suite.yml b/tests/cases/closeConnectionsNoCleanup/functional.suite.yml new file mode 100644 index 0000000..b189d50 --- /dev/null +++ b/tests/cases/closeConnectionsNoCleanup/functional.suite.yml @@ -0,0 +1,5 @@ +actor: FunctionalTester +modules: + enabled: + - Yii2 + - Asserts \ No newline at end of file diff --git a/tests/cases/closeConnectionsNoCleanup/functional/FixturesCest.php b/tests/cases/closeConnectionsNoCleanup/functional/FixturesCest.php new file mode 100644 index 0000000..025a423 --- /dev/null +++ b/tests/cases/closeConnectionsNoCleanup/functional/FixturesCest.php @@ -0,0 +1,38 @@ + EmptyFixture::class, + ], + ]; + } + + public function NoConnections1(FunctionalTester $I) + { + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(0, $count); + } + + public function NoConnections2(FunctionalTester $I) + { + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(0, $count); + } + + public function NoConnections3(FunctionalTester $I) + { + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(0, $count); + } + +} \ No newline at end of file diff --git a/tests/cases/closeConnectionsNoCleanup/functional/FixturesInBeforeCest.php b/tests/cases/closeConnectionsNoCleanup/functional/FixturesInBeforeCest.php new file mode 100644 index 0000000..acf6031 --- /dev/null +++ b/tests/cases/closeConnectionsNoCleanup/functional/FixturesInBeforeCest.php @@ -0,0 +1,37 @@ +haveFixtures([ + [ + 'class' => EmptyFixture::class, + ], + ]); + } + + public function OnlyOneConnection1(FunctionalTester $I) + { + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(1, $count); + } + + public function OnlyOneConnection2(FunctionalTester $I) + { + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(1, $count); + } + + public function OnlyOneConnection3(FunctionalTester $I) + { + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(1, $count); + } +} \ No newline at end of file diff --git a/tests/cases/closeConnectionsNoCleanup/functional/ThirdCest.php b/tests/cases/closeConnectionsNoCleanup/functional/ThirdCest.php new file mode 100644 index 0000000..0cbc42e --- /dev/null +++ b/tests/cases/closeConnectionsNoCleanup/functional/ThirdCest.php @@ -0,0 +1,40 @@ +assertEquals(0, $count); + } + + public function OnlyOneConnection2(FunctionalTester $I) + { + $I->haveFixtures([ + [ + 'class' => EmptyFixture::class, + ], + ]); + + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(1, $count); + } + + public function OnlyOneConnection3(FunctionalTester $I) + { + $I->haveFixtures([ + [ + 'class' => EmptyFixture::class, + ], + ]); + + $count = SqlliteHelper::connectionCount(); + $I->assertEquals(1, $count); + } +} \ No newline at end of file diff --git a/tests/cases/events/codeception.yml b/tests/cases/events/codeception.yml new file mode 100644 index 0000000..caf01d1 --- /dev/null +++ b/tests/cases/events/codeception.yml @@ -0,0 +1,15 @@ +paths: + tests: . + log: ../../_output + data: _data + support: ../../_support +namespace: tests\ +extensions: + enabled: + - Codeception\Extension\RunFailed +modules: + config: + Yii2: + configFile: config.php + transaction: true + cleanup: true \ No newline at end of file diff --git a/tests/cases/events/config.php b/tests/cases/events/config.php new file mode 100644 index 0000000..09869fd --- /dev/null +++ b/tests/cases/events/config.php @@ -0,0 +1,22 @@ + 'Response Events', + 'basePath' => __DIR__, + 'bootstrap' => [ + function(\yii\web\Application $application) { + $application->response->on(\yii\web\Response::EVENT_BEFORE_SEND, function($event) use ($application) { + $application->trigger('responseBeforeSendBootstrap'); + }); + } + ], + 'components' => [ + 'request' => [ + 'cookieValidationKey' => 'secret' + ], + 'response' => [ + 'on beforeSend' => function() { + \Yii::$app->trigger('responseBeforeSendConfig'); + } + ] + ] +]; \ No newline at end of file diff --git a/tests/cases/events/controllers/SiteController.php b/tests/cases/events/controllers/SiteController.php new file mode 100644 index 0000000..abcc66a --- /dev/null +++ b/tests/cases/events/controllers/SiteController.php @@ -0,0 +1,13 @@ +on('responseBeforeSendConfig', function(Event $event) use (&$sources) { + $sources[] = 'config'; + }); + \Yii::$app->on('responseBeforeSendBootstrap', function(Event $event) use (&$sources) { + $sources[] = 'bootstrap'; + }); + $I->assertEmpty($sources); + $I->amOnPage('/'); + $I->assertEquals(['config', 'bootstrap'], $sources); + + $sources = []; + $I->amOnPage('/'); + $I->assertEquals(['config', 'bootstrap'], $sources); + + } + + public function testAfterSendWithRecreate(FunctionalTester $I, \Codeception\Module\Yii2 $module) + { + $module->_reconfigure([ + 'responseCleanMethod' => Yii2::CLEAN_RECREATE + ]); + $module->client->startApp(); + $sources = []; + \Yii::$app->on('responseBeforeSendConfig', function(Event $event) use (&$sources) { + $sources[] = 'config'; + }); + \Yii::$app->on('responseBeforeSendBootstrap', function(Event $event) use (&$sources) { + $sources[] = 'bootstrap'; + }); + $I->assertEmpty($sources); + $I->amOnPage('/'); + $I->assertEquals(['config', 'bootstrap'], $sources); + + $sources = []; + $I->amOnPage('/'); + + // The module should fall back to the CLEAN_CLEAR method and keep event handlers intact. + $I->assertEquals(['config', 'bootstrap'], $sources); + + } + + public function testAfterSendWithForcedRecreate(FunctionalTester $I, \Codeception\Module\Yii2 $module) + { + $module->_reconfigure([ + 'responseCleanMethod' => Yii2::CLEAN_FORCE_RECREATE + ]); + $module->client->startApp(); + $sources = []; + \Yii::$app->on('responseBeforeSendConfig', function(Event $event) use (&$sources) { + $sources[] = 'config'; + }); + \Yii::$app->on('responseBeforeSendBootstrap', function(Event $event) use (&$sources) { + $sources[] = 'bootstrap'; + }); + + $I->assertEmpty($sources); + $I->amOnPage('/'); + + // We recreated the response component, since it has an event handler in its config + // that event handler will still work. + $I->assertEquals(['config'], $sources); + + $sources = []; + $I->amOnPage('/'); + + // We recreated the response component, since it has an event handler in its config + // that event handler will still work. + $I->assertEquals(['config'], $sources); + + } +} \ No newline at end of file diff --git a/tests/cases/locale-urls/codeception.yml b/tests/cases/locale-urls/codeception.yml new file mode 100644 index 0000000..caf01d1 --- /dev/null +++ b/tests/cases/locale-urls/codeception.yml @@ -0,0 +1,15 @@ +paths: + tests: . + log: ../../_output + data: _data + support: ../../_support +namespace: tests\ +extensions: + enabled: + - Codeception\Extension\RunFailed +modules: + config: + Yii2: + configFile: config.php + transaction: true + cleanup: true \ No newline at end of file diff --git a/tests/cases/locale-urls/config.php b/tests/cases/locale-urls/config.php new file mode 100644 index 0000000..101c377 --- /dev/null +++ b/tests/cases/locale-urls/config.php @@ -0,0 +1,31 @@ + 'Simple', + 'basePath' => __DIR__, + 'controllerNamespace' => 'app\localeurls\controllers', + 'components' => [ + 'request' => [ + 'enableCsrfValidation' => false, + 'cookieValidationKey' => 'test' + ], + 'urlManager' => [ + 'class' => UrlManager::class, + 'enablePrettyUrl' => true, + 'showScriptName' => false, + 'enableLanguagePersistence' => false, + 'enableLocaleUrls' => true, + 'languages' => ['nl', 'en'] + ] + ], + 'on beforeRequest' => function(\yii\base\Event $event) { + $app = $event->sender; + if ($app->has('urlManager', true)) { + $config = $app->getComponents()['urlManager']; + \Codeception\Util\Debug::debug('Resetting url manager: ' . print_r($config, true)); + $app->set('urlManager', $config); + } + } +]; \ No newline at end of file diff --git a/tests/cases/locale-urls/controllers/SiteController.php b/tests/cases/locale-urls/controllers/SiteController.php new file mode 100644 index 0000000..d68f82a --- /dev/null +++ b/tests/cases/locale-urls/controllers/SiteController.php @@ -0,0 +1,33 @@ + + +
+ +Submit +
+ + + + +HTML; + + } + + public function actionPost() + { + \Yii::$app->response->statusCode = 201; + return print_r(\Yii::$app->request->bodyParams, true); + } +} \ No newline at end of file diff --git a/tests/cases/locale-urls/functional.suite.yml b/tests/cases/locale-urls/functional.suite.yml new file mode 100644 index 0000000..754807a --- /dev/null +++ b/tests/cases/locale-urls/functional.suite.yml @@ -0,0 +1,13 @@ +# Codeception Test Suite Configuration +# +# Suite for functional tests +# Emulate web requests and make application process them +# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it +# Remove this suite if you don't use frameworks +actor: FunctionalTester +modules: + enabled: + - Yii2 + - Asserts +steps: + - Codeception\Step\ConditionalAssertion diff --git a/tests/cases/locale-urls/functional/LocaleUrlCest.php b/tests/cases/locale-urls/functional/LocaleUrlCest.php new file mode 100644 index 0000000..a945cb4 --- /dev/null +++ b/tests/cases/locale-urls/functional/LocaleUrlCest.php @@ -0,0 +1,40 @@ +assertInstanceOf(Application::class, \Yii::$app); + } + + public function testMultipleGet(FunctionalTester $I) + { + $I->amOnPage('/en/site/form'); + $I->amOnPage('/en/site/form'); + } + public function testFormSubmit(FunctionalTester $I) + { + $I->amOnPage(['site/form']); + $I->seeResponseCodeIs(200); + + $I->fillField('#test', 'test'); + $I->click('#submit'); + $I->canSeeResponseCodeIs(201); + } + + public function testFormSubmit2(FunctionalTester $I) + { + $I->amOnPage('/en/site/form'); + $I->seeResponseCodeIs(200); + $I->submitForm('form', [ + 'login-form[login]' => 'user', + 'login-form[password]' => 'test', + ]); + $I->canSeeResponseCodeIs(201); + } + +} \ No newline at end of file diff --git a/tests/cases/mock-mailer/codeception.yml b/tests/cases/mock-mailer/codeception.yml new file mode 100644 index 0000000..b0d1647 --- /dev/null +++ b/tests/cases/mock-mailer/codeception.yml @@ -0,0 +1,14 @@ +paths: + tests: . + log: ../../_output + data: _data + support: ../../_support +namespace: tests\ +extensions: + enabled: + - Codeception\Extension\RunFailed +modules: + config: + Yii2: + configFile: config.php + recreateApplication: true diff --git a/tests/cases/mock-mailer/config.php b/tests/cases/mock-mailer/config.php new file mode 100644 index 0000000..e6bd0ae --- /dev/null +++ b/tests/cases/mock-mailer/config.php @@ -0,0 +1,15 @@ + 'Simple', + 'basePath' => __DIR__, + 'controllerNamespace' => 'app\mockmailer\controllers', + 'components' => [ + 'request' => [ + 'enableCsrfValidation' => false, + 'cookieValidationKey' => 'test' + ], + 'mailer' => [ + 'class' => 'yii\swiftmailer\Mailer', + ], + ], +]; diff --git a/tests/cases/mock-mailer/controllers/SiteController.php b/tests/cases/mock-mailer/controllers/SiteController.php new file mode 100644 index 0000000..644557c --- /dev/null +++ b/tests/cases/mock-mailer/controllers/SiteController.php @@ -0,0 +1,39 @@ +doSendMail(); + + return __METHOD__; + } + + public function actionSendMailWithRedirect() + { + $this->doSendMail(); + + return $this->redirect(['site/index']); + } + + private function doSendMail() + { + return \Yii::$app->mailer->compose() + ->setFrom('from@domain.com') + ->setTo('to@domain.com') + ->setSubject('Message subject') + ->setTextBody('Plain text content') + ->setHtmlBody('HTML content') + ->send() + ; + } +} diff --git a/tests/cases/mock-mailer/functional.suite.yml b/tests/cases/mock-mailer/functional.suite.yml new file mode 100644 index 0000000..d2d1a2f --- /dev/null +++ b/tests/cases/mock-mailer/functional.suite.yml @@ -0,0 +1,11 @@ +# Codeception Test Suite Configuration +# +# Suite for functional tests +# Emulate web requests and make application process them +# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it +# Remove this suite if you don't use frameworks +actor: FunctionalTester +modules: + enabled: + - Yii2 + - Asserts \ No newline at end of file diff --git a/tests/cases/mock-mailer/functional/MockMailerCest.php b/tests/cases/mock-mailer/functional/MockMailerCest.php new file mode 100644 index 0000000..78ff126 --- /dev/null +++ b/tests/cases/mock-mailer/functional/MockMailerCest.php @@ -0,0 +1,26 @@ +assertInstanceOf(Application::class, \Yii::$app); + } + + public function testCountMailSentWithoutRedirect(FunctionalTester $I) + { + $I->amOnPage(['site/send-mail-without-redirect']); + + $I->seeEmailIsSent(1); + } + + public function testCountMailSentWithRedirect(FunctionalTester $I) + { + $I->amOnPage(['site/send-mail-with-redirect']); + + $I->seeEmailIsSent(1); + } +} diff --git a/tests/cases/pageCacheHeaderAlreadySent/codeception.yml b/tests/cases/pageCacheHeaderAlreadySent/codeception.yml new file mode 100644 index 0000000..01393b5 --- /dev/null +++ b/tests/cases/pageCacheHeaderAlreadySent/codeception.yml @@ -0,0 +1,13 @@ +paths: + tests: . + log: ../../_output + data: _data + support: ../../_support +namespace: tests\ +extensions: + enabled: + - Codeception\Extension\RunFailed +modules: + config: + Yii2: + configFile: config.php \ No newline at end of file diff --git a/tests/cases/pageCacheHeaderAlreadySent/config.php b/tests/cases/pageCacheHeaderAlreadySent/config.php new file mode 100644 index 0000000..b65fa02 --- /dev/null +++ b/tests/cases/pageCacheHeaderAlreadySent/config.php @@ -0,0 +1,12 @@ + 'PageCache', + 'basePath' => __DIR__, + 'controllerNamespace' => 'app\pageCacheHeaderAlreadySent\controllers', + 'components' => [ + 'cache' => [ + 'class' => \yii\caching\DummyCache::class, + ], + ], +]; diff --git a/tests/cases/pageCacheHeaderAlreadySent/controllers/UserController.php b/tests/cases/pageCacheHeaderAlreadySent/controllers/UserController.php new file mode 100644 index 0000000..ef8b022 --- /dev/null +++ b/tests/cases/pageCacheHeaderAlreadySent/controllers/UserController.php @@ -0,0 +1,22 @@ + [ + 'class' => PageCache::class + ] + ]; + } + + public function actionIndex() + { + return 'test'; + } +} diff --git a/tests/cases/pageCacheHeaderAlreadySent/functional.suite.yml b/tests/cases/pageCacheHeaderAlreadySent/functional.suite.yml new file mode 100644 index 0000000..42a4352 --- /dev/null +++ b/tests/cases/pageCacheHeaderAlreadySent/functional.suite.yml @@ -0,0 +1,12 @@ +# Codeception Test Suite Configuration +# +# Suite for api tests +# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it +# Remove this suite if you don't use frameworks +actor: FunctionalTester +modules: + enabled: + - Yii2 + - Asserts +steps: + - Codeception\Step\ConditionalAssertion diff --git a/tests/cases/pageCacheHeaderAlreadySent/functional/PageCest.php b/tests/cases/pageCacheHeaderAlreadySent/functional/PageCest.php new file mode 100644 index 0000000..6d9c545 --- /dev/null +++ b/tests/cases/pageCacheHeaderAlreadySent/functional/PageCest.php @@ -0,0 +1,13 @@ +amOnPage(['user/index']); + $I->canSeeResponseCodeIs(200); + + $I->amOnPage(['user/index']); + $I->canSeeResponseCodeIs(200); + } +} \ No newline at end of file diff --git a/tests/cases/simple/codeception.yml b/tests/cases/simple/codeception.yml new file mode 100644 index 0000000..cb08201 --- /dev/null +++ b/tests/cases/simple/codeception.yml @@ -0,0 +1,18 @@ +paths: + tests: . + log: ../../_output + data: _data + support: ../../_support +namespace: tests\ +extensions: + enabled: + - Codeception\Extension\RunFailed +settings: + shuffle: true + coolstuff: false +modules: + config: + Yii2: + configFile: config.php + transaction: true + cleanup: true \ No newline at end of file diff --git a/tests/cases/simple/config.php b/tests/cases/simple/config.php new file mode 100644 index 0000000..2e11ee3 --- /dev/null +++ b/tests/cases/simple/config.php @@ -0,0 +1,23 @@ + 'Simple', + 'basePath' => __DIR__, + 'controllerNamespace' => 'app\simple\controllers', + 'components' => [ + 'request' => [ + 'enableCsrfValidation' => false, + 'cookieValidationKey' => 'test' + ], + 'user' => [ + 'identityClass' => DummyUser::class + ] + ], + 'on beforeRequest' => function() { + if (isset(\Yii::$app->params['throw'])) { + throw \Yii::$app->params['throw']; + } + } +]; \ No newline at end of file diff --git a/tests/cases/simple/controllers/SiteController.php b/tests/cases/simple/controllers/SiteController.php new file mode 100644 index 0000000..7d8a550 --- /dev/null +++ b/tests/cases/simple/controllers/SiteController.php @@ -0,0 +1,72 @@ + + +
+ +Submit +
+ + + + +HTML; + + } + + public function actionPost() + { + \Yii::$app->response->statusCode = 201; + return print_r(\Yii::$app->request->bodyParams, true); + } + + public function actionException() + { + throw new \Exception('This is not an HttpException'); + } + + public function actionEnd() + { + \Yii::$app->response->statusCode = 500; + \Yii::$app->end(); + } + + + /** + * @param Action $action + * @return bool + * @throws \yii\web\BadRequestHttpException + */ + public function beforeAction($action) + { + if ($action->id === 'empty-response') { + \Yii::$app->response->stream = fopen('php://memory', 'r+'); + \Yii::$app->response->content = new EmptyString('Empty!'); + return false; + } + return parent::beforeAction($action); + } + + public function actionEmptyResponse() + { + // Dummy + } + + public function actionIndex() + { + return ''; + } +} \ No newline at end of file diff --git a/tests/cases/simple/functional.suite.yml b/tests/cases/simple/functional.suite.yml new file mode 100644 index 0000000..6da4e38 --- /dev/null +++ b/tests/cases/simple/functional.suite.yml @@ -0,0 +1,13 @@ +# Codeception Test Suite Configuration +# +# Suite for functional tests +# Emulate web requests and make application process them +# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it +# Remove this suite if you don't use frameworks +actor: FunctionalTester +modules: + enabled: + - Yii2 + - Asserts +steps: + - Codeception\Step\ConditionalAssertion diff --git a/tests/cases/simple/functional/SimpleCest.php b/tests/cases/simple/functional/SimpleCest.php new file mode 100644 index 0000000..b171f9b --- /dev/null +++ b/tests/cases/simple/functional/SimpleCest.php @@ -0,0 +1,74 @@ +assertInstanceOf(Application::class, \Yii::$app); + } + + public function testFormSubmit(FunctionalTester $I) + { + $I->amOnPage(['site/form']); + $I->seeResponseCodeIs(200); + + $I->fillField('#test', 'test'); + $I->click('#submit'); + $I->canSeeResponseCodeIs(201); + } + + public function testFormSubmit2(FunctionalTester $I) + { + $I->amOnPage(['site/form']); + $I->seeResponseCodeIs(200); + $I->submitForm('form', [ + 'login-form[login]' => 'user', + 'login-form[password]' => 'test', + ]); + $I->canSeeResponseCodeIs(201); + } + + public function testException(FunctionalTester $I) + { + $I->expectException(new \Exception('This is not an HttpException'), function() use ($I) { + $I->amOnPage(['site/exception']); + }); + $I->assertInstanceOf(Application::class, \Yii::$app); + } + + public function testExceptionInBeforeRequest(FunctionalTester $I) + { + $e = new \Exception('This is not an HttpException'); + \Yii::$app->params['throw'] = $e; + $I->expectException($e, function() use ($I) { + $I->amOnPage(['site/exception']); + }); + } + + public function testExitException(FunctionalTester $I) + { + $I->amOnPage(['site/end']); + $I->seeResponseCodeIs(500); + } + + public function testEmptyResponse(FunctionalTester $I) + { + $I->amOnPage(['site/empty-response']); + $I->seeResponseCodeIs(200); + } + + public function testMissingUser(FunctionalTester $I) + { + $I->expectException(ModuleException::class, function() use ($I) { + $I->amLoggedInAs('nobody'); + }); + $I->amOnPage('site/index'); + $I->assertTrue(\Yii::$app->user->isGuest); + } +} \ No newline at end of file diff --git a/tests/cases/simple/helpers/DummyUser.php b/tests/cases/simple/helpers/DummyUser.php new file mode 100644 index 0000000..8d38582 --- /dev/null +++ b/tests/cases/simple/helpers/DummyUser.php @@ -0,0 +1,77 @@ +value = $value; + } + + public function getValue() + { + return $this->value; + } + + public function __toString() + { + return ''; + } +} \ No newline at end of file diff --git a/tests/cases/sqlite/codeception.yml b/tests/cases/sqlite/codeception.yml new file mode 100644 index 0000000..caf01d1 --- /dev/null +++ b/tests/cases/sqlite/codeception.yml @@ -0,0 +1,15 @@ +paths: + tests: . + log: ../../_output + data: _data + support: ../../_support +namespace: tests\ +extensions: + enabled: + - Codeception\Extension\RunFailed +modules: + config: + Yii2: + configFile: config.php + transaction: true + cleanup: true \ No newline at end of file diff --git a/tests/cases/sqlite/config.php b/tests/cases/sqlite/config.php new file mode 100644 index 0000000..e20ed92 --- /dev/null +++ b/tests/cases/sqlite/config.php @@ -0,0 +1,24 @@ + 'Simple', + 'basePath' => __DIR__, + + 'components' => [ + 'db' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . tempnam(null, '/file0') + ], + 'db1' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . tempnam(null, '/file1') + ], + 'db21' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . ($name = tempnam(null, '/file2')) + ], + 'db22' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . $name + ] + ] +]; \ No newline at end of file diff --git a/tests/cases/sqlite/fixtures/TestFixture.php b/tests/cases/sqlite/fixtures/TestFixture.php new file mode 100644 index 0000000..f57b62f --- /dev/null +++ b/tests/cases/sqlite/fixtures/TestFixture.php @@ -0,0 +1,36 @@ + 'int' + ]; + + public $dbComponents = []; + + public function load() { + foreach($this->dbComponents as $name) { + /** @var Connection $connection */ + $connection = \Yii::$app->get($name); + $connection->createCommand()->createTable($this->tableName, $this->tableConfig)->execute(); + } + } + + public function unload() + { + foreach($this->dbComponents as $name) { + /** @var Connection $connection */ + $connection = \Yii::$app->get($name); + if (in_array($this->tableName, $connection->getSchema()->getTableNames('', true))) { + $connection->createCommand()->dropTable($this->tableName)->execute(); + } + } + } +} \ No newline at end of file diff --git a/tests/cases/sqlite/functional.suite.yml b/tests/cases/sqlite/functional.suite.yml new file mode 100644 index 0000000..f582374 --- /dev/null +++ b/tests/cases/sqlite/functional.suite.yml @@ -0,0 +1,13 @@ +# Codeception Test Suite Configuration +# +# Suite for functional tests +# Emulate web requests and make application process them +# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it +# Remove this suite if you don't use frameworks +actor: FunctionalTester +modules: + enabled: + - Yii2 + - Asserts +steps: + - Codeception\Step\ConditionalAssertion diff --git a/tests/cases/sqlite/functional/SqLiteCest.php b/tests/cases/sqlite/functional/SqLiteCest.php new file mode 100644 index 0000000..a581be8 --- /dev/null +++ b/tests/cases/sqlite/functional/SqLiteCest.php @@ -0,0 +1,46 @@ + TestFixture::class, + 'dbComponents' => ['db1', 'db21'] + ], + ]; + } + + public function testSharedPDO(FunctionalTester $I) + { + /** @var Connection $db1 */ + $db1 = $I->grabComponent('db1'); + $I->assertEquals(['test'], $db1->schema->getTableNames('', true)); + + /** @var Connection $db21 */ + $db21 = $I->grabComponent('db21'); + $I->assertEquals(['test'], $db21->schema->getTableNames('', true)); + + /** @var Connection $db22 */ + $db22 = $I->grabComponent('db22'); + + $I->assertEquals(['test'], $db22->schema->getTableNames('', true)); + } + + public function testTransaction(FunctionalTester $I) + { + /** @var Connection $db1 */ + $db1 = $I->grabComponent('db1'); + $I->assertFalse($db1->isActive); + $db1->open(); + $I->assertNotNull($db1->getTransaction()); + } +} diff --git a/tests/cases/yii2-app-advanced/_data/db.sqlite b/tests/cases/yii2-app-advanced/_data/db.sqlite new file mode 100644 index 0000000..5f5d793 Binary files /dev/null and b/tests/cases/yii2-app-advanced/_data/db.sqlite differ diff --git a/tests/cases/yii2-app-advanced/codeception.yml b/tests/cases/yii2-app-advanced/codeception.yml new file mode 100644 index 0000000..744faf9 --- /dev/null +++ b/tests/cases/yii2-app-advanced/codeception.yml @@ -0,0 +1,15 @@ +namespace: frontend\tests +actor_suffix: Tester +paths: + tests: ../../../vendor/yiisoft/yii2-app-advanced/frontend/tests + log: ../../_output + data: ../../../vendor/yiisoft/yii2-app-advanced/frontend/tests/_data + support: ../../../vendor/yiisoft/yii2-app-advanced/frontend/tests/_support +bootstrap: _bootstrap.php +settings: + colors: true + memory_limit: 1024M +modules: + config: + Yii2: + configFile: 'config.php' diff --git a/tests/cases/yii2-app-advanced/config.php b/tests/cases/yii2-app-advanced/config.php new file mode 100644 index 0000000..82c61da --- /dev/null +++ b/tests/cases/yii2-app-advanced/config.php @@ -0,0 +1,32 @@ + [ + 'db' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . codecept_output_dir('/db.sqlite') + ], + 'request' => [ + 'cookieValidationKey' => 'test' + ], + 'assetManager' => [ + 'basePath' => sys_get_temp_dir() + ], + 'mailer' => [ + 'viewPath' => '@common/mail', + ], + ], + 'aliases' => [ + '@bower' => '@vendor/bower-asset', + '@npm' => '@vendor/npm-asset' + ], + 'vendorPath' => __DIR__ . '/../../../vendor' +]); + +return $config; \ No newline at end of file diff --git a/tests/cases/yii2-app-advanced/migrate.php b/tests/cases/yii2-app-advanced/migrate.php new file mode 100644 index 0000000..a910536 --- /dev/null +++ b/tests/cases/yii2-app-advanced/migrate.php @@ -0,0 +1,17 @@ + [ + 'db' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'sqlite:' . __DIR__ . '/_data/db.sqlite' + ], + ], + 'vendorPath' => __DIR__ . '/../../../vendor' +]); + +return $config;