Bonjour,

Le contexte est l'utilisation de PHPUnit (9.1.4).

Mes tests PHPUnit sur des class simples fonctionnent très bien.

Mais quand je place à d'autres class, j'ai ce message : PHP Notice: Undefined index: SERVER_NAME
A priori cela vient de la ligne $this->Serveur_Nom = $_SERVER['SERVER_NAME'];

J'ai fait le test avec une class simplissime avec $this->Serveur_Nom = $_SERVER['SERVER_NAME']; dans le construct, et pareil,
PHP Notice: Undefined index: SERVER_NAME

Existe-t-il un moyen de gérer cette erreur ?

Les codes :
test2.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
require 'Math.php';
 
use PHPUnit\Framework\TestCase as TestCase;
// sometimes it can be
// use PHPUnit\Framework\TestCase as TestCase;
 
class Test2 extends TestCase{
    public function testFibonacci() {
        $math = new Math();
        $this->assertEquals(34, $math->fibonacci(9));
    }
 
    public function testFactorial() {
        $math = new Math();
        $this->assertEquals(120, $math->factorial(5));
    }
 
    public function testFactorialGreaterThanFibonacci() {
        $math = new Math();
        $this->assertTrue($math->factorial(6) > $math->fibonacci(6));
    }
}
math.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php    
class Math {
	function __construct()
	{
		$this->Serveur_Nom = $_SERVER['SERVER_NAME'];
	}
    public function fibonacci($n) {
        if (is_int($n) && $n > 0) {
            $elements = array();
            $elements[1] = 1;
            $elements[2] = 1;
            for ($i = 3; $i <= $n; $i++) {
                $elements[$i] = bcadd($elements[$i-1], $elements[$i-2]);
            }
            return $elements[$n];
        } else {
            throw new 
                InvalidArgumentException('You should pass integer greater than 0');
        }
    }
 
    public function factorial($n) {
        if (is_int($n) && $n >= 0) {
            $factorial = 1;
            for ($i = 2; $i <= $n; $i++) {
                $factorial *= $i;
            }
            return $factorial;
        } else {
            throw new 
                InvalidArgumentException('You should pass non-negative integer');
        }
    }
}
?>