Codeception Testing with Yii Framework 2.0 [Day 7]

Today’s Objective(s)

  1. Delve deeper into the “expect” function used in testModelBoolean()
  2. Try to use the entire $I->wantTo(‘load all values’); style of Codeception
  3. Research executing tests and viewing results from a browser
  4. Research best practices for Unit testing in Yii
  5. Research best pracices for acceptance/functional testing in Yii

Introduction

Today’s objective is to get a better understanding of the “expect” function from yesterday. The major reason I decided on this, because I found something interesting while I was going through it last night. Let me break it down.

Declared functions for expect

The following code is all coming from the Codeception github page.

Yesterday we found that the expect function is inherited and the UnitTester class used in Yii extends Codeception\Actor. That Actor class itself doesn’t have a definition for expect but it uses 2 traits Comment and Friend, looking into the Comment trait I found this:

trait Comment
{
 [...]
 
 public function expect($prediction)
 {
   return $this->comment('I expect ' . $prediction);
 }
 
 [...]
 
 public function comment($description)
 {
   $this->scenario->comment($description);
   return $this;
 }
}

So this expect accepts 1 argument $prediction and then calls the comment function while prepending “I expect ” to the argument, the result of which is returned. Just as a quick refresher let me show the code that we’re trying to dig into:

expect('argument is true, result should be false', $model->bool(true))
      ->false();

As you can see, in this function call we’re passing 2 arguments and then running the function false off of the returned object. This blows my mind and nags at me that I’ve found the wrong function definition. What I would really like to do in this case is debug and step into the function call… but my knowledge of debugging only covers web functions and not console functions, believe I’ll add it to the objectives list. :)

Anyway, I thought I’d dig deeper into this declaration to see where it goes. Above I also included the definition of the comment function, the result of which is being returned by our expect function. The comment function then calls the comment function of the class’s ($this) scenario object and returns the class itself. So let me find the definition of the scenario object and it’s comment function:

/**
 * @var \Codeception\Scenario
 */
 protected $scenario;
 public function __construct(\Codeception\Scenario $scenario)
 {
   $this->scenario = $scenario;
 }

Here we have the declaration of $scenario and find out that it it’s class is \Codeception\Scenario with that knowledge I’m off to find the definition of comment. I think I’ll do this a lot faster if I just do it all in a code box with comments on lines (//) ;)

 public function comment($comment) 
 {
   $this->runStep(new \Codeception\Step\Comment($comment,array()));
 }

 //comment calls this function
 public function runStep(Step $step)
 {
   $this->steps[] = $step;  //add step to the array of steps
   $result = $this->test->runStep($step);
   $step->executed = true;
   return $result;
 }

I wanted to find the declaration of the runStep function in the test variable of the Scenario class, but found that the class for test (\Codeception\TestCase) is an extension of PHPUnit_Framework_TestCase and implements PHPUnit_Framework_SelfDescribing.

I still want to go further into this, but I’ve run out of time. And I feel that this method is taking too long, I’ll rearrange the objectives for tomorrow and approach this the day after next with a proper debug stepping session.

Conclusion

About the only major thing learned today was that browsing through a GitHub repository online while hunting down what a function actually does, is a bit of a time sink. Hopefully improve on this in the future.

Upcoming Objective(s)

  1. Learn to debug console PHP application
  2. Delve deeper into the “expect” function used in testModelBoolean()
  3. Try to use the entire $I->wantTo(‘load all values’); style of Codeception
  4. Research executing tests and viewing results from a browser
  5. Research best practices for Unit testing in Yii
  6. Research best pracices for acceptance/functional testing in Yii

Ps. I know I’m playing it rather fast and loose with the definition of ‘object’/’class’ for a good portion of this, but I’m more interested in understanding the code behind it than being pedantic… that being said it does bug me that I’ve done it anyway.

One thought on “Codeception Testing with Yii Framework 2.0 [Day 7]”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>