phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

PHPUnit: Expects in setUp is bad usage

Open ScreamingDev opened this issue 6 years ago • 0 comments

Let's start with common details:

Subject Details
Issue type Feature request
Plugin Php Inspections (EA Extended) or Php Inspections (EA Ultimate), which version

Current behaviour (description/screenshot:)

class FistTest extends TestCase {
  function setUp() {
    $this->arrayMock = $this->createMock(\ArrayAccess::class);
    $this->arrayMock->expects($this->once())->method('getArrayCopy')->willReturn([1337]);
  } 

  function testFoo() {
    static::assertEquals(1337, $this-arrayMock()->getArrayCopy()[0]);
  }
}

class SimilarTest extend FirstTest {
  function testBar() {
    static::assertEquals( 'flat', (new World())->getShape() );
  }
}

(unsure if code valid) The second test fails because it did not call getArrayCopy (which is expected once).

Expected behaviour

It should not fail. So using expect in the setUp seems like bad usage to me. But I dunno what a phpUnit author would say. I would expect the setUp to just use $mock->method('foo')->willReturn('bar') without any expect at all. In my opinion using expect in setUp is bad usage but until well discussed or some phpUnit dev is asked I suggest this to be an optional check.

ScreamingDev avatar Feb 13 '19 22:02 ScreamingDev