This is an old revision of the document!
Yii 2.0 Testing Examples
Saving to Database
Check records in a table using any of these:
$this->tester->seeInDatabase('price', ['item_code' => 'TABLE_BLACK']); $this->tester->seeRecord( 'app\models\Price', ['description' => 'Black Table']); $this->tester->dontSeeRecord('app\models\Price', ['description' => 'Red Table']);
Example:
<?php namespace models; use app\models\Price; class PriceTest extends \Codeception\Test\Unit { use \Codeception\Specify; private $model; /** * @var \UnitTester */ protected $tester; protected function _before() { Price::deleteAll(['description' => 'Octane BTE 4']); Price::deleteAll(['description' => 'Octane 4']); $this->model = new Price(); $this->model->item_code = 'BTEO4'; $this->model->description = 'Octane BTE 4'; $this->model->cost = 100.00; $this->model->msrp = 1500.00; $this->model->price = 150.00; $this->model->price_volume = 140.00; $this->model->warranty_1yr = '20'; $this->model->warranty_2yr = '40'; $this->model->warranty_included = ''; $this->model->type = 'Product'; $this->model->category = 'BTE'; } protected function _after() { } public function testSavingPrice() { $this->model->save(); // Verify data was saved using framework methods $this->tester->seeInDatabase('price', ['item_code' => 'BTEO4', 'description' => 'Octane BTE 4', 'cost' => 100.00]); //$this->tester->seeInDatabase('price', ['item_code' => 'BTEO4']); $this->tester->seeRecord( 'app\models\Price', ['description' => 'Octane BTE 4']); // Checks that record exists in database $this->tester->dontSeeRecord('app\models\Price', ['description' => 'Octane 4']); // Checks that record does not exist in database } //... }
Comments
$I->wantTo('query the customer info using his phone number'); $I->expectTo('see query result');
Users
// Login $I->amLoggedInAs(\app\models\User::findByUsername('admin')); $I->amOnPage(['site/index']); $I->see('Logout'); // Logout \Yii::$app->user->logout(); $I->amOnPage(['site/index']); $I->see('Signup'); $I->see('Login');
Forms
public function signupNewUser(\FunctionalTester $I) { $faker = Faker\Factory::create(); $username = $faker->userName; $I->expectTo('Sign up new user'); \Yii::$app->user->logout(); $I->amOnPage(['site/index']); $I->see('Signup'); $I->click('Signup'); $I->seeCurrentUrlMatches('/user(.+)signup/'); $I->see('Signup', 'h1'); $I->fillField(['name' => 'SignupForm[username]'], $username); $I->fillField(['name' => 'SignupForm[password]'], 'testing'); $I->fillField(['name' => 'SignupForm[first_name]'], $faker->firstName); $I->fillField(['name' => 'SignupForm[last_name]'], $faker->lastName); $I->fillField(['name' => 'SignupForm[email]'], $faker->email); $I->fillField(['name' => 'SignupForm[phone]'], $faker->phoneNumber); $I->click('Signup', 'button'); // button $I->seeCurrentUrlMatches('/(.+)index(.+)/'); $I->see("Logout ({$username})"); // user $I->dontSee('Sign up as a user to get exclusive access to information.'); $I->dontSee('Cannot be blank.'); $I->dontSee('has already been taken.'); $I->dontSeeCurrentUrlMatches('/user(.+)signup/'); // logout \Yii::$app->user->logout(); $I->amOnPage(['site/index']); $I->see('Signup'); $I->see('Login'); }