在phpUnit中伪造$ _SERVER变量

时间:2018-11-13 12:42:27

标签: php phpunit

我还是phpUnit的新手,我无法(非常简单)进行测试。

<?php

use PHPUnit\Framework\TestCase;

class userTest extends TestCase {
    public function testTrue() {
        $this->assertTrue(true);

        //  This line wont work without autloader.php
        $user = new User();
    }
}

问题是我需要从autoloader.php加载所有类:

<?xml version="1.0" encoding="UTF-8"?>
    <phpunit bootstrap="inc/autoload.php"></phpunit>

但是我在那些类中有很多$ SERVER变量,例如

$_SERVER['REMOTE_ADDR']
$_SERVER['HTTPS']
$_SERVER['SERVER_NAME']
$_SERVER['DOCUMENT_ROOT']
$_SERVER['REQUEST_URI']

这是我得到的错误:

Notice: Undefined index: REMOTE_ADDR in /Applications/MAMP/htdocs/sakkadentrainer/classes/App.php on line 674

如何使这些变量起作用?我宁愿从phpunit.xml文件中将它们“伪装”为env.variables,但我不知道是否可行。

感谢您的帮助!

我的设置:     php 7.1.2,phpUnit 6.1.1,macOs Mojave,MAMP

解决方案https://phpunit.de/manual/6.5/en/appendixes.configuration.html#appendixes.configuration.php-ini-constants-variables

1 个答案:

答案 0 :(得分:2)

参考文档,您可以Setting PHP INI settings, Constants and Global Variables,例如:

{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}
{
    "outcome" => "failed",
    "failure-description" => "WFLYCTL0211: Cannot resolve expression '${httpPort}'",
    "rolled-back" => true,
    "response-headers" => {"process-state" => "reload-required"}
}

希望获得帮助