How to perform mock in a class?

1

I created this script for testing purposes, and I have a Calculadora class that receives as an instance an instance of class Operacao , when I perform the unit test passing the class the test occurs successfully.

But when I replace the class Operacao with a mock I get an error stating that the class is not of the desired type . How do I pass%% of this 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;
    }

}

operacao.php

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

Even after making the changes, the following occurred:

    
asked by anonymous 15.06.2017 / 01:29

1 answer

1

The ->getMock(); method was missing, such as specified in this tutorial link .

Example:

$opc = "string";
$this->opc = $this->getMockBuilder('operacao')
                  ->setConstructorArgs([$opc])
                  ->getMock();

$this->Calculadora = new Calculadora($this->opc, 1 , 2);

Minimum Example:

Classes:

<?php namespace Application;

class operacao
{
    private $operacao;
    public function __construct(string $opc)
    {
        $this->operacao = $opc;
    }

    public function getOperacao()
    {
        return $this->operacao;
    }
}
<?php namespace Application;

class calculadora
{
    private $operacao;
    public function __construct(operacao $operacao)
    {   
        $this->operacao = $operacao;
    }
    public function somar(int $num1, int $num2): int
    {
        return ($num1 + $num2);
    }
    public function subtrair(int $num1, int $num2): int
    {
        return ($num1 - $num2);
    }
}

Test class:

<?php

require "vendor/autoload.php";
require "t1.php";
require "t2.php";

class PHPTest extends \PHPUnit\Framework\TestCase
{
    protected $calculadora;
    protected $opc;

    public function setUp()
    {
        $this->opc = $this->getMockBuilder(\Application\operacao::class)
            ->setConstructorArgs([""])
            ->getMock();

        $this->calculadora = $this->getMockBuilder(\Application\calculadora::class)
            ->setConstructorArgs([$this->opc])
            ->setMethods(null)
            ->getMock();

    }

    public function tearDown()
    {
    }

    public function testOperacaoMatematicaSomaEquals()
    {
        $this->assertEquals(4, $this->calculadora->somar(2,2));
    }

    public function testOperacaoMatematicaSomaNotEquals()
    {
        $this->assertNotEquals(1, $this->calculadora->somar(2,2));
    }

    public function testOperacaoMatematicaSubtrairEquals()
    {
        $this->assertEquals(-1, $this->calculadora->subtrair(1,2));
    }

    public function testOperacaoMatematicaSubtrairNotEquals()
    {
        $this->assertNotEquals(1, $this->calculadora->subtrair(1,2));
    }
}

Test:

  

ThetestwasdevelopedinPHPversion7.0andpassedthe4testsandtheMocksettingsarecorrect.

Reference:

15.06.2017 / 01:33