在Kohana 3.2上运行PHPUnit时禁止请求 [英] Suppressing the request when running PHPUnit with Kohana 3.2

查看:91
本文介绍了在Kohana 3.2上运行PHPUnit时禁止请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Kohana 3.2中无法正确设置单元测试.

I'm having trouble correctly setting up unit testing in Kohana 3.2.

我安装了PHPUnit.我更改了引导程序以激活Kohana的unittest模块.我还更改了index.php文件,使其看起来像这样:

I installed PHPUnit. I changed the bootstrap to activate Kohana's unittest module. I also changed the index.php file to look like this:

if ( ! defined('SUPPRESS_REQUEST'))
{
    echo Request::factory()
        ->execute()
        ->send_headers()
        ->body();
}

我在应用程序文件夹中创建了一个文件夹测试.在其中,我插入了一个如下所示的phpunit.xml文件:

I created a folder tests in my application folder. In it, I inserted a phpunit.xml file that looks like this:

<phpunit colors="true" bootstrap="../../index.php">
<testsuites>
    <testsuite name="Kohana Tests">
        <directory>./</directory>
    </testsuite>
</testsuites>

我遇到两个问题(第一个是我确实需要回答的问题):

I am having two problems (the first one is the one I really need an answer to):

1-当我从命令行进入测试并尝试运行phpunit时,似乎SUPPRESS_REQUEST从未设置为true.该请求已执行,因此没有运行测试.我能够成功运行测试的唯一方法是暂时注释掉index.php中的整个Request :: factory()行.有谁知道如何解决这个问题?如果我应该在某个地方添加一个define('SUPPRESS_REQUEST',true),我应该在哪里做?我是Kohana和PHPUnit的新手.

1- When I go into tests from the command line and try running phpunit, it seems that SUPPRESS_REQUEST never gets set to true. The Request is executed, and therefore no tests are run. The only way I am able to run the tests successfully is to momentarily comment out the entire Request::factory() line in index.php. Does anyone know how to get around this problem? If I should be adding a define('SUPPRESS_REQUEST', true) somewhere, where should I do it? I'm new to Kohana and PHPUnit.

2- PHPUnit抱怨未设置变量REMOTE_ADDR.

2- PHPUnit complains that the variable REMOTE_ADDR is not set.

PHP Notice:  Undefined index: REMOTE_ADDR in
/Users/**/Sites/root/application/bootstrap.php on line 76

由于尽管如此,尽管测试似乎仍在运行,但实际上这并不是一个大问题,但我想知道是否有人知道我是否应该在特定位置设置此变量.

This is actually not a huge problem for now as tests still seem to run in spite of this, but I'm wondering if anyone knows if I should be setting this variable somewhere in specific.

推荐答案

在modules/unittest中,有一个名为bootstrap.php的文件,可以很好地与phpunit配合使用.

In modules/unittest there is a file called bootstrap.php which works perfectly well with phpunit.

引用该引导程序的phpunit.xml是这样的:

My phpunit.xml which references that bootstrap is this:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="../../modules/unittest/bootstrap.php">
    <testsuites>
        <testsuite name="Kohana Tests">
            <directory>./</directory>
        </testsuite>
    </testsuites>
</phpunit>

另外,对于REMOTE_ADDR问题,当phpunit运行PHP的CLI版本时,我认为它无法访问REMOTE_ADDR变量.如果您查看unittest中的引导程序,它不会使用与http相关的全局变量.

Also, for the REMOTE_ADDR problem, when phpunit is running the CLI version of PHP, which I don't think has access to a REMOTE_ADDR variable. If you look at the bootstrap from unittest, it does not use http related globals.

我不确定为什么您必须在引导程序中运行Request :: factory代码.在我的原始3.2安装上,Request :: factory代码位于index.php中,而不是bootstrap.php中,并且没有对SUPRESS REQUEST的引用.您可能在3.2之前的版本中有一些挥之不去的文件,需要清洁.

I'm not sure why you have to run Request::factory code in your bootstrap. On my vanilla 3.2 install, the Request::factory code lives in index.php and not bootstrap.php and does not have any reference to SUPRESS REQUEST. You may have some lingering files from a pre-3.2 installation which need cleaning.

这篇关于在Kohana 3.2上运行PHPUnit时禁止请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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