1
I created this script for testing purposes, and I have a class Calculadora
which takes a class instance as a parameter Operacao
, when performing the unit test passing the class the test occurs successfully.
But when replacing the class Operacao
by a mock
get an error
stating that the class is not of the desired type. How to pass one mock
of that class?
Calculator.php
<?php
namespace Application;
use Application\operacao;
class Calculadora
{
private $num;
private $num2;
private $operacao;
private $resul;
public function __construct(operacao $operacao, int $num, int $num2)
{
$this->operacao = $operacao;
$this->num = $num;
$this->num2 = $num2;
}
public function Somar()
{
return $this->num + $this->num2;
}
}
php operation.
namespace Application;
class operacao
{
private $operacao;
public function __construct(String $opc)
{
$this->operacao = $opc;
}
public function getOperacao()
{
return $this->operacao;
}
}
Phptest
use PHPUnit_Framework_TestCase as PHPUnit;
use Application\Calculadora;
use Application\operacao;
class PHPTest extends PHPUnit
{
public function setUp()
{
$this->opc = $this->getMockBuilder('operacao')
->getMock();
//$this->opc = new operacao('Soma');
$this->Calculadora = new Calculadora($this->opc, 1 , 2);
}
public function tearDown()
{
}
public function testOperacaoMatematica()
{
$this->assertEquals(3, $this->Calculadora->Somar());
}
}
I made a minimal example take a look @David
– novic