2013-07-30 11:55:55 +02:00
|
|
|
# Writing PHPUnit tests
|
|
|
|
|
|
|
|
## Test path and filename
|
|
|
|
|
|
|
|
The path where you should put your PHPUnit tests should reflect the path in the sourcetree, with test/php/ prepended. So
|
|
|
|
if you're testing a file library/Icinga/My/File.php the test file should be at test/php/library/Icinga/My/File.php. This
|
2014-04-10 15:54:50 +02:00
|
|
|
also applies for modules, where the tests are underneath modules/myModule/test/php
|
2013-07-30 11:55:55 +02:00
|
|
|
|
|
|
|
## Example test skeleton
|
|
|
|
|
|
|
|
Let's assume you're testing a class MyClass underneath the MyModule module and the file can be found at
|
|
|
|
modules/mymodule/library/MyModule/Helper/MyClass.php.
|
|
|
|
|
|
|
|
<?php
|
2014-04-10 15:54:50 +02:00
|
|
|
// The namespace is the same namespace as the file to test has, but with 'Tests' prepended
|
|
|
|
namespace Tests\Module\MyModule\Helper;
|
2013-07-30 11:55:55 +02:00
|
|
|
|
|
|
|
class MyClassTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testSomething()
|
|
|
|
{
|
|
|
|
$this->assertTrue(true, "Asserting that the world didn't end yet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 17:20:38 +02:00
|
|
|
## Testing Singletons
|
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
When test methods **modify static** class properties (which is the case when using singletons), do not add the PHPUnit
|
2013-09-03 17:20:38 +02:00
|
|
|
[`@backupStaticAttributes enabled`](http://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.backupStaticAttributes)
|
2014-04-11 15:31:29 +02:00
|
|
|
annotation to their [DocBlock](http://www.phpdoc.org/docs/latest/for-users/phpdoc/basic-syntax.html#what-is-a-docblock)
|
|
|
|
in order to backup and restore static attributes before and after the test execution respectively. Use the setUp()
|
|
|
|
and tearDown() routines instead to accomplish this task.
|
2013-09-03 17:20:38 +02:00
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace My\Test;
|
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
use Icinga\Test\BaseTestCase;
|
2013-09-03 17:20:38 +02:00
|
|
|
use My\CheesecakeFactory;
|
|
|
|
|
2014-04-11 15:31:29 +02:00
|
|
|
class SingletonTest extends BaseTestCase
|
2013-09-03 17:20:38 +02:00
|
|
|
{
|
2014-04-11 15:31:29 +02:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->openingHours = CheesecakeFactory::getOpeningHours();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
CheesecakeFactory::setOpeningHours($this->openingHours);
|
|
|
|
}
|
|
|
|
|
2013-09-03 17:20:38 +02:00
|
|
|
public function testThatInteractsWithStaticAttributes()
|
|
|
|
{
|
|
|
|
CheesecakeFactory::setOpeningHours(24);
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 15:31:29 +02:00
|
|
|
|
|
|
|
The reason to avoid using @backupStaticAttributes is the fact that if it is necessary to utilize a
|
|
|
|
singleton in your *unit* tests you probably want to rethink what you are going to test and because
|
|
|
|
some tests are using the mock framework [`Mockery`](https://github.com/padraic/mockery) which is
|
|
|
|
using static class properties to implement its caching mechanics.
|