sebastianbergmann / phpunit-mock-objects

Mock Object library for PHPUnit
https://phpunit.de/
Other
4.98k stars 154 forks source link

Do not mock method which returns final class #386

Closed kubawerlos closed 6 years ago

kubawerlos commented 6 years ago

Currently for a class Example:

class Example
{
    public function returnFinal(): FinalClass
    {
        return new FinalClass();
    }
}

final class FinalClass
{
}

when trying to mock:

    public function testExample(): void
    {
        $example = $this->createMock(Example::class);
        $example->returnFinal();
    }

an error occurs:

Class "FinalClass" is declared "final" and cannot be mocked
codecov-io commented 6 years ago

Codecov Report

Merging #386 into master will increase coverage by 0.08%. The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #386      +/-   ##
============================================
+ Coverage     71.73%   71.82%   +0.08%     
- Complexity      438      442       +4     
============================================
  Files            27       27              
  Lines          1256     1260       +4     
============================================
+ Hits            901      905       +4     
  Misses          355      355
Impacted Files Coverage Δ Complexity Δ
src/Generator.php 84.35% <100%> (+0.12%) 179 <3> (+4) :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 54a9c23...98b8796. Read the comment docs.

sebastianbergmann commented 6 years ago

Thanks!

ghost commented 6 years ago

You can actually mock the method when it returns the final class optionally, like method(): ?MyFinalClass.

sebastianbergmann commented 6 years ago

@pr0nbaer Correct. That is now possible as of a8984cde0be9cceb892a935f714d333eeda6b97b.