Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Do not mock method which returns final class #386

Merged
merged 1 commit into from
Nov 2, 2017
Merged

Do not mock method which returns final class #386

merged 1 commit into from
Nov 2, 2017

Conversation

kubawerlos
Copy link
Contributor

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
Copy link

codecov-io commented Nov 2, 2017

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) ⬆️

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 sebastianbergmann merged commit d1c69f3 into sebastianbergmann:master Nov 2, 2017
@sebastianbergmann
Copy link
Owner

Thanks!

@kubawerlos kubawerlos deleted the do-not-mock-method-which-has-final-class-as-return-type branch November 9, 2017 21:50
lcobucci added a commit to lcobucci/phpunit-mock-objects that referenced this pull request Nov 21, 2017
Because of the validation added recently that checks if the returned
class is final, we can no longer mock methods that uses `\Generator` or
`\Closure` as return type.

I thought on verying that in a different way but I can't think of
another possible scenario for this kind of issue.

Reference: sebastianbergmann#386
sebastianbergmann pushed a commit that referenced this pull request Nov 22, 2017
Because of the validation added recently that checks if the returned
class is final, we can no longer mock methods that uses `\Generator` or
`\Closure` as return type.

I thought on verying that in a different way but I can't think of
another possible scenario for this kind of issue.

Reference: #386
@ghost
Copy link

ghost commented Dec 1, 2017

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

@sebastianbergmann
Copy link
Owner

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

axiac pushed a commit to axiac/phpunit that referenced this pull request May 8, 2018
Because of the validation added recently that checks if the returned
class is final, we can no longer mock methods that uses `\Generator` or
`\Closure` as return type.

I thought on verying that in a different way but I can't think of
another possible scenario for this kind of issue.

Reference: sebastianbergmann/phpunit-mock-objects#386
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants