AspectMock icon indicating copy to clipboard operation
AspectMock copied to clipboard

How I can delete all final keywords from my class and methods

Open yapro opened this issue 7 years ago • 4 comments

I'm talking about: http://php.net/manual/en/language.oop5.final.php

Why I need this: https://phpunit.de/manual/current/en/test-doubles.html Final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behavior.

Off course I want to delete "final" only during a test execution.

yapro avatar Mar 23 '17 12:03 yapro

you can't remove 'final' from method

AspectMock injects some code to every method (modifying the code before parsing instead of playing with inheritance) and allows you to return from methods early with specified result

you can just double with test::double(…) with AspectMock

zuozp8 avatar Apr 04 '17 14:04 zuozp8

I know it, but I need delete "final".

yapro avatar Apr 04 '17 14:04 yapro

you can use AspectMock to modify the fuction that is final and use phpunit mocks for everything else

if you really need to delete 'final' you can look at https://github.com/Codeception/AspectMock/blob/master/src/AspectMock/Kernel.php and https://github.com/Codeception/AspectMock/blob/master/src/AspectMock/Intercept/BeforeMockTransformer.php and implement it yourself

zuozp8 avatar Apr 04 '17 16:04 zuozp8

@zuozp8

Do I read this correctly that AspectMock supports mocking 'final'/immutable classes out of the box?

you can't remove 'final' from method AspectMock injects some code to every method (modifying the code before parsing instead of playing with inheritance) and allows you to return from methods early with specified result you can just double with test::double(…) with AspectMock

lougreenwood avatar Jul 19 '17 19:07 lougreenwood