I have this code:
public function checkKeyConstraint($key) {
if ($this->hasKey($key)) {
throw new Exception('Key already exists '.$key);
}
return $this;
}
Also i have two PHPUnit test methods with data providers, one for existent keys and the other for non-existent. The problem is that throw new Exceptions('Key already exists '.$key); line is shown by the code coverage tool as a not executed.
PHP_CodeCoverage 1.1.3, PHPUnit 3.6.12, PHP 5.4.4, xdebug 2.2.1-5.4-vc9
UPD: test methods and data providers
/**
* @covers Dict::checkKeyConstraint
*
* @dataProvider providerNonexistentKeys
*/
public function testCheckKeyConstraintNonExistent($key) {
$this->assertEquals(self::$object, self::$object->checkKeyConstraint($key));
}
/**
* @covers Dict::checkKeyConstraint
*
* @expectedException Exception
* @dataProvider providerValidKeyValues
*/
public function testCheckKeyConstraintExistent($key) {
$this->assertEquals(self::$object, self::$object->checkKeyConstraint($key));
$this->fail();
}
public function providerNonexistentKeys() {
$data = array();
for ($i = 0; $i < 10; $i++) {
$data[] = array('randKey:' . rand());
$data[] = array(rand());
}
return $data;
}
public function providerValidKeyValues() {
$data = array();
for ($i = 0; $i < 20; $i++) {
$stub = new Key('id#' . $i, 'val#' . $i . '#string');
$data[] = array($stub, $stub);
$stub = new Key($i, 'val#' . $i . '#numeric');
$data[] = array($stub, $stub);
}
return $data;
}
I’m an idiot. The problem was that in this
testCheckKeyConstraintExistentmethod i check whole object, not it’s key. It should be rewritten as: