<?php
use \Codeception\Util\Stub;
class UserTest extends \Codeception\TestCase\Test
{
public function testUserSave() {
$user = Stub::make('User');
$user->setName('davert');
$this->assertEquals('davert', $user->getName());
$user->save();
$this->tester->seeInDatabase('users', array('name' => 'davert'));
}
}
?>
Unit
<?php
$I = new ApiTester($scenario);
$I->wantTo('create a new user by API');
$I->amHttpAuthenticated('davert','123456');
$I->haveHttpHeader('Content-Type','application/x-www-form-urlencoded');
$I->sendPOST('/users', array('name' => 'davert' ));
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(array('result' => 'ok'));
?>
API testing
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('create wiki page');
$I->amOnPage('/');
$I->click('Pages');
$I->click('New');
$I->see('New Page');
$I->fillField('title', 'Hobbit');
$I->fillField('body', 'By Peter Jackson');
$I->click('Save');
$I->see('page created'); // notice generated
$I->see('Hobbit','h1'); // head of page of is our title
$I->seeInCurrentUrl('pages/hobbit');
$I->seeInDatabase('pages', array('title' => 'Hobbit'));
?>
Acceptance testing
<?php $scenario->group(['profile', 'address']); $I = new AcceptanceTester($scenario); $I->wantTo('Add new post address'); $I->login($I); $I->amOnPage('/user/addressbook'); $uid = $I->grabFromCurrentUrl('@/user/(\d+)/index/useraddress@si'); $I->amOnPage('/user/' . $uid . '/index/useraddress/create'); $I->seeCurrentUrlEquals('/user/' . $uid . '/index/useraddress/create'); $address = $I->get_address_fields_selectors_id(); $I->selectOption($address['region'], 'Архангельская область'); $I->fillField($address['postal_code'], '414009'); $I->fillField($address['city'], 'Астрахань'); $I->fillField($address['street'], 'Таганрогская'); $I->fillField($address['house'], '20'); $I->fillField($address['flat'], '150'); $I->click('Сохранить профиль'); $I->seeCurrentUrlEquals('/user/' . $uid . '/index/useraddress'); $I->see('Профиль сохранен'); $I->see('414009, Россия, Астрахань, Таганрогская, 20, 150');
Real Code
composer require "codeception/codeception:*"
drush composer-manager require "codeception/codeception:*"
Install
Check version (2.1+)
vendor/bin/codecept --version
codecept bootstrap
vim codeception.yml
vim tests/selenium.suite.yml #set url
Init
Download & run Selenium
cd vendor/bin
wget http://selenium-release.storage.googleapis.com/2.46/selenium-server-standalone-2.46.0.jar
java -jar selenium-server-standalone-2.46.0.jar
Edit tests/selenium.suite.yml
Run!
codecept run
codecept run --steps
codecept run -g profile
codecept run tests/selenium/ProfileAddressAddCept.php
#instead java -jar selenium-server-standalone-2.46.0.jar #use phantomjs --webdriver=4444
# 1.x WebGuy, TestGuy, CodeGuy # 2.x AcceptanceTester, FunctionalTester, UnitTester