phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Add a method to create callable mock

Open mdumoulin opened this issue 6 years ago • 1 comments

In some case, it can be useful to mock a callable. So far, no method exists to provide it, but the following workaround is possible :

$mock = $this->createPartialMock(\stdClass::class, ['__invoke']);
$mock
      ->expects(self::once())
      ->method('__invoke')
      ->with(1, 2)
      ->willReturn(3);

The mock of method that do not actually exists in original class is deprecated in version 8 and will no longer be supported for version >= 9.

The previous workaround can be replaced by the following one :

$mock = $this->getMockBuilder(\stdClass::class)
    ->addMethods(['__invoke'])
    ->getMock();
$mock
      ->expects(self::once())
      ->method('__invoke')
      ->with(1, 2)
      ->willReturn(3);

This PR add a 'createCallableMock' method to provide the expected mock. If the PR is accepted, it would be possible to replace previous workaround by :

$mock = $this->createCallableMock();
$mock
      ->expects(self::once())
      ->method('__invoke')
      ->with(1, 2)
      ->willReturn(3);

mdumoulin avatar Oct 25 '19 08:10 mdumoulin

Codecov Report

Merging #3908 into master will increase coverage by <.01%. The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3908      +/-   ##
============================================
+ Coverage     83.67%   83.68%   +<.01%     
- Complexity     3855     3856       +1     
============================================
  Files           151      151              
  Lines         10199    10203       +4     
============================================
+ Hits           8534     8538       +4     
  Misses         1665     1665
Impacted Files Coverage Δ Complexity Δ
src/Framework/TestCase.php 80.9% <100%> (+0.08%) 339 <1> (+1) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 69ac974...b097a7b. Read the comment docs.

codecov[bot] avatar Oct 25 '19 08:10 codecov[bot]

Thank you for your contribution. I appreciate the time you invested in preparing this pull request. However, I have decided not to merge it.

sebastianbergmann avatar Jan 13 '24 13:01 sebastianbergmann