From 3f597fd45d68309bddabd24055c49665361fd4d8 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 22 Aug 2014 10:06:42 -0400 Subject: [PATCH 01/16] updated doc [skip ci] --- docs/guide/db-migrations.md | 2 +- framework/db/Migration.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index adc9751d61..a971bb245c 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -293,7 +293,7 @@ line: migration history information. It defaults to `migration`. The table structure is `version varchar(255) primary key, apply_time integer`. -* `connectionID`: string, specifies the ID of the database application component. +* `db`: string, specifies the ID of the database application component. Defaults to 'db'. * `templateFile`: string, specifies the path of the file to be served as the code diff --git a/framework/db/Migration.php b/framework/db/Migration.php index 6326636470..6369df1597 100644 --- a/framework/db/Migration.php +++ b/framework/db/Migration.php @@ -40,7 +40,10 @@ class Migration extends Component implements MigrationInterface { /** * @var Connection|string the DB connection object or the application component ID of the DB connection - * that this migration should work with. + * that this migration should work with. Note that when a Migration object is created by + * the `migrate` command, this property will be overwritten by the command. If you do not want to + * use the DB connection provided by the command, you may override the [[init()]] method + * and set this property after calling `parent::init()`. */ public $db = 'db'; From d86691240cb4fa93e01381d21e070184165578aa Mon Sep 17 00:00:00 2001 From: Ragazzo Date: Fri, 22 Aug 2014 14:53:51 +0400 Subject: [PATCH 02/16] fixed console guide and docs of FixtureController --- docs/guide/test-fixtures.md | 10 +++++----- docs/guide/tutorial-console.md | 8 ++++++++ framework/console/controllers/FixtureController.php | 12 ++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/guide/test-fixtures.md b/docs/guide/test-fixtures.md index ddd243565e..4ee2962142 100644 --- a/docs/guide/test-fixtures.md +++ b/docs/guide/test-fixtures.md @@ -305,13 +305,13 @@ yii fixture User UserProfile yii fixture User --append // load all fixtures -yii fixture/load * +yii fixture/load "*" // same as above -yii fixture * +yii fixture "*" // load all fixtures except ones -yii fixture * -DoNotLoadThisOne +yii fixture "*" -DoNotLoadThisOne // load fixtures, but search them in different namespace. By default namespace is: tests\unit\fixtures. yii fixture User --namespace='alias\my\custom\namespace' @@ -335,10 +335,10 @@ yii fixture/unload User yii fixture/unload User,UserProfile // unload all fixtures -yii fixture/unload all +yii fixture/unload "*" // unload all fixtures except ones -yii fixture/unload all -DoNotUnloadThisOne +yii fixture/unload "*" -DoNotUnloadThisOne ``` diff --git a/docs/guide/tutorial-console.md b/docs/guide/tutorial-console.md index af49f56b28..019a5f4ab6 100644 --- a/docs/guide/tutorial-console.md +++ b/docs/guide/tutorial-console.md @@ -77,6 +77,9 @@ file via the `appconfig` option when executing the command: yii --appconfig=path/to/config.php ... ``` +> Note: When specifying in console commands * as an input argument you should quote as "*", to avoid executing it as a shell command on +some platforms. + Creating your own console commands ---------------------------------- @@ -151,3 +154,8 @@ public function actionIndex() return 0; } ``` + +There are some predefined console controller constants for this cases like the following: + +- Controller::EXIT_CODE_NORMAL; +- Controller::EXIT_CODE_ERROR. diff --git a/framework/console/controllers/FixtureController.php b/framework/console/controllers/FixtureController.php index 37b0bf4c44..181f00ded8 100644 --- a/framework/console/controllers/FixtureController.php +++ b/framework/console/controllers/FixtureController.php @@ -25,10 +25,10 @@ use yii\test\FixtureTrait; * yii fixture User * * #load all fixtures - * yii fixture * + * yii fixture "*" * * #load all fixtures except User - * yii fixture * -User + * yii fixture "*" -User * * #append fixtures to already loaded * yii fixture User --append @@ -92,10 +92,10 @@ class FixtureController extends Controller * yii fixture/load --append User UserProfile * * # load all available fixtures found under 'tests\unit\fixtures' - * yii fixture/load * + * yii fixture/load "*" * * # load all fixtures except User and UserProfile - * yii fixture/load * -User -UserProfile + * yii fixture/load "*" -User -UserProfile * ~~~ * * @throws Exception if the specified fixture does not exist. @@ -164,10 +164,10 @@ class FixtureController extends Controller * yii fixture/unload User UserProfile * * # unload all fixtures found under 'tests\unit\fixtures' - * yii fixture/unload * + * yii fixture/unload "*" * * # unload all fixtures except User and UserProfile - * yii fixture/unload * -User -UserProfile + * yii fixture/unload "*" -User -UserProfile * ~~~ * * @throws Exception if the specified fixture does not exist. From da179e3bd4e89806bec72d5948a72421938f5e49 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 23 Aug 2014 00:42:55 +0400 Subject: [PATCH 03/16] Minor text adjustments --- docs/guide/tutorial-console.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/guide/tutorial-console.md b/docs/guide/tutorial-console.md index 019a5f4ab6..6f2132b7b9 100644 --- a/docs/guide/tutorial-console.md +++ b/docs/guide/tutorial-console.md @@ -77,8 +77,8 @@ file via the `appconfig` option when executing the command: yii --appconfig=path/to/config.php ... ``` -> Note: When specifying in console commands * as an input argument you should quote as "*", to avoid executing it as a shell command on -some platforms. +> **Note**: When using `*` in console don't forget to quote it as `"*"` in order to avoid executing it as a shell +> command. Creating your own console commands @@ -155,7 +155,7 @@ public function actionIndex() } ``` -There are some predefined console controller constants for this cases like the following: +There are some predefined constants you can use: -- Controller::EXIT_CODE_NORMAL; -- Controller::EXIT_CODE_ERROR. +- `Controller::EXIT_CODE_NORMAL` with value of `0`; +- `Controller::EXIT_CODE_ERROR` with value of `1`. From 8b37d2c196f25faaebd3ae352170ecdf4f03977a Mon Sep 17 00:00:00 2001 From: Ragazzo Date: Thu, 21 Aug 2014 20:19:56 +0400 Subject: [PATCH 04/16] Docs about testing --- docs/guide/test-environment-setup.md | 47 ++++++++++++++ docs/guide/test-overview.md | 71 ++++++++++++++-------- docs/guide/testing-advanced-application.md | 5 ++ docs/guide/testing-basic-application.md | 5 ++ 4 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 docs/guide/test-environment-setup.md create mode 100644 docs/guide/testing-advanced-application.md create mode 100644 docs/guide/testing-basic-application.md diff --git a/docs/guide/test-environment-setup.md b/docs/guide/test-environment-setup.md new file mode 100644 index 0000000000..94effdc5fe --- /dev/null +++ b/docs/guide/test-environment-setup.md @@ -0,0 +1,47 @@ +Testing environments setup +========================== + +> Note: This section is under development. + +By default Yii2 is bundled with [`Codeception`](https://github.com/Codeception/Codeception) testing framework +that allows you to write unit tests as functional and acceptance tests for UI too. You can also use other testing frameworks +and third party libs like: [Phpunit](https://github.com/sebastianbergmann/phpunit/), [Behat](https://github.com/Behat/Behat), [AspectMock](https://github.com/Codeception/AspectMock), [Mockery](https://github.com/padraic/mockery) and so on. + + +## Codeception + +Codeception testing support provided by Yii includes: + +- [Unit testing](test-unit.md) - verifies that a single unit of code is working as expected; +- [Functional testing](test-functional.md) - verifies scenarios from a user's perspective via browser emulation; +- [Acceptance testing](test-acceptance.md) - verifies scenarios from a user's perspective in a browser. + +Yii provides ready to use test sets for all three testing types in both [`yii2-basic`](https://github.com/yiisoft/yii2/tree/master/apps/basic) and [`yii2-advanced`](https://github.com/yiisoft/yii2/tree/master/apps/advanced) application templates. + +In order to run tests with Yii you need to install [Codeception](https://github.com/Codeception/Codeception). A good way to install it is +the following: + +``` +composer global require "codeception/codeception=2.0.*" +composer global require "codeception/specify=*" +composer global require "codeception/verify=*" +``` + +If you've never used Composer for global packages run `composer global status`. It should output: + +``` +Changed current directory to +``` + +Then add `/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command +line globally. + + +## Phpunit + + TBD + + +## Behat + + TBD diff --git a/docs/guide/test-overview.md b/docs/guide/test-overview.md index 3d20db3818..0d2a32a678 100644 --- a/docs/guide/test-overview.md +++ b/docs/guide/test-overview.md @@ -1,12 +1,6 @@ Testing ======= -> Note: This section is under development. - -TODO: - -- https://github.com/yiisoft/yii2/blob/master/extensions/codeception/README.md - Testing is an important part of software development. Whether we are aware of it or not, we conduct testing continuously. For example, when we write a class in PHP, we may debug it step by step or simply use echo or die statements to verify that implementation is correct. In case of web application we're entering some test data in forms to ensure the page @@ -14,31 +8,56 @@ interacts with us as expected. The testing process could be automated so that ea we just need to call up the code that perform testing for us. This is known as automated testing, which is the main topic of testing chapters. -The testing support provided by Yii includes: +[Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development) is an approach of developing software when you write your code in repeatable steps that verifies produced features +with needed assertions. -- [Unit testing](test-unit.md) - verifies that a single unit of code is working as expected. -- [Functional testing](test-functional.md) - verifies scenarios from a user's perspective via browser emulation. -- [Acceptance testing](test-acceptance.md) - verifies scenarios from a user's perspective in a browser. +Test - block of program that verifies correct or not correct behavior of needed feature. All tests should be automated to be able to make them +run often and fast. -Yii provides ready to use test sets for all three testing types in both basic and advanced application templates. +Main life cycle of this approach contains following steps: -Test environment setup ----------------------- +- Create a new test that covers a feature to be implemented. The test is expected to fail at its first execution because the feature has yet to be implemented; +- Run test and make sure the new test fails; +- Write simple code to make the new test pass; +- Run all tests and make sure they all pass; +- Refactor the code in needed way and make sure the tests still pass. -In order to run tests with Yii you need to install [Codeception](http://codeception.com/). A good way to install it is -the following: +Repeat step 1 to 5 to push forward the functionality implementation. Depending on your skills you can increase or decrease size +of code that you change between steps and tests run. If you feel that you are doing a lot of simple changes and loosing your time then it is +time to make steps bigger, but if you started to spend a lot of time on debug when writing tests it is a good sign to make steps slower. +Mainly you should write code only before test to be able to understand what it does and to avoid leaks of code implementation into tests. This approach +is called `Test First`. It is a vital part of testing to understand that code implementation should not leak to your tests, thus you will +be able to concentrate more on abstractions of code and features, rather then particular implementation. Tests that have such leaks becoming +hard to maintain and understand, and as a conclusion they are deleted. However depending on your understanding of TDD and your skills of writing good decoupled code you can write tests after +the implementation is done, there is nothing wrong about it, while you are understanding what you are doing and why. -``` -composer global require "codeception/codeception=2.0.*" -composer global require "codeception/specify=*" -composer global require "codeception/verify=*" -``` +There are a lot of reasons why you should use TDD, below are the common ones: -If you've never used Composer for global packages run `composer global status`. It should output: +- it saves your time on debug in long term; +- it keeps you concentrated only on one thing - currently developing feature, and not in whole project. Thus you only write code that you really need; +- it lets you know immediately if something will go wrong or functionality of some features will be broken; +- it lets you understand the use case of feature you are developing now and you are able to build systems with clear interfaces; +- it lets you understand dependencies between code and how to solve them to make system decoupled, particular with [Inversion Of Control](http://en.wikipedia.org/wiki/Inversion_of_control) principle. -``` -Changed current directory to -``` +TDD can help you to build good decoupled systems, but it will not replace some basic knowledges of refactoring and structuring code, so you +should not consider it as a `silver bullet`. It is almost always that you will think about code, features and specifications before writing any test case. +There are other software approaches to help you with it, like : [Behavior Driven Development (BDD)](http://en.wikipedia.org/wiki/Behavior-driven_development) and [Domain Driven Development (DDD)](https://en.wikipedia.org/wiki/Domain-driven_design). -Then add `/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command -line globally. +However it is not always necessary to use TDD in your project, you should consider below cases of when you can or should use it: + +- you are working on big project or project that gets bigger; +- you are working on long term project; +- you have time for TDD, since tests should not be written once and forgotten, they should be also crafted as other code; +- you have correct environment that allows you to use TDD. It can be testing frameworks, fixtures frameworks and so on; +- you are working on project with legacy code that should be rewritten fully or partial; +- your team understand the value of bringing good quality code to business. + +If you are working on simple CRUD or small projects usually you dont need TDD, since it can take a half of all time in developing with small projects. +When working on legacy code it is very hard to apply TDD due to hard system coupling and wrong architectural design issues, however by isolating some code blocks +and working with them one by one in small parts you can rewrite it much faster with TDD than without it, since you will be sure everything is works correctly and you can move on. + +Below are listed books where you can find more answers for your TDD quesitons: + +- "Test-driven Development by Example", Kent Beck; + +To know what testing frameworks and environments is bundled or supported by Yii2 you can see [Test Environment Setup](test-environment-setup.md) guide. diff --git a/docs/guide/testing-advanced-application.md b/docs/guide/testing-advanced-application.md new file mode 100644 index 0000000000..49a5cbbda9 --- /dev/null +++ b/docs/guide/testing-advanced-application.md @@ -0,0 +1,5 @@ +Advanced application testing environment setup +=========================================== + + TBD + diff --git a/docs/guide/testing-basic-application.md b/docs/guide/testing-basic-application.md new file mode 100644 index 0000000000..099c4312b1 --- /dev/null +++ b/docs/guide/testing-basic-application.md @@ -0,0 +1,5 @@ +Basic application testing environment setup +=========================================== + + TBD + From fda734ce7e635a61972057d750e825b936880abc Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 23 Aug 2014 02:50:48 +0400 Subject: [PATCH 05/16] Rewritten testing introduction, added environment setup to README.md, removed empty pages that were not referenced --- docs/guide/README.md | 1 + docs/guide/test-environment-setup.md | 34 +++----- docs/guide/test-overview.md | 92 +++++++++++----------- docs/guide/testing-advanced-application.md | 5 -- docs/guide/testing-basic-application.md | 5 -- 5 files changed, 59 insertions(+), 78 deletions(-) delete mode 100644 docs/guide/testing-advanced-application.md delete mode 100644 docs/guide/testing-basic-application.md diff --git a/docs/guide/README.md b/docs/guide/README.md index b69888044d..73b87054e8 100644 --- a/docs/guide/README.md +++ b/docs/guide/README.md @@ -151,6 +151,7 @@ Testing ------- * [Overview](test-overview.md) +* [Testing environment setup](test-endvironment-setup.md) * [Unit Tests](test-unit.md) * [Functional Tests](test-functional.md) * [Acceptance Tests](test-acceptance.md) diff --git a/docs/guide/test-environment-setup.md b/docs/guide/test-environment-setup.md index 94effdc5fe..28ecc33f8d 100644 --- a/docs/guide/test-environment-setup.md +++ b/docs/guide/test-environment-setup.md @@ -1,25 +1,21 @@ -Testing environments setup -========================== +Testing environment setup +====================== > Note: This section is under development. -By default Yii2 is bundled with [`Codeception`](https://github.com/Codeception/Codeception) testing framework -that allows you to write unit tests as functional and acceptance tests for UI too. You can also use other testing frameworks -and third party libs like: [Phpunit](https://github.com/sebastianbergmann/phpunit/), [Behat](https://github.com/Behat/Behat), [AspectMock](https://github.com/Codeception/AspectMock), [Mockery](https://github.com/padraic/mockery) and so on. - - -## Codeception - -Codeception testing support provided by Yii includes: +Yii2 has officially maintained integration with [`Codeception`](https://github.com/Codeception/Codeception) testing +framework that allows you to create the following test types: - [Unit testing](test-unit.md) - verifies that a single unit of code is working as expected; - [Functional testing](test-functional.md) - verifies scenarios from a user's perspective via browser emulation; - [Acceptance testing](test-acceptance.md) - verifies scenarios from a user's perspective in a browser. -Yii provides ready to use test sets for all three testing types in both [`yii2-basic`](https://github.com/yiisoft/yii2/tree/master/apps/basic) and [`yii2-advanced`](https://github.com/yiisoft/yii2/tree/master/apps/advanced) application templates. +Yii provides ready to use test sets for all three test types in both +[`yii2-basic`](https://github.com/yiisoft/yii2/tree/master/apps/basic) and +[`yii2-advanced`](https://github.com/yiisoft/yii2/tree/master/apps/advanced) application templates. -In order to run tests with Yii you need to install [Codeception](https://github.com/Codeception/Codeception). A good way to install it is -the following: +In order to run tests you need to install [Codeception](https://github.com/Codeception/Codeception). A good way to +install it is the following: ``` composer global require "codeception/codeception=2.0.*" @@ -27,7 +23,7 @@ composer global require "codeception/specify=*" composer global require "codeception/verify=*" ``` -If you've never used Composer for global packages run `composer global status`. It should output: +If you've never used Composer for global packages before, run `composer global status`. It should output: ``` Changed current directory to @@ -35,13 +31,3 @@ Changed current directory to Then add `/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command line globally. - - -## Phpunit - - TBD - - -## Behat - - TBD diff --git a/docs/guide/test-overview.md b/docs/guide/test-overview.md index 0d2a32a678..05a51e45a2 100644 --- a/docs/guide/test-overview.md +++ b/docs/guide/test-overview.md @@ -3,61 +3,65 @@ Testing Testing is an important part of software development. Whether we are aware of it or not, we conduct testing continuously. For example, when we write a class in PHP, we may debug it step by step or simply use echo or die statements to verify -that implementation is correct. In case of web application we're entering some test data in forms to ensure the page -interacts with us as expected. The testing process could be automated so that each time when we need to test something, -we just need to call up the code that perform testing for us. This is known as automated testing, which is the main topic -of testing chapters. +that implementation works according to our initial plan. In case of web application we're entering some test data in forms +to ensure the page interacts with us as expected. The testing process could be automated so that each time when we need +to verify something, we just need to call up the code that do it for us. The code that verifies that result matches what +we've planned is called test and the process of its creation and further execution is known as automated testing, which +is the main topic of testing chapters. -[Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development) is an approach of developing software when you write your code in repeatable steps that verifies produced features -with needed assertions. -Test - block of program that verifies correct or not correct behavior of needed feature. All tests should be automated to be able to make them -run often and fast. +Developing with tests +------------------ -Main life cycle of this approach contains following steps: +Test-Driven Development (TDD) or, in another terms, Behavior-Driven Development (BDD) is an approach of developing +software by describing behavior of a piece of code or the whole feature as a set of scenarios or tests before +writing actual code and only then creating the implementation that allows these tests to pass verifying that intended +behavior is achieved. -- Create a new test that covers a feature to be implemented. The test is expected to fail at its first execution because the feature has yet to be implemented; -- Run test and make sure the new test fails; -- Write simple code to make the new test pass; -- Run all tests and make sure they all pass; -- Refactor the code in needed way and make sure the tests still pass. +The process of developing a feature is the following: -Repeat step 1 to 5 to push forward the functionality implementation. Depending on your skills you can increase or decrease size -of code that you change between steps and tests run. If you feel that you are doing a lot of simple changes and loosing your time then it is -time to make steps bigger, but if you started to spend a lot of time on debug when writing tests it is a good sign to make steps slower. -Mainly you should write code only before test to be able to understand what it does and to avoid leaks of code implementation into tests. This approach -is called `Test First`. It is a vital part of testing to understand that code implementation should not leak to your tests, thus you will -be able to concentrate more on abstractions of code and features, rather then particular implementation. Tests that have such leaks becoming -hard to maintain and understand, and as a conclusion they are deleted. However depending on your understanding of TDD and your skills of writing good decoupled code you can write tests after -the implementation is done, there is nothing wrong about it, while you are understanding what you are doing and why. +- Create a new test that describes a feature to be implemented. +- Run new test and make sure it fails. It is expected since there's no implementation yet. +- Write simple code to make the new test pass. +- Run all tests and make sure they all pass. +- Improve code and make sure tests are still OK. -There are a lot of reasons why you should use TDD, below are the common ones: +> **Tip**: If you feel that you are loosing time doing a lot of simple changes try covering more by your test scenario +> so you do more changes before executing tests again. If you're debugging too much try doing the opposite. -- it saves your time on debug in long term; -- it keeps you concentrated only on one thing - currently developing feature, and not in whole project. Thus you only write code that you really need; -- it lets you know immediately if something will go wrong or functionality of some features will be broken; -- it lets you understand the use case of feature you are developing now and you are able to build systems with clear interfaces; -- it lets you understand dependencies between code and how to solve them to make system decoupled, particular with [Inversion Of Control](http://en.wikipedia.org/wiki/Inversion_of_control) principle. +The reason to create tests before doing any implemenation is that it allows you to focus on what do we want to achieve +and fully dive into "how to do it" afterwards. Usually it leads to better abstractions and easier test maintenance when +it comes to feature adjustments. -TDD can help you to build good decoupled systems, but it will not replace some basic knowledges of refactoring and structuring code, so you -should not consider it as a `silver bullet`. It is almost always that you will think about code, features and specifications before writing any test case. -There are other software approaches to help you with it, like : [Behavior Driven Development (BDD)](http://en.wikipedia.org/wiki/Behavior-driven_development) and [Domain Driven Development (DDD)](https://en.wikipedia.org/wiki/Domain-driven_design). +So to sum up pros of such approach are the following: -However it is not always necessary to use TDD in your project, you should consider below cases of when you can or should use it: +- Keeps you focused on one thing at a time so both planning and implementation are getting better. +- Results in test-covering more features in greater detail i.e. if tests are OK most probably nothing's broken. -- you are working on big project or project that gets bigger; -- you are working on long term project; -- you have time for TDD, since tests should not be written once and forgotten, they should be also crafted as other code; -- you have correct environment that allows you to use TDD. It can be testing frameworks, fixtures frameworks and so on; -- you are working on project with legacy code that should be rewritten fully or partial; -- your team understand the value of bringing good quality code to business. +In the long term it usually gives you a good time-saving effect. -If you are working on simple CRUD or small projects usually you dont need TDD, since it can take a half of all time in developing with small projects. -When working on legacy code it is very hard to apply TDD due to hard system coupling and wrong architectural design issues, however by isolating some code blocks -and working with them one by one in small parts you can rewrite it much faster with TDD than without it, since you will be sure everything is works correctly and you can move on. +> **Tip**: If you want to know more about the principles for gathering software requirements and modeling the subject +> matter it's good to learn [Domain Driven Development (DDD)](https://en.wikipedia.org/wiki/Domain-driven_design). -Below are listed books where you can find more answers for your TDD quesitons: +When and how to test +------------------ -- "Test-driven Development by Example", Kent Beck; +While test first approach described above makes sense for long term and relatively complex projects it could be overkill +for simpler ones. There are some indicators of when it's appropriate: -To know what testing frameworks and environments is bundled or supported by Yii2 you can see [Test Environment Setup](test-environment-setup.md) guide. +- Project is already large and complex. +- Project requirements are starting to get complex. Project grows constantly. +- Project is meant to be long term. +- The cost of the failure is too high. + +There's nothing wrong in creating tests covering behavior of existing implementation. + +- Project is a legacy one to be gradually renewed. +- You've got a project to work on and it has no tests. + +In some cases any form of automated testing could be overkill: + +- Project is simple and isn't getting any complex. +- It's one-time project that's going to be expired. + +Still if you have time it's good to automate testing in these cases as well. diff --git a/docs/guide/testing-advanced-application.md b/docs/guide/testing-advanced-application.md deleted file mode 100644 index 49a5cbbda9..0000000000 --- a/docs/guide/testing-advanced-application.md +++ /dev/null @@ -1,5 +0,0 @@ -Advanced application testing environment setup -=========================================== - - TBD - diff --git a/docs/guide/testing-basic-application.md b/docs/guide/testing-basic-application.md deleted file mode 100644 index 099c4312b1..0000000000 --- a/docs/guide/testing-basic-application.md +++ /dev/null @@ -1,5 +0,0 @@ -Basic application testing environment setup -=========================================== - - TBD - From 93d586e9d3bf33059dd8a2e0ba545414dcf8b1f7 Mon Sep 17 00:00:00 2001 From: Vasiliy Baukin Date: Sat, 23 Aug 2014 22:02:40 +0400 Subject: [PATCH 06/16] expanded db-migration doc --- docs/guide/db-migrations.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index a971bb245c..e04bccd0e1 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -337,3 +337,26 @@ the console application's configuration file like the following, Now if we run the `migrate` command, the above configurations will take effect without requiring us to enter the command line options every time. Other command options can be also configured this way. + +### Managing migrations for multiple databases +The preferred way to manage migrations for multiple databases is to create a directory for each database's migrations, and then run the migrate command for each database like this: + +``` +yii migrate --migrationPath=@app/migrations/db2 --db=db2 +``` + +``` +yii migrate create --migrationPath=@app/migrations/db2 --db=db2 +``` +This way you have full control on which database corresponds to which migration directory. Another benefit is that migration history is kept in the database migrations are applied to. So each database would keep it's migration history. + + Alternative approach is set the database, which the migration should be performed on, in the migration class. To do so, we need to override the [[yii\db\Migration::init()]] method as follows: +```php + public function init() + { + parent::init(); + $this->db = Yii::$app->dbConnectionId; + } + +``` +where `dbConnectionId` is ID of the database application component. This approach is much faster to implement and use, you create and apply migrations running migrate command as usual, without providing extra parameters. We recommend using [[yii\db\Migration::init()]] with caution though, as in this case the migration history is saved to the database configured in [[yii\console\controllers\MigrateController::db]] (database which MigrateController currently operates on). From 5a87ebadd4139e77935a5e1c179071c6d1ce73b1 Mon Sep 17 00:00:00 2001 From: Vasiliy Baukin Date: Sat, 23 Aug 2014 22:14:15 +0400 Subject: [PATCH 07/16] Update db-migrations.md fix a typo. --- docs/guide/db-migrations.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index e04bccd0e1..7db9815271 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -359,4 +359,5 @@ This way you have full control on which database corresponds to which migration } ``` -where `dbConnectionId` is ID of the database application component. This approach is much faster to implement and use, you create and apply migrations running migrate command as usual, without providing extra parameters. We recommend using [[yii\db\Migration::init()]] with caution though, as in this case the migration history is saved to the database configured in [[yii\console\controllers\MigrateController::db]] (database which MigrateController currently operates on). +where `dbConnectionId` is ID of the database application component. This approach is much faster to implement and use, you create and apply migrations running migrate command as usual, without providing extra parameters. We recommend using [[yii\db\Migration::init()]] with caution though, as in this case the migration history is saved to the database configured in [[yii\console\controllers\MigrateController::db]] + - database which MigrateController currently operates on. From 385e13978dc1e971014cd2c11702e3ec1b22ee18 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 23 Aug 2014 18:18:31 -0400 Subject: [PATCH 08/16] Fixes #4800. --- docs/guide/db-migrations.md | 48 +++++++++++++++++++++++++------------ framework/db/Migration.php | 11 +++++++-- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index 7db9815271..6434addd25 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -338,26 +338,44 @@ Now if we run the `migrate` command, the above configurations will take effect without requiring us to enter the command line options every time. Other command options can be also configured this way. -### Managing migrations for multiple databases -The preferred way to manage migrations for multiple databases is to create a directory for each database's migrations, and then run the migrate command for each database like this: + +### Migrating with Multiple Databases + +By default, migrations will be applied to the database specified by the `db` application component. +You may change it by specifying the `--db` option, for example, ``` -yii migrate --migrationPath=@app/migrations/db2 --db=db2 +yii migrate --db=db2 ``` -``` -yii migrate create --migrationPath=@app/migrations/db2 --db=db2 -``` -This way you have full control on which database corresponds to which migration directory. Another benefit is that migration history is kept in the database migrations are applied to. So each database would keep it's migration history. +The above command will apply *all* migrations found in the default migration path to the `db2` database. + +If your application works with multiple databases, some migrations should be applied to one database while +some others should be applied to another database. In this case, it is recommended that you create a base +migration class for each different database and override the [[yii\db\Migration::init()]] method like the following, - Alternative approach is set the database, which the migration should be performed on, in the migration class. To do so, we need to override the [[yii\db\Migration::init()]] method as follows: ```php - public function init() - { - parent::init(); - $this->db = Yii::$app->dbConnectionId; - } +public function init() +{ + $this->db = 'db2'; + parent::init(); +} +``` + +If a migration needs to be applied to one database, you create a migration class by extending from the corresponding +base migration class. + +Now if you run the `yii migrate` command, each migration will be applied to the corresponding database. +Because each migration has harcoded the DB connection, the `--db` option of the `migrate` command will have no effect. + +If you still want to support changing DB connection via the `--db` option, you may take the following alternative +approach to work with multiple databases. + +For each database, create a migration path and save all corresponding migration classes there. To apply migrations, +run the command as follows, ``` -where `dbConnectionId` is ID of the database application component. This approach is much faster to implement and use, you create and apply migrations running migrate command as usual, without providing extra parameters. We recommend using [[yii\db\Migration::init()]] with caution though, as in this case the migration history is saved to the database configured in [[yii\console\controllers\MigrateController::db]] - - database which MigrateController currently operates on. +yii migrate --migrationPath=@app/migrations/db1 --db=db1 +yii migrate --migrationPath=@app/migrations/db2 --db=db2 +... +``` diff --git a/framework/db/Migration.php b/framework/db/Migration.php index 6369df1597..25fff24042 100644 --- a/framework/db/Migration.php +++ b/framework/db/Migration.php @@ -42,8 +42,15 @@ class Migration extends Component implements MigrationInterface * @var Connection|string the DB connection object or the application component ID of the DB connection * that this migration should work with. Note that when a Migration object is created by * the `migrate` command, this property will be overwritten by the command. If you do not want to - * use the DB connection provided by the command, you may override the [[init()]] method - * and set this property after calling `parent::init()`. + * use the DB connection provided by the command, you may override the [[init()]] method like the following: + * + * ```php + * public function init() + * { + * $this->db = 'db2'; + * parent::init(); + * } + * ``` */ public $db = 'db'; From 3b705855aaf5f4260c92c43de415fdd39c9dd8b7 Mon Sep 17 00:00:00 2001 From: wenbin1989 Date: Mon, 18 Aug 2014 15:12:15 +0800 Subject: [PATCH 09/16] fix bugs of BlameableBehavior in console application. In console application, \yii\console\Application doesn't have `getUser()` method. Use BlameableBehavior in console application will cause an exception. --- framework/behaviors/BlameableBehavior.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/framework/behaviors/BlameableBehavior.php b/framework/behaviors/BlameableBehavior.php index 791a07748a..bd4c5b8b2e 100644 --- a/framework/behaviors/BlameableBehavior.php +++ b/framework/behaviors/BlameableBehavior.php @@ -102,9 +102,12 @@ class BlameableBehavior extends AttributeBehavior protected function getValue($event) { if ($this->value === null) { - $user = Yii::$app->getUser(); - - return $user && !$user->isGuest ? $user->id : null; + if (Yii::$app->hasMethod('getUser')) { + $user = Yii::$app->getUser(); + return $user && !$user->isGuest ? $user->id : null; + } else { + return null; + } } else { return call_user_func($this->value, $event); } From ce6f33af28c69a3afe372efec1cc5a0913f7b96f Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 23 Aug 2014 18:32:24 -0400 Subject: [PATCH 10/16] Fixes #4736 --- framework/behaviors/BlameableBehavior.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/framework/behaviors/BlameableBehavior.php b/framework/behaviors/BlameableBehavior.php index bd4c5b8b2e..b1ff94f08a 100644 --- a/framework/behaviors/BlameableBehavior.php +++ b/framework/behaviors/BlameableBehavior.php @@ -102,12 +102,8 @@ class BlameableBehavior extends AttributeBehavior protected function getValue($event) { if ($this->value === null) { - if (Yii::$app->hasMethod('getUser')) { - $user = Yii::$app->getUser(); - return $user && !$user->isGuest ? $user->id : null; - } else { - return null; - } + $user = Yii::$app->get('user', false); + return $user && !$user->isGuest ? $user->id : null; } else { return call_user_func($this->value, $event); } From 7d795c620e56d13bf81be79f79f467b9c741fbbc Mon Sep 17 00:00:00 2001 From: Larry Ullman Date: Sat, 23 Aug 2014 20:06:42 -0400 Subject: [PATCH 11/16] First pass of edits --- docs/guide/rest-versioning.md | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/docs/guide/rest-versioning.md b/docs/guide/rest-versioning.md index 0b876ca742..50b4e1ae00 100644 --- a/docs/guide/rest-versioning.md +++ b/docs/guide/rest-versioning.md @@ -1,16 +1,18 @@ Versioning ========== -Your APIs should be versioned. Unlike Web applications which you have full control on both client side and server side -code, for APIs you usually do not have control of the client code that consumes the APIs. Therefore, backward -compatibility (BC) of the APIs should be maintained whenever possible, and if some BC-breaking changes must be -introduced to the APIs, you should bump up the version number. You may refer to [Semantic Versioning](http://semver.org/) -for more information about designing the version numbers of your APIs. +A good API is *versioned*: changes and new features are implemented in new versions of the API instead of continually altering just one version. Unlike Web applications, with which you have full control of both the client-side and server-side +code, APIs are meant to be used by clients beyond your control. For this reason, backward +compatibility (BC) of the APIs should be maintained whenever possible. If a change that may break BC is necessary, you should introduce it in new version of the API, and bump up the version number. Existing clients can continue to use the old, working version of the API; and new or upgraded clients can get the new functionality in the new API version. -Regarding how to implement API versioning, a common practice is to embed the version number in the API URLs. -For example, `http://example.com/v1/users` stands for `/users` API of version 1. Another method of API versioning -which gains momentum recently is to put version numbers in the HTTP request headers, typically through the `Accept` header, -like the following: +> Tip: Refer to [Semantic Versioning](http://semver.org/) +for more information on designing API version numbers. + +One common way to implement API versioning is to embed the version number in the API URLs. +For example, `http://example.com/v1/users` stands for the `/users` endpoint of API version 1. + +Another method of API versioning, +which has gained momentum recently, is to put the version number in the HTTP request headers. This is typically done through the `Accept` header: ``` // via a parameter @@ -19,16 +21,16 @@ Accept: application/json; version=v1 Accept: application/vnd.company.myapp-v1+json ``` -Both methods have pros and cons, and there are a lot of debates about them. Below we describe a practical strategy -of API versioning that is kind of a mix of these two methods: +Both methods have their pros and cons, and there are a lot of debates about each approach. Below you'll see a practical strategy +for API versioning that is a mix of these two methods: * Put each major version of API implementation in a separate module whose ID is the major version number (e.g. `v1`, `v2`). Naturally, the API URLs will contain major version numbers. * Within each major version (and thus within the corresponding module), use the `Accept` HTTP request header to determine the minor version number and write conditional code to respond to the minor versions accordingly. -For each module serving a major version, it should include the resource classes and the controller classes -serving for that specific version. To better separate code responsibility, you may keep a common set of +For each module serving a major version, the module should include the resource and controller classes +serving that specific version. To better separate code responsibility, you may keep a common set of base resource and controller classes, and subclass them in each individual version module. Within the subclasses, implement the concrete code such as `Model::fields()`. @@ -86,11 +88,11 @@ return [ ]; ``` -As a result, `http://example.com/v1/users` will return the list of users in version 1, while +As a result of the above code, `http://example.com/v1/users` will return the list of users in version 1, while `http://example.com/v2/users` will return version 2 users. -Using modules, code for different major versions can be well isolated. And it is still possible -to reuse code across modules via common base classes and other shared classes. +Thanks to modules, the code for different major versions can be well isolated. But modules make it still possible +to reuse code across the modules via common base classes and other shared resources. To deal with minor version numbers, you may take advantage of the content negotiation feature provided by the [[yii\filters\ContentNegotiator|contentNegotiator]] behavior. The `contentNegotiator` @@ -101,7 +103,7 @@ For example, if a request is sent with the HTTP header `Accept: application/json after content negotiation, [[yii\web\Response::acceptParams]] will contain the value `['version' => 'v1']`. Based on the version information in `acceptParams`, you may write conditional code in places -such as actions, resource classes, serializers, etc. +such as actions, resource classes, serializers, etc. to provide the appropriate functionality. -Since minor versions require maintaining backward compatibility, hopefully there are not much +Since minor versions by definition require maintaining backward compatibility, hopefully there would not be many version checks in your code. Otherwise, chances are that you may need to create a new major version. From ca40502888c6ca33bfa712afb2e1ed7c8f7972ab Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 24 Aug 2014 13:37:53 +0400 Subject: [PATCH 12/16] Added more notes and a book reference to testing intro --- docs/guide/test-overview.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/guide/test-overview.md b/docs/guide/test-overview.md index 05a51e45a2..75c1799174 100644 --- a/docs/guide/test-overview.md +++ b/docs/guide/test-overview.md @@ -13,7 +13,7 @@ is the main topic of testing chapters. Developing with tests ------------------ -Test-Driven Development (TDD) or, in another terms, Behavior-Driven Development (BDD) is an approach of developing +Test-Driven Development (TDD) and Behavior-Driven Development (BDD) are approaches of developing software by describing behavior of a piece of code or the whole feature as a set of scenarios or tests before writing actual code and only then creating the implementation that allows these tests to pass verifying that intended behavior is achieved. @@ -26,12 +26,15 @@ The process of developing a feature is the following: - Run all tests and make sure they all pass. - Improve code and make sure tests are still OK. -> **Tip**: If you feel that you are loosing time doing a lot of simple changes try covering more by your test scenario -> so you do more changes before executing tests again. If you're debugging too much try doing the opposite. +After it's done the process is repeated again for another feature or improvement. If existing feature is to be changed, +tests should be changed as well. + +> **Tip**: If you feel that you are loosing time doing a lot of small and simple iterations try covering more by your +> test scenario so you do more before executing tests again. If you're debugging too much try doing the opposite. The reason to create tests before doing any implemenation is that it allows you to focus on what do we want to achieve and fully dive into "how to do it" afterwards. Usually it leads to better abstractions and easier test maintenance when -it comes to feature adjustments. +it comes to feature adjustments in for of less coupled components. So to sum up pros of such approach are the following: @@ -65,3 +68,8 @@ In some cases any form of automated testing could be overkill: - It's one-time project that's going to be expired. Still if you have time it's good to automate testing in these cases as well. + +Further reading +------------- + +- [Test Driven Development: By Example / Kent Beck](http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530/). From 2af195d8f744ccad927f25aeb5c378fd605c26d3 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 24 Aug 2014 13:48:20 +0400 Subject: [PATCH 13/16] Replaced Amazon link with ISBN --- docs/guide/test-overview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/test-overview.md b/docs/guide/test-overview.md index 75c1799174..0243ccec48 100644 --- a/docs/guide/test-overview.md +++ b/docs/guide/test-overview.md @@ -72,4 +72,4 @@ Still if you have time it's good to automate testing in these cases as well. Further reading ------------- -- [Test Driven Development: By Example / Kent Beck](http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530/). +- Test Driven Development: By Example / Kent Beck. ISBN: 0321146530. From c9d51ee0fddec74a5c5da61c1eb933c7b3c8a49e Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 24 Aug 2014 14:14:35 +0400 Subject: [PATCH 14/16] Adjusted testing namespaces in application templates --- apps/advanced/tests/codeception/backend/_bootstrap.php | 2 +- .../tests/codeception/backend/acceptance.suite.yml | 2 +- .../tests/codeception/backend/acceptance/LoginCept.php | 4 ++-- apps/advanced/tests/codeception/backend/codeception.yml | 2 +- .../tests/codeception/backend/functional.suite.yml | 2 +- .../tests/codeception/backend/functional/LoginCept.php | 4 ++-- .../tests/codeception/backend/unit/DbTestCase.php | 2 +- apps/advanced/tests/codeception/backend/unit/TestCase.php | 2 +- apps/advanced/tests/codeception/common/_bootstrap.php | 2 +- .../tests/codeception/common/_pages/LoginPage.php | 2 +- .../tests/codeception/common/_support/FixtureHelper.php | 6 +++--- apps/advanced/tests/codeception/common/codeception.yml | 2 +- .../tests/codeception/common/fixtures/UserFixture.php | 2 +- .../advanced/tests/codeception/common/unit/DbTestCase.php | 4 ++-- apps/advanced/tests/codeception/common/unit/TestCase.php | 4 ++-- .../codeception/common/unit/models/LoginFormTest.php | 8 ++++---- apps/advanced/tests/codeception/console/_bootstrap.php | 2 +- apps/advanced/tests/codeception/console/codeception.yml | 2 +- .../tests/codeception/console/unit/DbTestCase.php | 4 ++-- apps/advanced/tests/codeception/console/unit/TestCase.php | 4 ++-- apps/advanced/tests/codeception/frontend/_bootstrap.php | 2 +- .../tests/codeception/frontend/_pages/AboutPage.php | 2 +- .../tests/codeception/frontend/_pages/ContactPage.php | 2 +- .../tests/codeception/frontend/_pages/SignupPage.php | 2 +- .../tests/codeception/frontend/acceptance.suite.yml | 2 +- .../tests/codeception/frontend/acceptance/AboutCept.php | 4 ++-- .../tests/codeception/frontend/acceptance/ContactCept.php | 4 ++-- .../tests/codeception/frontend/acceptance/HomeCept.php | 2 +- .../tests/codeception/frontend/acceptance/LoginCept.php | 4 ++-- .../tests/codeception/frontend/acceptance/SignupCest.php | 4 ++-- apps/advanced/tests/codeception/frontend/codeception.yml | 2 +- .../tests/codeception/frontend/functional.suite.yml | 2 +- .../tests/codeception/frontend/functional/AboutCept.php | 4 ++-- .../tests/codeception/frontend/functional/ContactCept.php | 4 ++-- .../tests/codeception/frontend/functional/HomeCept.php | 2 +- .../tests/codeception/frontend/functional/LoginCept.php | 4 ++-- .../tests/codeception/frontend/functional/SignupCest.php | 4 ++-- .../tests/codeception/frontend/unit/DbTestCase.php | 4 ++-- .../advanced/tests/codeception/frontend/unit/TestCase.php | 4 ++-- .../codeception/frontend/unit/models/ContactFormTest.php | 4 ++-- .../frontend/unit/models/PasswordResetRequestFormTest.php | 8 ++++---- .../frontend/unit/models/ResetPasswordFormTest.php | 8 ++++---- .../codeception/frontend/unit/models/SignupFormTest.php | 8 ++++---- apps/basic/tests/codeception/_bootstrap.php | 2 +- apps/basic/tests/codeception/_pages/AboutPage.php | 2 +- apps/basic/tests/codeception/_pages/ContactPage.php | 2 +- apps/basic/tests/codeception/_pages/LoginPage.php | 2 +- apps/basic/tests/codeception/acceptance/AboutCept.php | 2 +- apps/basic/tests/codeception/acceptance/ContactCept.php | 2 +- apps/basic/tests/codeception/acceptance/LoginCept.php | 2 +- apps/basic/tests/codeception/functional/AboutCept.php | 2 +- apps/basic/tests/codeception/functional/ContactCept.php | 2 +- apps/basic/tests/codeception/functional/LoginCept.php | 2 +- .../tests/codeception/unit/models/ContactFormTest.php | 2 +- .../basic/tests/codeception/unit/models/LoginFormTest.php | 2 +- apps/basic/tests/codeception/unit/models/UserTest.php | 2 +- extensions/codeception/TestCase.php | 2 +- 57 files changed, 88 insertions(+), 88 deletions(-) diff --git a/apps/advanced/tests/codeception/backend/_bootstrap.php b/apps/advanced/tests/codeception/backend/_bootstrap.php index affe4081d8..69c38520d6 100644 --- a/apps/advanced/tests/codeception/backend/_bootstrap.php +++ b/apps/advanced/tests/codeception/backend/_bootstrap.php @@ -18,4 +18,4 @@ $_SERVER['SCRIPT_FILENAME'] = YII_TEST_BACKEND_ENTRY_FILE; $_SERVER['SCRIPT_NAME'] = YII_BACKEND_TEST_ENTRY_URL; $_SERVER['SERVER_NAME'] = 'localhost'; -Yii::setAlias('@codeception', dirname(__DIR__)); +Yii::setAlias('@tests', dirname(dirname(__DIR__))); diff --git a/apps/advanced/tests/codeception/backend/acceptance.suite.yml b/apps/advanced/tests/codeception/backend/acceptance.suite.yml index 0157530a2e..f93f392e24 100644 --- a/apps/advanced/tests/codeception/backend/acceptance.suite.yml +++ b/apps/advanced/tests/codeception/backend/acceptance.suite.yml @@ -12,7 +12,7 @@ class_name: AcceptanceTester modules: enabled: - PhpBrowser - - codeception\common\_support\FixtureHelper + - tests\codeception\common\_support\FixtureHelper # you can use WebDriver instead of PhpBrowser to test javascript and ajax. # This will require you to install selenium. See http://codeception.com/docs/04-AcceptanceTests#Selenium # "restart" option is used by the WebDriver to start each time per test-file new session and cookies, diff --git a/apps/advanced/tests/codeception/backend/acceptance/LoginCept.php b/apps/advanced/tests/codeception/backend/acceptance/LoginCept.php index e86b3f3713..bb6c65f80c 100644 --- a/apps/advanced/tests/codeception/backend/acceptance/LoginCept.php +++ b/apps/advanced/tests/codeception/backend/acceptance/LoginCept.php @@ -1,7 +1,7 @@ wantTo('ensure login page works'); diff --git a/apps/advanced/tests/codeception/backend/codeception.yml b/apps/advanced/tests/codeception/backend/codeception.yml index 088fd21653..c16ac4765b 100644 --- a/apps/advanced/tests/codeception/backend/codeception.yml +++ b/apps/advanced/tests/codeception/backend/codeception.yml @@ -1,4 +1,4 @@ -namespace: codeception_backend +namespace: tests\codeception\backend actor: Tester paths: tests: . diff --git a/apps/advanced/tests/codeception/backend/functional.suite.yml b/apps/advanced/tests/codeception/backend/functional.suite.yml index 8945473581..5bf5c8b7f5 100644 --- a/apps/advanced/tests/codeception/backend/functional.suite.yml +++ b/apps/advanced/tests/codeception/backend/functional.suite.yml @@ -11,7 +11,7 @@ modules: enabled: - Filesystem - Yii2 - - codeception\common\_support\FixtureHelper + - tests\codeception\common\_support\FixtureHelper config: Yii2: configFile: '../config/backend/functional.php' diff --git a/apps/advanced/tests/codeception/backend/functional/LoginCept.php b/apps/advanced/tests/codeception/backend/functional/LoginCept.php index 4322258d88..3072d97521 100644 --- a/apps/advanced/tests/codeception/backend/functional/LoginCept.php +++ b/apps/advanced/tests/codeception/backend/functional/LoginCept.php @@ -1,7 +1,7 @@ wantTo('ensure login page works'); diff --git a/apps/advanced/tests/codeception/backend/unit/DbTestCase.php b/apps/advanced/tests/codeception/backend/unit/DbTestCase.php index 4f5a5fd352..35baef91be 100644 --- a/apps/advanced/tests/codeception/backend/unit/DbTestCase.php +++ b/apps/advanced/tests/codeception/backend/unit/DbTestCase.php @@ -4,5 +4,5 @@ namespace backend\tests\unit; class DbTestCase extends \yii\codeception\DbTestCase { - public $appConfig = '@codeception/config/backend/unit.php'; + public $appConfig = '@tests/codeception/config/backend/unit.php'; } diff --git a/apps/advanced/tests/codeception/backend/unit/TestCase.php b/apps/advanced/tests/codeception/backend/unit/TestCase.php index 5fd4fbc632..ad734d93ba 100644 --- a/apps/advanced/tests/codeception/backend/unit/TestCase.php +++ b/apps/advanced/tests/codeception/backend/unit/TestCase.php @@ -4,5 +4,5 @@ namespace backend\tests\unit; class TestCase extends \yii\codeception\TestCase { - public $appConfig = '@codeception/config/backend/unit.php'; + public $appConfig = '@tests/codeception/config/backend/unit.php'; } diff --git a/apps/advanced/tests/codeception/common/_bootstrap.php b/apps/advanced/tests/codeception/common/_bootstrap.php index b2d88d1e40..3e768208cc 100644 --- a/apps/advanced/tests/codeception/common/_bootstrap.php +++ b/apps/advanced/tests/codeception/common/_bootstrap.php @@ -11,4 +11,4 @@ require(YII_APP_BASE_PATH . '/common/config/aliases.php'); // set correct script paths $_SERVER['SERVER_NAME'] = 'localhost'; -Yii::setAlias('@codeception', dirname(__DIR__)); \ No newline at end of file +Yii::setAlias('@tests', dirname(dirname(__DIR__))); \ No newline at end of file diff --git a/apps/advanced/tests/codeception/common/_pages/LoginPage.php b/apps/advanced/tests/codeception/common/_pages/LoginPage.php index 7f93ed0633..6a1dc84337 100644 --- a/apps/advanced/tests/codeception/common/_pages/LoginPage.php +++ b/apps/advanced/tests/codeception/common/_pages/LoginPage.php @@ -1,6 +1,6 @@ [ 'class' => UserFixture::className(), - 'dataFile' => '@codeception/common/fixtures/data/init_login.php', + 'dataFile' => '@tests/codeception/common/fixtures/data/init_login.php', ], ]; } diff --git a/apps/advanced/tests/codeception/common/codeception.yml b/apps/advanced/tests/codeception/common/codeception.yml index 935e97c261..e8a3407a48 100644 --- a/apps/advanced/tests/codeception/common/codeception.yml +++ b/apps/advanced/tests/codeception/common/codeception.yml @@ -1,4 +1,4 @@ -namespace: codeception_common +namespace: tests\codeception\common actor: Tester paths: tests: . diff --git a/apps/advanced/tests/codeception/common/fixtures/UserFixture.php b/apps/advanced/tests/codeception/common/fixtures/UserFixture.php index db5e0d19a5..7153c8c0b3 100644 --- a/apps/advanced/tests/codeception/common/fixtures/UserFixture.php +++ b/apps/advanced/tests/codeception/common/fixtures/UserFixture.php @@ -1,6 +1,6 @@ [ 'class' => UserFixture::className(), - 'dataFile' => '@codeception/common/unit/fixtures/data/models/user.php' + 'dataFile' => '@tests/codeception/common/unit/fixtures/data/models/user.php' ], ]; } diff --git a/apps/advanced/tests/codeception/console/_bootstrap.php b/apps/advanced/tests/codeception/console/_bootstrap.php index b2d88d1e40..3e768208cc 100644 --- a/apps/advanced/tests/codeception/console/_bootstrap.php +++ b/apps/advanced/tests/codeception/console/_bootstrap.php @@ -11,4 +11,4 @@ require(YII_APP_BASE_PATH . '/common/config/aliases.php'); // set correct script paths $_SERVER['SERVER_NAME'] = 'localhost'; -Yii::setAlias('@codeception', dirname(__DIR__)); \ No newline at end of file +Yii::setAlias('@tests', dirname(dirname(__DIR__))); \ No newline at end of file diff --git a/apps/advanced/tests/codeception/console/codeception.yml b/apps/advanced/tests/codeception/console/codeception.yml index 41b31f6281..d1ae79465f 100644 --- a/apps/advanced/tests/codeception/console/codeception.yml +++ b/apps/advanced/tests/codeception/console/codeception.yml @@ -1,4 +1,4 @@ -namespace: codeception_console +namespace: tests\codeception\console actor: Tester paths: tests: . diff --git a/apps/advanced/tests/codeception/console/unit/DbTestCase.php b/apps/advanced/tests/codeception/console/unit/DbTestCase.php index 0d8e56e8b6..d74860dee8 100644 --- a/apps/advanced/tests/codeception/console/unit/DbTestCase.php +++ b/apps/advanced/tests/codeception/console/unit/DbTestCase.php @@ -1,11 +1,11 @@ wantTo('ensure that about works'); diff --git a/apps/advanced/tests/codeception/frontend/acceptance/ContactCept.php b/apps/advanced/tests/codeception/frontend/acceptance/ContactCept.php index c80469ee9c..c537fd4807 100644 --- a/apps/advanced/tests/codeception/frontend/acceptance/ContactCept.php +++ b/apps/advanced/tests/codeception/frontend/acceptance/ContactCept.php @@ -1,6 +1,6 @@ wantTo('ensure that contact works'); diff --git a/apps/advanced/tests/codeception/frontend/acceptance/HomeCept.php b/apps/advanced/tests/codeception/frontend/acceptance/HomeCept.php index efbcddbb86..5cf837916f 100644 --- a/apps/advanced/tests/codeception/frontend/acceptance/HomeCept.php +++ b/apps/advanced/tests/codeception/frontend/acceptance/HomeCept.php @@ -1,5 +1,5 @@ wantTo('ensure that home page works'); diff --git a/apps/advanced/tests/codeception/frontend/acceptance/LoginCept.php b/apps/advanced/tests/codeception/frontend/acceptance/LoginCept.php index cc8411d8de..e4363a837d 100644 --- a/apps/advanced/tests/codeception/frontend/acceptance/LoginCept.php +++ b/apps/advanced/tests/codeception/frontend/acceptance/LoginCept.php @@ -1,6 +1,6 @@ wantTo('ensure login page works'); diff --git a/apps/advanced/tests/codeception/frontend/acceptance/SignupCest.php b/apps/advanced/tests/codeception/frontend/acceptance/SignupCest.php index 9b86b953ab..ab4b7bba28 100644 --- a/apps/advanced/tests/codeception/frontend/acceptance/SignupCest.php +++ b/apps/advanced/tests/codeception/frontend/acceptance/SignupCest.php @@ -1,8 +1,8 @@ wantTo('ensure that about works'); diff --git a/apps/advanced/tests/codeception/frontend/functional/ContactCept.php b/apps/advanced/tests/codeception/frontend/functional/ContactCept.php index 9630b0de83..1aaac3f5a1 100644 --- a/apps/advanced/tests/codeception/frontend/functional/ContactCept.php +++ b/apps/advanced/tests/codeception/frontend/functional/ContactCept.php @@ -1,6 +1,6 @@ wantTo('ensure that contact works'); diff --git a/apps/advanced/tests/codeception/frontend/functional/HomeCept.php b/apps/advanced/tests/codeception/frontend/functional/HomeCept.php index c250e43402..0f5b2ffbb9 100644 --- a/apps/advanced/tests/codeception/frontend/functional/HomeCept.php +++ b/apps/advanced/tests/codeception/frontend/functional/HomeCept.php @@ -1,5 +1,5 @@ wantTo('ensure that home page works'); $I->amOnPage(Yii::$app->homeUrl); diff --git a/apps/advanced/tests/codeception/frontend/functional/LoginCept.php b/apps/advanced/tests/codeception/frontend/functional/LoginCept.php index c5d94afa59..0fb3477614 100644 --- a/apps/advanced/tests/codeception/frontend/functional/LoginCept.php +++ b/apps/advanced/tests/codeception/frontend/functional/LoginCept.php @@ -1,6 +1,6 @@ wantTo('ensure login page works'); diff --git a/apps/advanced/tests/codeception/frontend/functional/SignupCest.php b/apps/advanced/tests/codeception/frontend/functional/SignupCest.php index 0ed81b7a22..525b0373fa 100644 --- a/apps/advanced/tests/codeception/frontend/functional/SignupCest.php +++ b/apps/advanced/tests/codeception/frontend/functional/SignupCest.php @@ -1,8 +1,8 @@ [ 'class' => UserFixture::className(), - 'dataFile' => '@codeception/frontend/unit/fixtures/data/models/user.php' + 'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php' ], ]; } diff --git a/apps/advanced/tests/codeception/frontend/unit/models/ResetPasswordFormTest.php b/apps/advanced/tests/codeception/frontend/unit/models/ResetPasswordFormTest.php index 1997b2f4f7..a2f5012400 100644 --- a/apps/advanced/tests/codeception/frontend/unit/models/ResetPasswordFormTest.php +++ b/apps/advanced/tests/codeception/frontend/unit/models/ResetPasswordFormTest.php @@ -1,9 +1,9 @@ [ 'class' => UserFixture::className(), - 'dataFile' => '@codeception/frontend/unit/fixtures/data/models/user.php' + 'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php' ], ]; } diff --git a/apps/advanced/tests/codeception/frontend/unit/models/SignupFormTest.php b/apps/advanced/tests/codeception/frontend/unit/models/SignupFormTest.php index ff8b56d288..4d869f1122 100644 --- a/apps/advanced/tests/codeception/frontend/unit/models/SignupFormTest.php +++ b/apps/advanced/tests/codeception/frontend/unit/models/SignupFormTest.php @@ -1,9 +1,9 @@ [ 'class' => UserFixture::className(), - 'dataFile' => '@codeception/frontend/unit/fixtures/data/models/user.php', + 'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php', ], ]; } diff --git a/apps/basic/tests/codeception/_bootstrap.php b/apps/basic/tests/codeception/_bootstrap.php index 5f5ae10b42..9855939782 100644 --- a/apps/basic/tests/codeception/_bootstrap.php +++ b/apps/basic/tests/codeception/_bootstrap.php @@ -12,4 +12,4 @@ $_SERVER['SCRIPT_FILENAME'] = YII_TEST_ENTRY_FILE; $_SERVER['SCRIPT_NAME'] = YII_TEST_ENTRY_URL; $_SERVER['SERVER_NAME'] = 'localhost'; -Yii::setAlias('@codeception', __DIR__); +Yii::setAlias('@tests', dirname(__DIR__)); diff --git a/apps/basic/tests/codeception/_pages/AboutPage.php b/apps/basic/tests/codeception/_pages/AboutPage.php index b6ca34a293..b56320d093 100644 --- a/apps/basic/tests/codeception/_pages/AboutPage.php +++ b/apps/basic/tests/codeception/_pages/AboutPage.php @@ -1,6 +1,6 @@ wantTo('ensure that about works'); diff --git a/apps/basic/tests/codeception/acceptance/ContactCept.php b/apps/basic/tests/codeception/acceptance/ContactCept.php index c9adc7e15e..98ba2ef12c 100644 --- a/apps/basic/tests/codeception/acceptance/ContactCept.php +++ b/apps/basic/tests/codeception/acceptance/ContactCept.php @@ -1,6 +1,6 @@ wantTo('ensure that contact works'); diff --git a/apps/basic/tests/codeception/acceptance/LoginCept.php b/apps/basic/tests/codeception/acceptance/LoginCept.php index 6d07c08958..4f022e1c28 100644 --- a/apps/basic/tests/codeception/acceptance/LoginCept.php +++ b/apps/basic/tests/codeception/acceptance/LoginCept.php @@ -1,6 +1,6 @@ wantTo('ensure that login works'); diff --git a/apps/basic/tests/codeception/functional/AboutCept.php b/apps/basic/tests/codeception/functional/AboutCept.php index 3edd61da31..ae7b45b308 100644 --- a/apps/basic/tests/codeception/functional/AboutCept.php +++ b/apps/basic/tests/codeception/functional/AboutCept.php @@ -1,6 +1,6 @@ wantTo('ensure that about works'); diff --git a/apps/basic/tests/codeception/functional/ContactCept.php b/apps/basic/tests/codeception/functional/ContactCept.php index c5e83b4f19..47604058d7 100644 --- a/apps/basic/tests/codeception/functional/ContactCept.php +++ b/apps/basic/tests/codeception/functional/ContactCept.php @@ -1,6 +1,6 @@ wantTo('ensure that contact works'); diff --git a/apps/basic/tests/codeception/functional/LoginCept.php b/apps/basic/tests/codeception/functional/LoginCept.php index 4cb283ac97..991d27c3c9 100644 --- a/apps/basic/tests/codeception/functional/LoginCept.php +++ b/apps/basic/tests/codeception/functional/LoginCept.php @@ -1,6 +1,6 @@ wantTo('ensure that login works'); diff --git a/apps/basic/tests/codeception/unit/models/ContactFormTest.php b/apps/basic/tests/codeception/unit/models/ContactFormTest.php index 8adda18c61..3234829432 100644 --- a/apps/basic/tests/codeception/unit/models/ContactFormTest.php +++ b/apps/basic/tests/codeception/unit/models/ContactFormTest.php @@ -1,6 +1,6 @@ Date: Sun, 24 Aug 2014 15:11:06 +0400 Subject: [PATCH 15/16] Added description to each test config of advanced application --- apps/advanced/tests/codeception/config/backend/acceptance.php | 4 ++++ apps/advanced/tests/codeception/config/backend/config.php | 3 +++ apps/advanced/tests/codeception/config/backend/functional.php | 3 +++ apps/advanced/tests/codeception/config/backend/unit.php | 3 +++ apps/advanced/tests/codeception/config/console/unit.php | 4 +++- .../advanced/tests/codeception/config/frontend/acceptance.php | 4 ++++ apps/advanced/tests/codeception/config/frontend/config.php | 3 +++ .../advanced/tests/codeception/config/frontend/functional.php | 3 +++ apps/advanced/tests/codeception/config/frontend/unit.php | 3 +++ 9 files changed, 29 insertions(+), 1 deletion(-) diff --git a/apps/advanced/tests/codeception/config/backend/acceptance.php b/apps/advanced/tests/codeception/config/backend/acceptance.php index bf9204c8f7..ce673f8ca2 100644 --- a/apps/advanced/tests/codeception/config/backend/acceptance.php +++ b/apps/advanced/tests/codeception/config/backend/acceptance.php @@ -1,5 +1,9 @@ Date: Sun, 24 Aug 2014 15:26:57 +0400 Subject: [PATCH 16/16] Added faker to basic app console commands, adjusted readme to mention database setup --- apps/basic/tests/README.md | 26 ++++++++++++++++--- .../tests/codeception/bin/yii_acceptance | 7 +++++ .../tests/codeception/bin/yii_functional | 7 +++++ apps/basic/tests/codeception/bin/yii_unit | 7 +++++ .../tests/codeception/fixtures/.gitignore | 2 ++ .../tests/codeception/templates/.gitignore | 2 ++ 6 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 apps/basic/tests/codeception/fixtures/.gitignore create mode 100644 apps/basic/tests/codeception/templates/.gitignore diff --git a/apps/basic/tests/README.md b/apps/basic/tests/README.md index 1c4b50bcac..34439f8a27 100644 --- a/apps/basic/tests/README.md +++ b/apps/basic/tests/README.md @@ -21,20 +21,40 @@ Changed current directory to Then add `/vendor/bin` to you `PATH` environment variable. Now we're able to use `codecept` from command line globally. -2. Build the test suites: +2. Install faker extension by running the following from template root directory where `composer.json` is: + +``` +composer require --dev yiisoft/yii2-faker:* +``` + +3. Create three databases that are used in tests: + +* `yii2_basic_unit` - for unit tests; +* `yii2_basic_functional` - for functional tests; +* `yii2_basic_acceptance` - for acceptance tests. + +Then update databases by applying migrations: + +``` +codeception/bin/yii_acceptance migrate +codeception/bin/yii_functional migrate +codeception/bin/yii_unit migrate +``` + +4. Build the test suites: ``` codecept build ``` -3. In order to be able to run acceptance tests you need to start a webserver. The simplest way is to use PHP built in +5. In order to be able to run acceptance tests you need to start a webserver. The simplest way is to use PHP built in webserver. In the `web` directory execute the following: ``` php -S localhost:8080 ``` -4. Now you can run the tests with the following commands: +6. Now you can run the tests with the following commands: ``` # run all available tests diff --git a/apps/basic/tests/codeception/bin/yii_acceptance b/apps/basic/tests/codeception/bin/yii_acceptance index f67a47fbbb..99ac417472 100644 --- a/apps/basic/tests/codeception/bin/yii_acceptance +++ b/apps/basic/tests/codeception/bin/yii_acceptance @@ -14,6 +14,13 @@ $config = yii\helpers\ArrayHelper::merge( require(YII_APP_BASE_PATH . '/config/console.php'), require(__DIR__ . '/../config/config.php'), [ + 'controllerMap' => [ + 'fixture' => [ + 'class' => 'yii\faker\FixtureController', + 'fixtureDataPath' => dirname(__DIR__) . 'fixtures', + 'templatePath' => dirname(__DIR__) . 'templates' + ], + ], 'components' => [ 'db' => [ 'dsn' => 'mysql:host=localhost;dbname=yii2_basic_acceptance', diff --git a/apps/basic/tests/codeception/bin/yii_functional b/apps/basic/tests/codeception/bin/yii_functional index f6e3988a06..39efa983b2 100644 --- a/apps/basic/tests/codeception/bin/yii_functional +++ b/apps/basic/tests/codeception/bin/yii_functional @@ -14,6 +14,13 @@ $config = yii\helpers\ArrayHelper::merge( require(YII_APP_BASE_PATH . '/config/console.php'), require(__DIR__ . '/../config/config.php'), [ + 'controllerMap' => [ + 'fixture' => [ + 'class' => 'yii\faker\FixtureController', + 'fixtureDataPath' => dirname(__DIR__) . 'fixtures', + 'templatePath' => dirname(__DIR__) . 'templates' + ], + ], 'components' => [ 'db' => [ 'dsn' => 'mysql:host=localhost;dbname=yii2_basic_functional', diff --git a/apps/basic/tests/codeception/bin/yii_unit b/apps/basic/tests/codeception/bin/yii_unit index 1ab8e1fbae..f99ec5333f 100644 --- a/apps/basic/tests/codeception/bin/yii_unit +++ b/apps/basic/tests/codeception/bin/yii_unit @@ -14,6 +14,13 @@ $config = yii\helpers\ArrayHelper::merge( require(YII_APP_BASE_PATH . '/config/console.php'), require(__DIR__ . '/../config/config.php'), [ + 'controllerMap' => [ + 'fixture' => [ + 'class' => 'yii\faker\FixtureController', + 'fixtureDataPath' => dirname(__DIR__) . 'fixtures', + 'templatePath' => dirname(__DIR__) . 'templates' + ], + ], 'components' => [ 'db' => [ 'dsn' => 'mysql:host=localhost;dbname=yii2_basic_unit', diff --git a/apps/basic/tests/codeception/fixtures/.gitignore b/apps/basic/tests/codeception/fixtures/.gitignore new file mode 100644 index 0000000000..c96a04f008 --- /dev/null +++ b/apps/basic/tests/codeception/fixtures/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/apps/basic/tests/codeception/templates/.gitignore b/apps/basic/tests/codeception/templates/.gitignore new file mode 100644 index 0000000000..c96a04f008 --- /dev/null +++ b/apps/basic/tests/codeception/templates/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file