Mocking PHP functions in unit tests

前端 未结 4 2295
深忆病人
深忆病人 2021-02-13 17:52

I\'m unit-testing some PHP code with SimpleTest and I\'ve run into trouble. In my tests of a database class I want to be able to set an expectation for PHPs mysql f

4条回答
  •  有刺的猬
    2021-02-13 18:01

    In PHP 5.3+ environment you may workaround the need to use runkit extension by hacking the namespaces. The only requirement in that the function calls do not use fully-qualified namespace like \mysql_query() - and they usually don't. Then you can define the same function in your test, by defining the test in a namespace, and PHP will call your function instead of the global one. Personally I use this approach to stub the time() function call. Here is a nice example with the mockery framework

提交回复
热议问题