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 @@ + +
+ + +