dousha2020 2016-11-30 16:47
浏览 49
已采纳

遗留环境中的PHPUnit

I'm starting to setup PHPUnit (v4.8) to be used in my 'legacy' code (it's not so legacy, but it have bad programming practices).

The structure of my folders is as follows

/home
  -phpunit.xml
  /folder1
  /folder2
  /folder3
  /vendor
  /tests
   -Test1.php
  /includes
   -functions.php
   /libs
    -User.php
    -TableClass.php
    ....

functions.php

<?php //require_once $_SERVER['DOCUMENT_ROOT'] . "/home/vendor/autoload.php" ; require_once $_SERVER['DOCUMENT_ROOT'] . "/home/includes/libs/table_classes/User.php" ; ?>

I have commented that line, because I think composer automatically loads it. Question 1, Am I Rigth? (because phpunit get automatically recognized inside my Test class...)

Test1.php

<?php

class Test1 extends PHPUnit_Framework_TestCase
{
  public function testSomething()
  {
    // $something = getColNameByStatusId(1);
    $this->assertEquals(1,2);
  }
}
?>

phpunit.xml

<phpunit bootstrap="includes/functions.php" colors="true">
    <testsuite name="Test1" >
        <directory>./tests</directory>
    </testsuite>
</phpunit>

Then I Execute phpunit in command line

My functions.php works fine in my code, of course with no composer integration, but when It's loaded with phpunit it 'breaks', I get the following error:

Warning: require_once(/home/includes/libs/table_classes/User.php): failed to open stream: No such file or directory in C:\wamp\www\home\includes\functions.php on line 18

I think I'm missing the 'loading' stuff for phpunit. My code doesn't use namespaces and PSR-0 neither PSR-4.

Question 2- How to properly load files in this case?

My goal is to load functions.php then it will load all other 'table' classes for doing my tests

  • 写回答

2条回答 默认 最新

  • dqkv0603 2016-11-30 19:30
    关注

    Replace $_SERVER['DOCUMENT_ROOT'] with __DIR__ and adjusted the paths accordingly, and everything worked fine.

    PHPUnit does not set $_SERVER['DOCUMENT_ROOT'] so It was not finding my files. Apache's do that. So the CLI of PHPUnit couldn't find it.

    Hope it helps someone else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记