2

I am trying to write a phpunit test so I can test that I'm getting the correct assertion, this test i currently have which is passing and works as intended.

/**
@test
*/
  $filmInfo = $this->postRequest();

        $this->assertFilm($this->requestData, $filmInfo['id']);

        $film = Film::findOrFail($filmInfo['id']);

        $this->assertEquals('LOTR', $filmInfo['name']);
        $this->assertEquals('Film about a ring', $filmInfo['description']);
        $this->assertEquals('Fantasy', $filmInfo['genre']['main']);
        $this->assertEquals('Adventure', $filmInfo['genre']['sub']);
    }

This is the request data array it is referring to:

    private function requestData(): array
    {
        return [
            'name' => 'LOTR',
            'description' => 'Film about a ring',
            'main' => 'Fantasy',
            'sub' => 'Adventure',
        ];
    }

This test works fine and it passing but I want to test it within one assertion like so:

  $this->assertEquals([
            'name' => 'LOTR',
            'description' => 'Film about a ring',
            'genre' => [
                'main' => 'Fantasy',
                'sub' => 'Adventure'
            ]
            ,
        ], $filmInfo);

But I keep getting an error that the 2 arrays I'm asserting are not matching, do you guys have an idea on what could be causing this?

1
  • Does $filmInfo have more data in it? Can you dump/post it? Commented Oct 20, 2019 at 16:17

1 Answer 1

3

Just like what the PHPUnit said, your array aren't matching. Your array should match with all values in $filmInfo array.

From your code, we can guessing that you aren't comparing the id. Maybe you can try this code:

$this->assertEquals(array_merge($filmInfo, [
    'name' => 'LOTR',
    'description' => 'Film about a ring',
    'genre' => [
        'main' => 'Fantasy',
        'sub' => 'Adventure'
    ],
]), $filmInfo);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.