A
A
Alexey Potapenko2019-08-26 20:59:24
PHP
Alexey Potapenko, 2019-08-26 20:59:24

How to test a class method that calls the void method of a property of that class and changes the state of that class?

There are classes:

class Aaa {
    private $bbb;
    public $some_property = 10;

    function __construct(B $bbb){
        $this->bbb = $bbb;
    }

    public function some_func(){
        // ...
        $bbb->change_aaa_property($this);
        // ...
    }
}

class Bbb {
    function change_aaa_property(A $aaa){
        $aaa->some_property += 10;
    }
}

In the some_func() method of the Aaa class, the change_aaa_property() method is called on the $bbb object, which is a property of the Aaa class. In this change_aaa_property() property $some_property of class Aaa is changed.
I know that dependencies need to be replaced with mock objects, i.e. $bbb needs to be replaced with a mock object. But then how would the dummy $bbb object's change_aaa_property() method be able to change the $some_property of the $aaa object??? After all, mock objects, it seems, do not have an implementation of methods. And after testing some_func() , the value of $some_property remains the same (10).
In short, I want to understand how, when testing some_func() , to have the change_aaa_property() method of the dummy $bbb object change the $some_property property of the Aaa class.
Or perhaps there is another way to test some_func()?
I searched for a long time but did not find anything intelligible, maybe I was looking wrong. Yes, perhaps this is bad design and you shouldn’t do it, but is it really possible to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2019-08-26
@mcakaneo

$bbb = new Bbb();
$aaa = new Aaa($bbb);
$aaa->some_func();
$this->assertEquals(20, $aaa->some_property);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question