php - Is it possible to mock "require" or "require_once" statement with Kahlan? -


i have old legacy code. before starting refactoring want make unit tests code. bad thing files relies on each other "require_once" statements (hovewer, functions want test isolated).

so can't write normal tests if not able mock "require_once" statement.

i'm using kahlan framework testing. possible mock such statements? if yes, how?

it not possible.

however, have made plugin kahlan allows "extract" functions file , skip other code. not allow "mock" statements, allow test isolated functions. made pull-request , if not accepted, make separate packagist composer packet , update answer.

changes can found here:

https://github.com/lamp-of-god/kahlan/tree/isolator-plugin

example of usage:

file:

<?php require_once 'something';  // string "skipped" on test  do_something();  // string  function test() {     return 42; } 

test file plugin:

<?php  use kahlan\plugin\isolator;  isolator::isolate(dirname(__dir__).'/../test.php');  describe('test()', function() {     it('returns 42', function() {         expect(test())->tobe(42);     }); }); 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -