A helper class for testing, especially for unit tests to access inaccessible methods or to get/inject properties.
Install via composer:
dcrr composer req --dev r83dev/test-accessible
Use the AccessibleTrait in your test:
class MyTest {
use R83Dev\TestAccessible\AccessibleTrait;
}Use one of the helper function to access private or protected stuff of an instantiated object.
$this->callInaccessibleMethod($object, 'method_name', 'argument1', 'argument2', ...);$this->getInaccessibleProperty($object, 'property_name');$this->setInaccessibleProperty($object, 'property_name', 'new_property_value');$this->setInaccessibleProperties($object, ['property_name' => 'new_property_value']);$this->getInaccessibleConstant($object, 'CONSTANT_NAME');This is just a features demo, not useful in terms of content.
Example of a unit to be tested:
class MyUnit
{
protected const STATE_ON = 'on';
protected const STATE_OFF = 'off';
private string $state = self::STATE_ON;
protected function setState(string $state): void
{
$this->state = $state;
}
public function getState(): string
{
return $this->state;
}
}Example unittest:
class MyUnitTest extends PHPUnit\Framework\TestCase
{
use R83Dev\TestAccessible\AccessibleTrait;
private ?MyUnit $unit;
protected function setUp(): void
{
$this->unit = new MyUnit();
}
#[\PHPUnit\Framework\Attributes\Test]
public function getStateShouldReturnCorrectInitialValue(): void
{
$this->assertSame(
$this->getInaccessibleConstant($this->unit, 'STATE_ON'),
$this->getInaccessibleProperty($this->unit, 'state')
);
}
#[\PHPUnit\Framework\Attributes\Test]
public function getStateReturnsNewValueFromSetter(): void
{
$off = $this->getInaccessibleConstant($this->unit, 'STATE_OFF');
$this->callInaccessibleMethod($this->unit, 'setState', $off)
$this->assertSame($off, $this->unit->getState());
}
#[\PHPUnit\Framework\Attributes\Test]
public function getStateReturnsNewValueFromPropertyAllocation(): void
{
$this->assertSame(
'unknown',
$this->setInaccessibleProperty($this->unit, 'state', 'unknown')->getState()
);
}
}