I’m just beginning with PHPUnit and TDD.
Among others, I can’t really answer to this question: Is this a good test? Am i actually testing my code or something already tested (i.e. the framework or PHP itself)?
Little example, this is the test subject:
class DateMax extends Constraint
{
/**
* @var string
*/
public $limit;
/**
* @var string
*/
private $invalidLimit = 'Option "limit" should be a valid date/time string.';
public function __construct($options = null)
{
parent::__construct($options);
if(false === strtotime($this->limit)) {
throw new InvalidOptionsException($this->invalidLimit, ['limit']);
}
}
}
I want to test that InvalidOptionsException is expected when invalid “limit” options are passed, otherwise $constraint->limit holds the correct value:
/**
* @dataProvider getInvalidLimits
* @expectedException InvalidOptionsException
*/
public function testInvalidLimits($testLimit)
{
new DateMax($testLimit);
}
/**
* @dataProvider getValidLimits
*/
public function testValidLimits($testLimit)
{
$constraint = new DateMax($testLimit);
$this->assertEquals($testLimit, $constraint->limit);
}
/**
* @return array[]
*/
public function getInvalidLimits()
{
return array(array('invalid specification'), array('tomorr'));
}
/**
* @return array[]
*/
public function getValidLimits()
{
return array(array('now'), array('+1 day'),array('last Monday'));
}
So question is does this make any sense or I’m testing the framework/PHP itself?
Of course it has sense, because you override constructor of Constraint class and there is possibility that you’ll break something inside it. So basing on your constructor logic basically you want to test two things:
edit: Some use case where first test will be useful may be this one:
Let say at some moment you want to extend your DateMax constructor in this way:
but for example you didn’t notice that method “doWeirdThings” takes a reference as argument. So in fact it changes $options value, what you didn’t expect, but first test fails so you won’t miss it.