PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Phpunit: replace class with mock

 March 09, 2022     cakephp-3.0, php, phpunit     No comments   

Issue

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?


Solution

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



Answered By - Oleks
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing