Cómo probar las variables de clase privadas/protegidas cuando se establecen como tipo predeterminado.
Este es un ejemplo simplificado, entienda el problema.
El código funciona, tengo un problema cuando quiero probar con phpUnit.
IBase.php
namespace interfaces;
interface IBase {}
FileNavigate.php
class FileNavigate {
private IBase $f3; // <= línea: 5 #RED
public function __construct(IBase $f3, $file = '') { // <= #GREEN use IBase
$this->f3 = $f3;
}
}
FileNavigateTest.php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class FileNavigateTest extends TestCase {
public function testInterface() {
$mock = $this->createMock(\interfaces\IBase::class);
$f3getFileNavigate = new FileNavigate($mock); // <= línea: 10
$this->assertTrue(true);
}
}
cmd:
PHPUnit 8.5.21 por Sebastian Bergmann y colaboradores.
..E... 1 / 1 (100%)
Tiempo: 8 ms, Memoria: 4.00 MB
Hubo 1 error:
1) FileNavigateTest::testInterface
ParseError: error de sintaxis, unexpected 'IBase' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
/html/app/v2/FileNavigate.php:5
/html/tests/FileNavigateTest.php:10
ERRORES!
Tests: 1, Afirmaciones: 1, Errores: 1.
Sé que el error está en el archivo “FileNavigate.php” en la línea 5.
Solo mi IDE funciona mejor cuando uso esta notación.
¿Alguien sabe cómo probarlo?
La clase es para verificar si el objeto que recibe tiene esta interfaz, el mock reemplaza una clase que ya tiene esta interfaz.
davy.ai
El problema con el código es que la declaración de tipo para la propiedad
$f3
dentro de la claseFileNavigate
está usando la interfazIBase
sin importarla o cualificarla completamente. Esto causa unParseError
cuando se intenta ejecutar el test PHPUnit, ya que no se puede resolver el tipoIBase
.Para solucionar este problema, puedes importar la interfaz
IBase
en la claseFileNavigate
:O cualificar completamente el tipo
IBase
en el constructor:Cualquiera de estas opciones debería solucionar el
ParseError
y permitirte ejecutar el test PHPUnit.