Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
systems:yii2:testing_examples [2018/04/23 08:41]
smayr [Users]
systems:yii2:testing_examples [2018/04/24 15:44] (current)
smayr [Forms]
Line 2: Line 2:
  
 == Saving to Database == == Saving to Database ==
 +Add records to a table using any of these:
 +<code php>
 +$tblUser = Yii::$app->db->schema->getRawTableName(app\models\User::tableName());
 +$this->tester->haveInDatabase($tblUser, [
 +    'username'      => 'test',
 +    'email'         => 'jdoe@example.com',
 +    'phone'         => '407-555-4444',
 +    'first_name'    => 'John',
 +    'last_name'     => 'Doe',
 +    'password_hash' => '$2y$13$GU0atvXcHvtszSoOgW3o/OB1WGSYh244IgAjMVDi16wzb8M0l5paS',
 +    'auth_key'      => '0d5QBmVsdb_HDRILGZQ_marAMHUMShNJ',
 +    'access_token'  => '103-token',
 +    'role'          => 10,
 +    'status'        => 0,
 +]);
 +$user_id = $this->tester->haveRecord('app\models\User', ['username' => 'admin']);   // Using ORM      
 +</code>
 +
 +Update database records:
 +<code php>
 +$tblUser = Yii::$app->db->schema->getRawTableName(app\models\User::tableName());
 +// Updates user record to be used
 +$this->tester->updateInDatabase($tblUser, ['status' => 0], ['username' => 'demo']);
 +</code>
 +
 Check records in a table using any of these: Check records in a table using any of these:
 <code php> <code php>
 $this->tester->seeInDatabase('price', ['item_code' => 'TABLE_BLACK']); $this->tester->seeInDatabase('price', ['item_code' => 'TABLE_BLACK']);
-$this->tester->seeRecord(    'app\models\Price', ['description' => 'Black Table']); +$this->tester->seeRecord(    'app\models\Price', ['description' => 'Black Table']);  // Using ORM 
-$this->tester->dontSeeRecord('app\models\Price', ['description' => 'Red Table']);        +$this->tester->dontSeeRecord('app\models\Price', ['description' => 'Red Table']);    // Using ORM      
 </code> </code>
                
Line 61: Line 86:
     }     }
          
 +    function testRecordCanBeChanged()
 +    {
 +        // Create an Price record from framework.  Price will be deleted after the test.
 +        $id = $this->tester->haveRecord('app\models\Price', [
 +            'item_code'   => 'BTEO66',
 +            'description' => 'Octane 6',
 +            'price'       => 200,
 +        ]);
 +        $this->assertNotEquals($id, null);
 +        $this->assertNotNull($id);
 +        
 +        // Access model
 +        $price = \app\models\Price::find()->where(['id' => $id])->one();
 +        //codecept_debug("Price found: " . print_r($price, true));
 +        $price->item_code = 'BTEO6';
 +        $price->save();
 +        $this->assertEquals('BTEO6', $price->item_code);
 +        
 +        // Verify data was saved using framework methods
 +        $this->tester->seeRecord(    'app\models\Price', ['item_code' => 'BTEO6']);
 +        $this->tester->dontSeeRecord('app\models\Price', ['item_code' => 'BTEO66']);
 +    }
     //...     //...
 }     }    
Line 66: Line 113:
  
 == Comments == == Comments ==
 +Use ''amGoingTo'', ''expect'', ''expectTo'' to make test more descriptive:
 <code php> <code php>
-$I->wantTo('query the customer info using his phone number');+$I->amGoingTo('query the customer info using his phone number');
 $I->expectTo('see query result'); $I->expectTo('see query result');
 +$I->expect('good results');
 </code> </code>
  
Line 127: Line 176:
     $I->see('Login');     $I->see('Login');
 }     }    
 +</code>
 +
 +With fields, you can fill them in several says, all meaning the same thing:
 +<code php>
 +// All equivalent (input box)
 +$I->fillField('input',   ['name' => 'UserSearch[username]'], 'demo');
 +$I->fillField(['name' => 'UserSearch[username]'], 'demo');
 +$I->fillField('UserSearch[username]', 'demo');
 +$I->fillField('#user-username', 'demo');
 +$I->fillField('#myformname input=[UserSearch[username]]', 'demo');
 +$I->fillField('input=[UserSearch[username]]', 'demo');
 +$I->fillField(['id' => 'user-username'], 'demo');
 +$I->fillField('input[id=user-username]', 'demo');
 +
 +// All equivalent (selection box)
 +$I->seeOptionIsSelected('form select[id=user-status]', 'inactive');      // status: inactive
 +$I->seeOptionIsSelected('select[id=user-status]', 'inactive');           // status: inactive
 +$I->seeOptionIsSelected('#user-status', 'inactive');                     // status: inactive
 +</code>
 +
 +== Debug ==
 +<code php>
 +// Access model
 +$entry = \app\models\Entry::find()->where(['id' => $id])->one();
 +codecept_debug("find a value in " . print_r($entry, true));  // debug
 </code> </code>