Ideas how to write good OOP code, SOLID, incapsulation, loose coupling and so lead to
- not using public properties
- use as little getters as possible
Say we have code
class Some
{
protected int $x;
protected int $y;
public function __construct(int $x, int $y) {
$this->x = $x;
$this->y = $y;
}
public function swapXY() : void {
$r = $this->x;
$this->x = $this->y;
$this->y = $r;
}
}
Test will not work
class SomeTest extends PHPUnitFrameworkTestCase
{
public function testSwap() {
$obj = new Some(5, 6);
$obj->swapXY();
$this->assertSame(6, $obj->x); // ERROR
$this->assertSame(5, $obj->y);
}
}
How can I test Some::swapXY() method with phpunit since phpunit 9.0 dropped support methods like assertAttributeSame() ?
I am not discussing here testing protected/private methods.
Only this situation: We send a signal to object via it’s method.
Post condition of this work is not some value returned, but some of object properties set to another values.
Using Reflection will help, but why it is considered like so bad idea that it was taken away from testing framework?
Thanks
Tried to solve with the ways recomended