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,176 times
2
![](../../users/profiles/3797956.webp)
pandomic
- 569
- 1
- 5
- 16
-
2Did you read this https://phpunit.de/manual/3.0/en/mock-objects.html ? – JustOnUnderMillions Dec 01 '16 at 16:33
1 Answers
2
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