Codeception/AspectMock

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

yapro opened this issue · 4 comments

yapro commented

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.

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

yapro commented

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

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

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