From ba52dcd892e46f8d15a339be3267de67fbfe5002 Mon Sep 17 00:00:00 2001 From: ostashev Date: Tue, 31 May 2016 17:57:03 +0300 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20(#7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/README.md | 127 ++++++++++++++++++ tests/codeception.yml | 36 +++++ tests/codeception/.gitignore | 4 + tests/codeception/_bootstrap.php | 16 +++ tests/codeception/_output/.gitignore | 2 + tests/codeception/_pages/ContactPage.php | 26 ++++ tests/codeception/acceptance.suite.yml | 27 ++++ tests/codeception/acceptance/ContactCept.php | 57 ++++++++ tests/codeception/acceptance/_bootstrap.php | 2 + tests/codeception/bin/_bootstrap.php | 10 ++ tests/codeception/bin/yii | 20 +++ tests/codeception/bin/yii.bat | 20 +++ tests/codeception/config/acceptance.php | 11 ++ tests/codeception/config/config.php | 26 ++++ tests/codeception/config/functional.php | 25 ++++ tests/codeception/config/unit.php | 11 ++ tests/codeception/fixtures/.gitignore | 2 + tests/codeception/functional.suite.yml | 16 +++ tests/codeception/functional/_bootstrap.php | 2 + tests/codeception/templates/.gitignore | 2 + tests/codeception/unit.suite.yml | 6 + tests/codeception/unit/_bootstrap.php | 3 + tests/codeception/unit/fixtures/.gitkeep | 0 tests/codeception/unit/fixtures/data/.gitkeep | 0 tests/codeception/unit/models/UserTest.php | 17 +++ .../unit/templates/fixtures/.gitkeep | 0 web/index-test.php | 16 +++ 27 files changed, 484 insertions(+) create mode 100644 tests/README.md create mode 100644 tests/codeception.yml create mode 100644 tests/codeception/.gitignore create mode 100644 tests/codeception/_bootstrap.php create mode 100644 tests/codeception/_output/.gitignore create mode 100644 tests/codeception/_pages/ContactPage.php create mode 100644 tests/codeception/acceptance.suite.yml create mode 100644 tests/codeception/acceptance/ContactCept.php create mode 100644 tests/codeception/acceptance/_bootstrap.php create mode 100644 tests/codeception/bin/_bootstrap.php create mode 100644 tests/codeception/bin/yii create mode 100644 tests/codeception/bin/yii.bat create mode 100644 tests/codeception/config/acceptance.php create mode 100644 tests/codeception/config/config.php create mode 100644 tests/codeception/config/functional.php create mode 100644 tests/codeception/config/unit.php create mode 100644 tests/codeception/fixtures/.gitignore create mode 100644 tests/codeception/functional.suite.yml create mode 100644 tests/codeception/functional/_bootstrap.php create mode 100644 tests/codeception/templates/.gitignore create mode 100644 tests/codeception/unit.suite.yml create mode 100644 tests/codeception/unit/_bootstrap.php create mode 100644 tests/codeception/unit/fixtures/.gitkeep create mode 100644 tests/codeception/unit/fixtures/data/.gitkeep create mode 100644 tests/codeception/unit/models/UserTest.php create mode 100644 tests/codeception/unit/templates/fixtures/.gitkeep create mode 100644 web/index-test.php diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..fd80fe5 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,127 @@ +This directory contains various tests for the basic application. + +Tests in `codeception` directory are developed with [Codeception PHP Testing Framework](http://codeception.com/). + +After creating the basic application, follow these steps to prepare for the tests: + +1. Install Codeception if it's not yet installed: + + ``` + 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. + +2. Install faker extension by running the following from template root directory where `composer.json` is: + + ``` + composer require --dev "yiisoft/yii2-faker:*" + ``` + +3. Create `yii2_basic_tests` database and update it by applying migrations (you may skip this step if you do not have created any migrations yet): + + ``` + codeception/bin/yii migrate + ``` + + The command needs to be run in the `tests` directory. + The database configuration can be found at `tests/codeception/config/config.php`. + +4. Build the test suites: + + ``` + codecept build + ``` + +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 + ``` + +6. Now you can run the tests with the following commands: + + ``` + # run all available tests + codecept run + # run acceptance tests + codecept run acceptance + # run functional tests + codecept run functional + # run unit tests + codecept run unit + ``` + +Fixtures Default Configuration +------------------------------ +The `fixture` commands refer to the following `ActiveFixture` configuration by default: + +- Fixtures path: `@tests/unit/fixtures` +- Fixtures data path: `@tests/unit/fixtures/data` +- Template files path: `@tests/unit/templates/fixtures` +- Namespace: `tests\unit\fixtures` + +Where `@tests` refers to `@app/tests/codeception`. + +Code coverage support +--------------------- + +By default, code coverage is disabled in `codeception.yml` configuration file, you should uncomment needed rows to be able +to collect code coverage. You can run your tests and collect coverage with the following command: + +``` +#collect coverage for all tests +codecept run --coverage-html --coverage-xml + +#collect coverage only for unit tests +codecept run unit --coverage-html --coverage-xml + +#collect coverage for unit and functional tests +codecept run functional,unit --coverage-html --coverage-xml +``` + +You can see code coverage output under the `tests/_output` directory. + +###Remote code coverage + +When you run your tests not in the same process where code coverage is collected, then you should uncomment `remote` option and its +related options, to be able to collect code coverage correctly. To setup remote code coverage you should follow [instructions](http://codeception.com/docs/11-Codecoverage) +from codeception site. + +1. install `Codeception c3` remote support `composer require "codeception/c3:*"`; + +2. copy `c3.php` file under your `web` directory; + +3. include `c3.php` file in your `index-test.php` file before application run, so it can catch needed requests. + +4. edit `c3.php` to update config file path (~ line 55) with `$config_file = realpath(__DIR__ . '/../tests/codeception.yml');` + +Configuration options that are used by remote code coverage: + +- c3_url: url pointing to entry script that includes `c3.php` file, so `Codeception` will be able to produce code coverage; +- remote: whether to enable remote code coverage or not; +- remote_config: path to the `codeception.yml` configuration file, from the directory where `c3.php` file is located. This is needed + so that `Codeception` can create itself instance and collect code coverage correctly. + +By default `c3_url` and `remote_config` setup correctly, you only need to copy and include `c3.php` file in your `index-test.php` + +After that you should be able to collect code coverage from tests that run through `PhpBrowser` or `WebDriver` with same command +as for other tests: + +``` +#collect coverage from remote +codecept run acceptance --coverage-html --coverage-xml +``` + +Please refer to [Codeception tutorial](http://codeception.com/docs/01-Introduction) for +more details about writing and running acceptance, functional and unit tests. diff --git a/tests/codeception.yml b/tests/codeception.yml new file mode 100644 index 0000000..b71ba31 --- /dev/null +++ b/tests/codeception.yml @@ -0,0 +1,36 @@ +actor: Tester +#coverage: +# #c3_url: http://localhost:8080/index-test.php/ +# enabled: true +# #remote: true +# #remote_config: '../tests/codeception.yml' +# white_list: +# include: +# - ../models/* +# - ../controllers/* +# - ../commands/* +# - ../mail/* +# blacklist: +# include: +# - ../assets/* +# - ../config/* +# - ../runtime/* +# - ../vendor/* +# - ../views/* +# - ../web/* +# - ../tests/* +paths: + tests: codeception + log: codeception/_output + data: codeception/_data + helpers: codeception/_support +settings: + bootstrap: _bootstrap.php + suite_class: \PHPUnit_Framework_TestSuite + memory_limit: 1024M + log: true + colors: true +config: + # the entry script URL (with host info) for functional and acceptance tests + # PLEASE ADJUST IT TO THE ACTUAL ENTRY SCRIPT URL + test_entry_url: http://localhost:8080/index-test.php \ No newline at end of file diff --git a/tests/codeception/.gitignore b/tests/codeception/.gitignore new file mode 100644 index 0000000..985dbb4 --- /dev/null +++ b/tests/codeception/.gitignore @@ -0,0 +1,4 @@ +# these files are auto generated by codeception build +/unit/UnitTester.php +/functional/FunctionalTester.php +/acceptance/AcceptanceTester.php diff --git a/tests/codeception/_bootstrap.php b/tests/codeception/_bootstrap.php new file mode 100644 index 0000000..755029e --- /dev/null +++ b/tests/codeception/_bootstrap.php @@ -0,0 +1,16 @@ + $value) { + $inputType = $field === 'body' ? 'textarea' : 'input'; + $this->actor->fillField($inputType . '[name="ContactForm[' . $field . ']"]', $value); + } + $this->actor->click('contact-button'); + } +} diff --git a/tests/codeception/acceptance.suite.yml b/tests/codeception/acceptance.suite.yml new file mode 100644 index 0000000..d36d736 --- /dev/null +++ b/tests/codeception/acceptance.suite.yml @@ -0,0 +1,27 @@ +# Codeception Test Suite Configuration + +# suite for acceptance tests. +# perform tests in browser using the Selenium-like tools. +# powered by Mink (http://mink.behat.org). +# (tip: that's what your customer will see). +# (tip: test your ajax and javascript by one of Mink drivers). + +# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES. + +class_name: AcceptanceTester +modules: + enabled: + - PhpBrowser +# you can use WebDriver instead of PhpBrowser to test javascript and ajax. +# This will require you to install selenium. See http://codeception.com/docs/03-AcceptanceTests#selenium-webdriver +# "restart" option is used by the WebDriver to start each time per test-file new session and cookies, +# it is useful if you want to login in your app in each test. +# - WebDriver + config: + PhpBrowser: +# PLEASE ADJUST IT TO THE ACTUAL ENTRY POINT WITHOUT PATH INFO + url: http://localhost:8080 +# WebDriver: +# url: http://localhost:8080 +# browser: firefox +# restart: true diff --git a/tests/codeception/acceptance/ContactCept.php b/tests/codeception/acceptance/ContactCept.php new file mode 100644 index 0000000..f848443 --- /dev/null +++ b/tests/codeception/acceptance/ContactCept.php @@ -0,0 +1,57 @@ +wantTo('ensure that contact works'); + +$contactPage = ContactPage::openBy($I); + +$I->see('Contact', 'h1'); + +$I->amGoingTo('submit contact form with no data'); +$contactPage->submit([]); +if (method_exists($I, 'wait')) { + $I->wait(3); // only for selenium +} +$I->expectTo('see validations errors'); +$I->see('Contact', 'h1'); +$I->see('Name cannot be blank'); +$I->see('Email cannot be blank'); +$I->see('Subject cannot be blank'); +$I->see('Body cannot be blank'); +$I->see('The verification code is incorrect'); + +$I->amGoingTo('submit contact form with not correct email'); +$contactPage->submit([ + 'name' => 'tester', + 'email' => 'tester.email', + 'subject' => 'test subject', + 'body' => 'test content', + 'verifyCode' => 'testme', +]); +if (method_exists($I, 'wait')) { + $I->wait(3); // only for selenium +} +$I->expectTo('see that email address is wrong'); +$I->dontSee('Name cannot be blank', '.help-inline'); +$I->see('Email is not a valid email address.'); +$I->dontSee('Subject cannot be blank', '.help-inline'); +$I->dontSee('Body cannot be blank', '.help-inline'); +$I->dontSee('The verification code is incorrect', '.help-inline'); + +$I->amGoingTo('submit contact form with correct data'); +$contactPage->submit([ + 'name' => 'tester', + 'email' => 'tester@example.com', + 'subject' => 'test subject', + 'body' => 'test content', + 'verifyCode' => 'testme', +]); +if (method_exists($I, 'wait')) { + $I->wait(3); // only for selenium +} +$I->dontSeeElement('#contact-form'); +$I->see('Thank you for contacting us. We will respond to you as soon as possible.'); diff --git a/tests/codeception/acceptance/_bootstrap.php b/tests/codeception/acceptance/_bootstrap.php new file mode 100644 index 0000000..36f9f1d --- /dev/null +++ b/tests/codeception/acceptance/_bootstrap.php @@ -0,0 +1,2 @@ +run(); +exit($exitCode); diff --git a/tests/codeception/bin/yii.bat b/tests/codeception/bin/yii.bat new file mode 100644 index 0000000..d516b3a --- /dev/null +++ b/tests/codeception/bin/yii.bat @@ -0,0 +1,20 @@ +@echo off + +rem ------------------------------------------------------------- +rem Yii command line bootstrap script for Windows. +rem +rem @author Qiang Xue +rem @link http://www.yiiframework.com/ +rem @copyright Copyright (c) 2008 Yii Software LLC +rem @license http://www.yiiframework.com/license/ +rem ------------------------------------------------------------- + +@setlocal + +set YII_PATH=%~dp0 + +if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe + +"%PHP_COMMAND%" "%YII_PATH%yii" %* + +@endlocal diff --git a/tests/codeception/config/acceptance.php b/tests/codeception/config/acceptance.php new file mode 100644 index 0000000..c688575 --- /dev/null +++ b/tests/codeception/config/acceptance.php @@ -0,0 +1,11 @@ + 'en-US', + 'controllerMap' => [ + 'fixture' => [ + 'class' => 'yii\faker\FixtureController', + 'fixtureDataPath' => '@tests/codeception/fixtures', + 'templatePath' => '@tests/codeception/templates', + 'namespace' => 'tests\codeception\fixtures', + ], + ], + 'components' => [ + 'db' => [ + 'dsn' => 'mysql:host=localhost;dbname=yii2_basic_tests', + ], + 'mailer' => [ + 'useFileTransport' => true, + ], + 'urlManager' => [ + 'showScriptName' => true, + ], + ], +]; diff --git a/tests/codeception/config/functional.php b/tests/codeception/config/functional.php new file mode 100644 index 0000000..6d22bd9 --- /dev/null +++ b/tests/codeception/config/functional.php @@ -0,0 +1,25 @@ + [ + 'request' => [ + // it's not recommended to run functional tests with CSRF validation enabled + 'enableCsrfValidation' => false, + // but if you absolutely need it set cookie domain to localhost + /* + 'csrfCookie' => [ + 'domain' => 'localhost', + ], + */ + ], + ], + ] +); diff --git a/tests/codeception/config/unit.php b/tests/codeception/config/unit.php new file mode 100644 index 0000000..5bab5ea --- /dev/null +++ b/tests/codeception/config/unit.php @@ -0,0 +1,11 @@ +loadFixtures(['user']); + } + + // TODO add test methods here +} diff --git a/tests/codeception/unit/templates/fixtures/.gitkeep b/tests/codeception/unit/templates/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/web/index-test.php b/web/index-test.php new file mode 100644 index 0000000..32b4ce3 --- /dev/null +++ b/web/index-test.php @@ -0,0 +1,16 @@ +run();