How to perform mock in a class?

Asked

Viewed 353 times

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

Even after making the changes the following occurred :inserir a descrição da imagem aqui

  • I made a minimal example take a look @David

1 answer

1


It lacked calling the method ->getMock();, 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);

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

Testing:

inserir a descrição da imagem aqui

The test was developed in PHP version 7.0 and passed the 4 tests and the settings of Mock are correct.

Reference:

  • had already tried with the "getMock", as here in the documentation : https://phpunit.de/manual/current/pt_br/test-doubles.html ! but without success. Regarding the second treatment the method "createMock" is being accused as undefined :/.

  • when performing the first way I get the following error message "__Construct() must be an instance of Application operation, instance of Mock_operacao_11020db9 Given"

  • Maybe @Davidsantos the error is higher than these two lines, if you have how to put the two classes in your question ???

  • the code is pretty crude, but it’s okay.

  • Your operation class has builder with parameter should be that I made an edit take a look?

  • then I made the change and the result was : Reflectionexception: Class Mock_operacao_d23b9f26 does not have a constructor, so you cannot pass any constructor Arguments

  • maybe it has something to do with the outdated version, I am using the 3.7, I will update!

  • @Davidsantos I wrote again the method is a little different the thing when it is with Mock!

  • made the changes as you posted, actually copied and pasted the code, but did not solve, posted a print

  • @Davidsantos I did the example, changed code you really copied in full? I think it is a punctual problem...

  • it just looked different my imports in the test class, but the rest I copied everything.

  • @Davidsantos the problem seems to me local!

  • 1

    is you were right, after redoing everything the test worked, thank you very much!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.