Codeception Testing with Yii Framework 2.0 [Day 6]

Today’s Objective(s)

  1. Explore and understand the basic functions of basic test methods (Understand what is going on in testModelBoolean() function to be precise).
  2. Try to use the entire $I->wantTo(‘load all values’); style of Codeception

Introduction

So today’s highest priority item is exploring the test methods, ie. how does the TestCase Class tick exactly. Not many actions involved in this, so let’s delve into a few of the key functions that I’m not quite understanding in this code:

class MySimpleModelTest extends TestCase
{
 use Specify;

 public function testModelBoolean()
 {
   $model = new MySimpleModel();

   $this->specify('model bool function should return a boolean opposite to boolean passed', function () use ($model) {
     expect('argument is true, result should be false', $model->bool(true))->false();
     expect('argument is false, result should be true', $model->bool(false))->true();
   });
 }
}

$this->specify

Now, from my very brief overviews of documentation in Codeception I can tell you that this function is best use to describe the test that you are running (Which is a good thing and believe I’ll keep this around), but I’d like to get a grip on the callback part of this function.

I’m guessing from reading the code above that the function’s basic parameters are a string to describe the test being performed, and an anonymous function that does the actual test… what’s confusing me out of this is why it needs to “use ($model)” in the callback function definition and I believe this is a lack in basic PHP education on my part, which I will now read up on. [time passes]

From reading this documentation on anonymous functions (under example #3) I can see that defining an anonymous function with the ‘use’ “language construct” (Learnt a new term there) allows the function to use the variable as it’s own and as it stands in the Parent scope (Our function ‘testModelBoolean’). I have a few further questions on this matter, but that’s out of scope of this article.

expect

This function looks like a rather interesting assertion function to me:

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

The definition I’m getting from reading that code is that it accepts a string to describe what it’s expecting and then the function that is to be tested. It then returns some form of Class or Interface that can then be used to check the returned value…. let me try and find out an actual definition of this. [time passes]

So after a bit of searching… trying to google “expect” is a bit of a waste of time, I looked into the code that I have and found that expect is being inherited from \Codeception\Actor and went off to find that the function is declared in the Comment trait that the Actor class uses and the declaration seems to only accept one argument which is a prediction… I’ve found some more interesting stuff in there, but think I’ll leave this here for now and continue tomorrow :)

Conclusion

Not a lot to add tonight, but I have learnt a basic of PHP which is useful (heh) and believe I’m about to be jumping down a rabbit hole while trying to understand this ‘expect’ function which should be a lot of fun!

Upcoming Objective(s)

  1. Explore and understand the basic functions of basic test methods (Understand what is going on in testModelBoolean() function to be precise).
  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

(I added the questions I had from Day 3 so that they wouldn’t get lost)

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>