Acceptance test with Codeception, Laravel ORM, Faker in Window
Let's say we have a website located at http://localhost:8080/
Unzip and put it somewhere you want, then add that folder path to system PATH like this:
Now you can check php version by type
Open
Init Codeception:
To run codeception with real browser, for example with chrome we need to download selenium and chrome driver.
Download selenium from http://docs.seleniumhq.org/download/ (jar file) and put inside
Download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads (exe file) and put it to anywhere then set system PATH like to do with PHP.
Open
Create factory file:
Define factory:
Write test:
Start selenium server:
1. Install composer
Download and install composer for window https://getcomposer.org/download/.2. Install php
If you already have php installed, go to next step.
To check if you already have php installed, open cmd and type php -v. If you have php installed, php version will show up.
Download php zip file from http://windows.php.net/download.
Unzip and put it somewhere you want, then add that folder path to system PATH like this:
Now you can check php version by type
php -v in cmd.
3. Install laravel
Open cmd and go to folder that you want to put your test code to.Tips: open folder by Window Explorer then type "cmd" in address bar, cmd will open and point to current folder.Create project and install laravel by type:
composer create-project --prefer-dist laravel/laravel your-project-testAnd cd to
your-project-test folder in cmd.
4. Config database
If you using MySQL, make sure to enablepdo_mysql by uncomment extension=pdo_mysql line in php.ini file inside your php folder.
Open
.env file and config your sql infomation, example:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME=root DB_PASSWORD=123456
5. Generate models from your existing database
Installreliese/laravel via composer:
composer require reliese/laravel --devPublish config file and clear cache:
php artisan vendor:publish --tag=reliese-models php artisan config:clear
This will avoid "mkdir(): Invalid path" errorOpen
app/Providers/AppServiceProvider.php and register provider:
public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register(\Reliese\Coders\CodersServiceProvider::class);
}
}
Run this command to generate models:
php artisan code:modelsModels will be generated in
app/Models folder.
If you still got "mkdir(): Invalid path" error, try runing these command again and check ifconfig/models.phpfile exists:php artisan vendor:publish --tag=reliese-models # this command will create config/models.php file php artisan config:clear
6. Install Codeception
Download codecept.phar file from http://codeception.com/quickstart and put intoyour-project-test folder.
Init Codeception:
php codecept.phar bootstrapAll Codeception files generated in
your-project-test/tests/ folder.
By default laravel already has a tests folder, if you do not use laravel default test file, delete tests folder before init codeception.
7. Config Codeception
Codeception use PHPBrowser by default, it very fast but does not havewait function to wait for javascript actions. So it better to use real browser.
To run codeception with real browser, for example with chrome we need to download selenium and chrome driver.
Download selenium from http://docs.seleniumhq.org/download/ (jar file) and put inside
your-project-test.
Download chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads (exe file) and put it to anywhere then set system PATH like to do with PHP.
Open
test/acceptance.suite.yml file and add config:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://localhost:8080/ # your website url
browser: chrome
- \Helper\Acceptance
- Laravel5: # this will load all laravel feature like: helper function (ex. dd()), Eloquent, factories ...
part: ORM
cleanup: false # can't wrap into transaction
Need to rebuild codeception after change config file:
php codecept.phar build
8. Define factories
For example with classapp/Models/User.php:
Create factory file:
php artisan make:factory UserFactoryUserFactory.php file will be created in
database/factories/ folder.
Define factory:
<?php
use Faker\Generator as Faker;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
];
});
Laravel auto created UserFactory by default.
9. Create acceptance test
php codecept.phar generate:cest acceptance Login
test/acceptance/LoginCest.php file will be created.
Write test:
<?php
use App\User;
class LoginCest
{
public function _before(AcceptanceTester $I)
{
}
public function _after(AcceptanceTester $I)
{
}
// tests
public function tryToTest(AcceptanceTester $I)
{
// use Eloquent example
$user = User::first();
// use factories example
$user = factory(User::class)->make(); // make user object
$user = factory(User::class)->create(); // make user object and insert to database
// acceptance test example
$I->amOnPage('/login');
$I->fillField('username', 'davert');
$I->fillField('password', 'qwerty');
$I->click('LOGIN');
$I->see('Welcome, Davert!');
}
}
10. Run
Open cmd (point to your-project-test folder).Start selenium server:
java -jar selenium-server-standalone-3.8.1.jarOpen other cmd and run:
php codecept.phar run