Summer Sale! All accounts are 50% off this week.

Hashim's avatar

Input and output passed to method are the same when using DataProvider:

I'm trying to set up a unit test to make sure that a method that transforms an input returns the output it should, and am doing this with a DataProvider. "8805" is the result returned when "0000" is passed to my decoder method, so both the values 0000 and 1111 - the rightmost column of the data - are being passed into the decoder used while seemingly ignoring the leftmost column entirely.

I'm pretty sure the error is in the array syntax for my data provider, but given that I have a few hundred values it isn't easy to see where, and I'd like to both a) understand exactly what I'm doing wrong and b) be sure that my tests are working exactly as I expect them to.

My unit test:

class DecoderTest extends TestCase
{
    /**
     * 
     * 
     * @dataProvider precomputedCodes
     * @return void
     */
    public function testDecoder($input, $output)
    {

        $manufacturer = new Manufacturer;
        $this->assertEquals($output, $manufacturer->decoder($input));
    }

    public function precomputedCodes() 
    {
        return [
            "precomputed_dataset" => [
            "V101" => "0000",           
            "X101" => "1111",
            // a few hundred more values        
            ],   
        ];
    }
}

The result of running php artisan test:

Warning: TTY mode is not supported on Windows platform.

   FAIL  Tests\Unit\DecoderTest
  ⨯ decoder with data set "precomputed_dataset"

  ---

  • Tests\Unit\DecoderTest > decoder with data set "precomputed_dataset"
  Failed asserting that two strings are equal.

  at D:\Users\CurrentUser\Documents\Projects\Websites\Project\tests\Unit\DecoderTest.php:24
     22▕
     23▕         $manufacturer = new Manufacturer;
  ➜  24▕         $this->assertEquals($output, $manufacturer->decoder($input));
     25▕     }
     26▕
     27▕     public function precomputedCodes()
     28▕     {

  1   D:\Users\CurrentUser\Documents\Projects\Websites\Project\vendor\phpunit\phpunit\phpunit:98
      PHPUnit\TextUI\Command::main()
  --- Expected
  +++ Actual
  @@ @@
  -'1111'
  +'8805'

  Tests:  1 failed
  Time:   0.04s
0 likes
5 replies
tykus's avatar

You need a value (array item) for each input to the test method:

public function precomputedCodes() 
{
    return [
        'precomputed_dataset' => [
            ["V101", "0000"],           
            ["X101", "1111"],
        ],   
    ];
}
Hashim's avatar

@tykus I had tried this but then it ends up failing with the error:

substr(): Argument #1 ($string) must be of type string, array given

for the following line in the method itself:

$input = strtoupper(substr($input, -4));
tykus's avatar
tykus
Best Answer
Level 104

@Hashim you don't need the key and wrapping array:

public function precomputedCodes() 
{
    return [
        ["V101", "0000"],           
        ["X101", "1111"],
    ];
}

1 like
Hashim's avatar

@tykus I was trying to future-proof it to work with different named datasets, but I suppose what I'm trying to do here doesn't work with that method and each array has to be considered a separate "dataset".

tykus's avatar

@Hashim correct.

You can named the individual datasets which will given better testdox output (although tedious to name a few hundred more values!)

public function precomputedCodes() 
{
    return [
        'something' => ["V101", "0000"],           
        'something else' => ["X101", "1111"],
        // ...
    ];
}
1 like

Please or to participate in this conversation.