Writing a “Hello World” Test with QBElggTestsForPHPUnit

Posted by on Dec 16, 2011 | 0 comments

Hello World

 

Writing a test with QBElggTestsForPHPUnit is not much different that writing a standard test with PHPUnit. In future posts we will see how to tweak things a little (like adding custom fixtures) but right now I’m just going to show you how to write a very simple tests: the HelloWorldTest . The purpose of this test is two-folded:

  • Showing a simple example of a test.
  • Showing that each test runs on a “clean” DB state.

To do this we will create a test case class with two test methods (testCreateNewObject1 and testCreateNewObject2) such that the second test depends on the first one. This will ensure that testCreateNewObject1 will run always before testCreateNewObject2 (check the @depends PHPUnit directive).

In testCreateNewObject1 we are going to create a new object, save it to the DB and check that it has a valid GUID. But that’s not it: we are also going to pass the GUID of the object to the next test and see what happens when testCreateNewObject2 creates a new object. If the tests don’t share the same DB state then creating a new object should yield the same GUID, since both tests where executed on “clean” DBs. As a matter of fact, the GUID created in testCreateNewObject1 should not even be a valid entity GUID in testCreateNewObject2.

So, here goes the code with the necessary comments:

require_once(dirname(__FILE__) . '/../QBElggTestsForPHPUnit/model/ElggTestCase.php');

class HelloWorldTest extends ElggTestCase
{
/**
* We are going to test that creating an object in
* different tests yields the same GUID. To do this
* we create an object in one test and pass the GUID to
* another test that depends on this one.
*
* @return int GUID
*/

public function testCreateNewObject1()
{
$obj = new ElggObject();
$obj->title = "Hello World Test!";
$obj->save();
$this->assertInternalType('integer', $obj->getGUID());
$this->assertNotNull($obj->getGUID());
$this->assertNotEquals($obj->getGUID(), 0);
return $obj->getGUID();
}

/**
* Now we check that there is no entity with
* the previously created GUID and that if
* we create a new object we get that GUID
* again.
*
* @depends testCreateNewObject1
*
* @param int $guid
*/

public function testCreateNewObject2($guid)
{
$this->assertEquals(get_entity($guid), array());

$obj = new ElggObject();
$obj->title = "A new object";
$obj->save();
$this->assertInternalType('integer', $obj->getGUID());
$this->assertNotNull($obj->getGUID());
$this->assertNotEquals($obj->getGUID(), 0);
$this->assertEquals($guid, $obj->getGUID());
}
}

We can now run this test in the console by doing:

phpunit --process-isolation HelloWorldTest.php
PHPUnit 3.6.3 by Sebastian Bergmann.

..

Time: 6 seconds, Memory: 2.75Mb

OK (2 tests, 8 assertions)

And that is pretty much it :)

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>