Suppose there are several public methods in the class under test:

class Sample { function method1() { return [1,2,3]; } function method2($id) { return (in_array($id, $this->method1())); } } 

method2 depends on the results of method1 .

I create tests for the first method, in which I drive it with different sets of initial values. Then I take the tests for the second method, in which I emulate the results of the first:

 $mock = $this->getMockBuilder(Sample::class) ->disableOriginalConstructor() ->setMethods(['method1']) ->getMock(); $mock->method('method1')->willReturn([1,2,3]); $this->assertTrue($mock->method2(1)); 

And now the question:

Sometime in the distant future, as a result of refactoring, I decide that the result of the first method should be displayed as [1=>true,2=>true,3=>true] . With the tests of the first method, everything is in order, they will not pass, and I will immediately solve this problem. But the tests of the second receive the emulated source data, which is no longer relevant.

What is the opportunity / strategy to protect yourself from this situation?

  • one
    I would venture to suggest that if you refactor any method on which something depends, you will also correct the dependent for correct processing, otherwise everything will fall. And there and tests with them. - user207618
  • @Skyrocker Well, if you follow the TDD methodology, then the tests are primary and you need to write code so that the tests are executed. And if I hope that I remember what depends on me - why do I need tests at all? - toxxxa
  • one
    Editing external methods is always a risk. In such cases, it is worth doing something like a repository, where all the methods and their possible returns. There you will correct and all the others, which are returned for checks taken from the storage, will fall with an error. - user207618

0