2

I'm still new to phpUnit and I can't make my (very simple) test work.

<?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();
    }
}

The problem is that I need to load all my classes from autoloader.php:

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

But I'n those classes I have a lot of $SERVER variables, like

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

This is the error I get:

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

How can I make those variables work? I would prefer to kind of "fake" them as env.variables from the phpunit.xml file, but I don't know if that's possible.

Thanks for you help!

My Setup: php 7.1.2, phpUnit 6.1.1, macOs Mojave, MAMP

SOLUTION: https://phpunit.de/manual/6.5/en/appendixes.configuration.html#appendixes.configuration.php-ini-constants-variables

B. Rentrug
  • 103
  • 1
  • 11

1 Answers1

6

Referring to the Doc, you can Setting PHP INI settings, Constants and Global Variables, as example:

<php>
  <server name="REMOTE_ADDR" value="127.0.0.1"/>
</php>

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • 1
    Perfect! Thanks – B. Rentrug Nov 13 '18 at 14:53
  • Something else: I now get this error: Error: Class 'NumberFormatter' not found. Its due to an unloaded php extension (php_intl.dll). I tried to add it in phpunit.xml: But it does not work... – B. Rentrug Nov 13 '18 at 18:03
  • Probably you should add it in the php.ini – Matteo Nov 13 '18 at 19:16
  • which php.ini? the one from MAMP or does phpUnit has an own php.ini? – B. Rentrug Nov 14 '18 at 20:02
  • check this answer https://stackoverflow.com/a/30047099/2270041 in order to find the correct files to edit – Matteo Nov 14 '18 at 20:12
  • Thanks, but I can't locate my php.ini. `php -i | grep 'php.ini'` return /etc. But in /etc I only have a php.ini.default file. From phpUnit and its phpinfo I know that `post_max_size` is 8MB. So i tried to find the php.ini file with this value. I found one in `/usr/local/etc/php/5.6/php.ini` but the changes in this file, didn't affect phpunti... I'm lost ;) – B. Rentrug Nov 15 '18 at 08:01
  • What a pain... I found the solution here: https://stackoverflow.com/a/40516071/2839368. I needed to copy the php.ini.default file to etc/ and rename it as php.ini – B. Rentrug Nov 15 '18 at 08:22