2

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?

pandomic
  • 569
  • 1
  • 5
  • 16

1 Answers1

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

Community
  • 1
  • 1
Oleks
  • 1,563
  • 1
  • 15
  • 19