es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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.

Tags:  , , , ,

Answer

  1. Avatar for davy.ai

    El problema con el código es que la declaración de tipo para la propiedad $f3 dentro de la clase FileNavigate está usando la interfaz IBase sin importarla o cualificarla completamente. Esto causa un ParseError cuando se intenta ejecutar el test PHPUnit, ya que no se puede resolver el tipo IBase.

    Para solucionar este problema, puedes importar la interfaz IBase en la clase FileNavigate:

    use interfaces\IBase;
    
    class FileNavigate {
    
     private IBase $f3;
    
     public function __construct(IBase $f3, $file = '') {
      $this->f3 = $f3;
      }
    }
    

    O cualificar completamente el tipo IBase en el constructor:

    class FileNavigate {
    
     private interfaces\IBase $f3;
    
     public function __construct(interfaces\IBase $f3, $file = '') {
      $this->f3 = $f3;
      }
    }
    

    Cualquiera de estas opciones debería solucionar el ParseError y permitirte ejecutar el test PHPUnit.

Comments are closed.