Is it possible to replace some class instantiation with the mock one so that when I call new SomeClass()
the mock one is created with specified mocked behavior?
Asked
Active
Viewed 1,948 times
2

pandomic
- 607
- 1
- 6
- 17
-
2Did you read this https://phpunit.de/manual/3.0/en/mock-objects.html ? – JustOnUnderMillions Dec 01 '16 at 16:33
1 Answers
3
It is highly not recommended but possible, take a look at links bellow
https://github.com/php-test-helpers/php-test-helpers
https://github.com/krakjoe/uopz
It's better to use Dependency Injection (simply, it means injecting instances as arguments to function instead of creating them inside the function). As a result you will be able easily to mock that argument while testing. You can google about other pros of DI, but here a few links for you: answer on SO, DI and phpunit