PHPUnit的几个麻烦

时间:2012-10-22 10:37:58

标签: phpunit kohana

我有两个控制器。它们的第一条路径是controller / news.php,第二条路径是controller / admin / news.php。我有每个控制器的路由。

对于控制器/ admin / news.php我有

Route::set('news-admin', 'admin/news(/)', array('start' => '\d+'))
    ->defaults(array(
        'directory' => 'admin',
        'controller' => 'news',
        'action' => 'index',
        'start' => 0,
    ));

对于controller / news.php:

Route::set('news', 'news(/)', array('start' => '\d+'))
    ->defaults(array(
        'controller' => 'news',
        'action' => 'index',
        'start' => 0,
    ));

当我使用浏览器时,所有工作都可以。当我打电话给

  

$ response = Request :: factory('/ news') - > execute()

单位测试路线,测试运行。但是当我打电话给

  

$ response = Request :: factory('admin / news') - > execute()

我只收到下一条消息

PHPUnit 3.7.8 by Sebastian Bergmann. 
Configuration read from /home/mydir/Projects/www/kohsite/application/tests/phpunit.xml

经过多次实验,我明白我无法测试路径是否包含放置在子文件夹中的控制器的“目录”。

下面我展示了我的phpunit.xml

<phpunit bootstrap="bootstrap.php" colors="true">
    <testsuite name="ApplicationTestSuite">
      <directory>./classes</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../tests</directory>
            <exclude>
                <directory suffix="*">../cache</directory>
                <directory suffix="*">../config</directory>
                <directory suffix="*">../logs</directory>
                <directory suffix=".php">../views</directory>
                <file>../bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

2 个答案:

答案 0 :(得分:1)

我假设您正在使用Kohana 3.x?我不确定您的应用程序设置如何,但是当我设计一个具有管理控制器的站点时,我通常会创建一个不在子文件夹中的管理控制器。默认路由可以处理对http://domain.com/<controller>/<action>/<id&gt;的任何请求。例如http://domain.com/admin/index

如果我想专门为管理新闻设置控制器,我会创建一个名为“admin”的文件夹,并设置如下控制器定义:

class Controller_Admin_News extends Controller_Admin {

然后我会在bootstrap.php中写一个看起来像这样的路径:

Route::set('admin_news', 'admin/news(/<action>(/<id>))')
    ->defaults(array(
        'controller' => 'admin_news',
        'action'     => 'index'
    ));

尝试设置您的应用,看看它是否有帮助。

答案 1 :(得分:0)

当然,我正在使用Kohana 3.2。我有控制器

class Controller_Admin_News extends Controller_Admin_Common