PHPUnit + CodeIgniter具有相同名称的多个对象 [英] PHPUnit + CodeIgniter multiple objects with same name

查看:74
本文介绍了PHPUnit + CodeIgniter具有相同名称的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前通过使用CIUnit(https://bitbucket.org/kenjis/my-ciunit)与phpunit测试我的CodeIgniter应用.问题是我有多个具有相同名称的控制器.我在根控制器目录中有一个名为"Blog"的控制器,在controller/ajax/目录中有一个名为"Blog"的控制器. 原因是将所有ajax请求与主控制器分开.

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致命错误:无法在...中重新声明类Blog

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

好吧,我不惊讶我遇到了这个错误.

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

我有什么解决方案?

  1. 在带有"ajax"的ajax目录中添加前缀控制器(仅看起来有点愚蠢的url/ajax/ajax_blog)
  2. 使用名称空间(我想我也需要使用名称空间codeigniter)
  3. 创建3个单独的phpunit.xml文件

这不是我要找的解决方案.我还有其他选择吗?是否可以以某种方式单独运行每个测试套件,但仍然可以在一个命令中运行?我可以在测试套件之间清理"对象吗?还有吗?

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?

推荐答案

除了您提到的选项外,没有其他选择,因为不可能在PHP中卸载"类定义.

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

正常运行CI时,将两个控制器命名为相同不是问题,因为每个请求仅实例化一个控制器,但应避免这种情况.

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.

如果这只是您不喜欢的ajax网址,则可以在路由中(在config/routes.php中)覆盖它:

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';

这篇关于PHPUnit + CodeIgniter具有相同名称的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆