How to check if the class was instantiated via Dataprovider using PHP Unit

Asked

Viewed 769 times

0

Could someone make an example? Considering my class below:

class MinhaClasse
{

    private $param;

    public function __construct(array $params = []) {
       
      $this->param = $params;
    
    }

} 

I have my test class:

require_once "MinhaClasse.php";

class MinhaClasseTest extends PHPUnit_Framework_TestCase
{

    /**
     * @dataProvider additionProvider
     */
  
    public function additionProvider()
    {
        return array(
          array('?????', null, null),
          array(0, 1, 1),
          array(1, 0, 1),
          array(1, 1, 3)
        );
    }

    public function testShouldMyClassIsInstantiate($instance, $value, $expected)
    {
        $this->assertEquals($expected, $value);
    }   
}

2 answers

1

A method DataProvider returns an array of arrays or an object that implements the interface Iterator. The test method will be called with the matrix content and its arguments.

Some key points using Dataprovider:

  • Dataprovider method must be public.
  • Dataprovider will return a array data.
  • Use test annotation(@dataProvider) to declare your dataProvider method.

It’s really very simple to use dataProvider. First let’s create a new public method, which returns a data set matrix as arguments of the test method. So we added an annotation to the test method to say that PHPUnit will provide the arguments.

  <?php
require 'Calculator.php';

class CalculatorTests extends PHPUnit_Framework_TestCase
{
    private $calculator;

    protected function setUp()
    {
        $this->calculator = new Calculator();
    }

    protected function tearDown()
    {
        $this->calculator = NULL;
    }

    public function addDataProvider() {
        return array(
            array(1,2,3),
            array(0,0,0),
            array(-1,-1,-2),
        );
    }

    /**
     * @dataProvider addDataProvider
     */
    public function testAdd($a, $b, $expected)
    {
        $result = $this->calculator->add($a, $b);
        $this->assertEquals($expected, $result);
    }

 }

Source

Handbook

  • Thanks for the @Lollipop reply, but my question is not how to use dataProvider, what I need to know is how I could do a test that would answer me if the class tested by dataProvider was instantiated through PHP Unit, maybe put something like instanceof ...

0


I ended up arriving at the solution alone, to test an instance just do the following, as described in the documentation: https://phpunit.de/manual/current/pt_br/appendixes.assertions.html#appendixes.assertions.assertInstanceOf

require_once "MinhaClasse.php";

class MinhaClasseTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider additionProvider
     */
    public function additionProvider()
    {
        return array(
          array('MinhaClasse', new MinhaClasse()),
        );
    }

    public function testShouldMyClassIsInstantiate($expected, $instance)
    {
      $this->assertInstanceOf($expected, $instance,'The Class "{$expected}" can\'t be instantiated'); 
    }   
}

Browser other questions tagged

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