Is it good to reorganize a test case if two or more test classes that test different implementations of the same interface / abstract class have common tests , but with different devices?
Let's say that the code and tests look like this:
interface MathOperation
{
public function doMath($a, $b);
}
class Sumator implements MathOperation
{
public function doMath($a, $b)
{
return $a + $b;
}
}
class Multiplicator implements MathOperation
{
public function doMath($a, $b)
{
return $a * $b;
}
}
class SumatorTest extends PHPUnit_Framework_TestCase
{
protected $sumator;
public function setUp()
{
$this->sumator = new Sumator;
}
public function testDoMath($a, $b, $expected)
{
$result = $this->sumator->doMath($a, $b);
$this->assertEqual($expected, $result);
}
public function fixtures()
{
return array(
array(1, 1, 2);
array(2, 1, 3);
array(100, -1, 99);
);
}
}
class MultiplicatorTest extends PHPUnit_Framework_TestCase
{
protected $multiplicator;
public function setUp()
{
$this->multiplicator = new Multiplicator;
}
public function testDoMath($a, $b, $expected)
{
$result = $this->multiplicator->doMath($a, $b);
$this->assertEqual($expected, $result);
}
public function fixtures()
{
return array(
array(1, 1, 1);
array(2, 1, 2);
array(100, -1, -100);
);
}
}
and I want them (tests) to look like this:
class MathOperationTestCase extends PHPUnit_Framework_TestCase
{
protected $operation;
public function setUp()
{
$this->operation = $this->createImpl();
}
abstract function createImpl();
public function testDoMath($a, $b, $expected)
{
$result = $this->operation->doMath($a, $b);
$this->assertEqual($expected, $result);
}
abstract public function fixtures();
}
class SumatorTest extends MathOperationTestCase
{
public function createImpl()
{
return new Sumator;
}
public function fixtures()
{
return array(
array(1, 1, 2);
array(2, 1, 3);
array(100, -1, 99);
);
}
}
class MultiplicatorTest extends MathOperationTestCase
{
public function createImpl()
{
return new Multiplicator;
}
public function fixtures()
{
return array(
array(1, 1, 1);
array(2, 1, 2);
array(100, -1, -100);
);
}
}
This seems more structured, but may not have readability . Therefore, in the end, I'm not sure if this is a practical practice.