Answer the question
In order to leave comments, you need to log in
PHPUnit: What's the right way to test the classes that need to be tested and their initial creation/population?
For example, we have a class that implements the `ArrayAccess` interface. Accordingly, even just filling it with values, we are already using its functionality, which must also be tested, namely the offsetSet() method. That is, we have a test for the OffsetSet() method itself:
protected function setUp()
{
$this->Filter = new Filter();
}
public function testOffsetSet()
{
$this->Filter[ 'page' ] = 1;
$this->Filter[ 'limit' ] = 2;
$this->Filter[ 'category_id' ] = 3;
$this->AssertEquals($this->Filter->get_array(), [
'page' => 1,
'limit' => 2,
'category_id' => 3,
]);
}
public function testOffsetGet()
{
$this->Filter[ 'page' ] = 1;
$this->Filter[ 'limit' ] = 2;
$this->Filter[ 'category_id' ] = 3;
echo $this->Filter[ 'page' ];
}
private $Filter;
protected function setUp()
{
$this->Filter = new Filter();
}
protected function tearDown()
{
unset($this->Filter);
}
/**
* @return Filter
*/
public function testOffsetSet()
{
$this->Filter[ 'page' ] = 1;
$this->Filter[ 'limit' ] = 2;
$this->Filter[ 'category_id' ] = 3;
return $this->Filter;
}
/**
* @depends testOffsetSet
*
* @param Filter $filter
*/
public function testGet_array($filter)
{
$this->AssertEquals($filter->get_array(), [
'page' => 1,
'limit' => 2,
'category_id' => 3,
]);
}
Answer the question
In order to leave comments, you need to log in
ArrayAccess is an interface and, accordingly, it is not necessary to test it, but specific methods of the class and check that the class implements it.
for example, let there be a Magic class that implements ArrayAccess, then MagicTest could be something like this
class MagicTest extends Test
{
public function __setUp()
{
$this->magic = new Magic();
}
public function providerValue()
{
return ;// и тд
}
/**
* @dataProvider providerValue
* @depends testMustBeArrayAccesable
*/
public function testSet($key, $value)
{
$this->magic[$key] = $value;
assertSame($value, $this->magic[$key]);
}
public fuinction testMustBeArrayAccesable()
{
assertTrue($this->magic instanceof ArrayAccess);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question