PHPUnit + CodeIgniter multiple objects with same name

依然范特西╮ 提交于 2019-12-12 03:15:56

问题


I currently test my CodeIgniter app with phpunit by using CIUnit (https://bitbucket.org/kenjis/my-ciunit). The problem is that I have multiple controllers with the same name. I have a controller in the root controller directory named "Blog" and I have a controller called "Blog" in the controller/ajax/ directory. The reason is to seperate all ajax requests from the main controller.

When I am running tests on both files, I get the following error:

PHP Fatal error: Cannot redeclare class Blog in ...

Well, I am not suprised I am getting this error.

What are my options to resolve this?

  1. Prefix controllers in ajax directory with "ajax" (looks only a bit stupid url/ajax/ajax_blog)
  2. Use namespaces (I guess I need to namespace codeigniter too then)
  3. Create 3 seperate phpunit.xml files

This aren't really solutions I am looking for. Do I have any other options? Is it somehow possible to run each testsuite seperatly, but still in one command? Can I "clean" objects between testsuites? Anything else?


回答1:


There are no other options except those you mentioned, as it is impossible to "unload" class definitions in PHP.

Naming two controllers the same is not a problem when you run CI normally, since only one controller is instantiated per request, but something that should be avoided.

If it is just the ajax-url you don't like, maybe override it in a route (in config/routes.php):

$routes['ajax/blog'] = 'ajax/ajax_blog';


来源:https://stackoverflow.com/questions/10314014/phpunit-codeigniter-multiple-objects-with-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!