class Calculator
{
/**
* @return array
*/
function doPowTable ($n)
{
$table = array();
for ($i = 1; $i <= $n; $i++)
{
$table[] = array(
'i' => $i,
'factor' => pow($i, 2)
);
}
return $table;
}
}
and a format class:
class Formatter
{
/**
* @return array
*/
function getAndFormat ($n)
{
$table = Calculator.doPowTable ($n);
return array_map (function($item) {
$item['i_factor'] = $item['i'].'_'.$item['factor'];
return $item;
}, $table);
}
}
and testing it.
Calculator:
$this->assertEquals (array(
array('i' => 1, 'factor' => 1),
array('i' => 2, 'factor' => 4),
array('i' => 3, 'factor' => 9),
array('i' => 4, 'factor' => 16),
), Calculator.doPowTable(4));
Helper:
$this->setMock ('Calculator.doPowTable')->once()->with(4)->willReturn(
array('i' => 1, 'factor' => 1),
array('i' => 2, 'factor' => 4),
array('i' => 3, 'factor' => 9),
array('i' => 4, 'factor' => 16),
);
$this->assertEquals (array(
array('i' => 1, 'factor' => 1, 'i_factor' => '1_1'),
array('i' => 2, 'factor' => 4, 'i_factor' => '2_4'),
array('i' => 3, 'factor' => 9, 'i_factor' => '3_9'),
array('i' => 4, 'factor' => 16, 'i_factor' => '4_16'),
), Helper.format($source));
so everything passes. But what if I rename the factor column to pow? Ok, doPowTable test is changed then too, OK. But the 2nd will pass, even though it gets a "wrong" input structure, but still will pass in test environment. But in reality, this is still bad, in production it will fail.
So my main problem is, if the original structure changes, it then should changed in mocked-fixture too, but nothing enforces it.
Aucun commentaire:
Enregistrer un commentaire