0
votes

I'm learning phpunit tests implementation, my problem, is that I don't know how to test the case where the if condition is not true, so it doesn't define the $view->somethingSemantic array, then, how can I mock this PHPUNIT test to make it pass?

Should I test both scenarios in one test? or should I split in two... The documentation is so simple but can't find any reference to a case alike.

Now I get a error - Undefined property for #case 2 Test #2 fails, all the time, or it shows the Love semantics value compared vs the first array Love semantics as if they were supposed to match to pass the test?

// this is the function I'm creating the UT.
protected function _doSomeMagicPlease( $view ) {
if($this->config->DoSomeMagicPlease){
    $view->somethingSemantic = array(
        "Something else semantic" => $this->lsd(),
        "Love semantics" => $this->magic()
    );
}
}

// assertion - the view->somethingSemantic does not exist in case 2. 

$this->assertEquals($expected , $view->somethingSemantic);

it seems it is comparing both arrays of the data provider:

// this is my data provider:

public function checkDoSomeMagicPleaseDataProvider()
    {
    return [
            [
            'message' => 'Case 1',
            'configObject' => (object) array(
                'values' => (object) array(
                    'DoSomeMagicPlease'=> 'true'
                    )
                ),
                'expected' => array(
                    "Something else semantic" => "12345",
                    "Love semantics" => "https://www.someurl.com"
                ),
            ],
            [
            'message' => 'Case 2',
            'configObject' => (object) array(
                'values' => (object) array(
                    'DoSomeMagicPlease'=> "false"
                    )
                ),
                'expected' => array(
                    "Something else semantic" => "12345",
                    "Love semantics" => "false"
                )
            ]
        ];
    }

Should I change my assertion? any hint is appreciated !!

1

1 Answers

1
votes

One easy way to handle this, is to make sure somethingSemantic is always defined. null or an empty array seem like a reasonable default value.

    public function checkDoSomeMagicPleaseDataProvider()
    {
        return [
            // ...
            [
                'message'      => 'Case 2',
                'configObject' => (object)array(
                    'values' => (object)array(
                        'DoSomeMagicPlease' => 'false'
                    )
                ),
                'expected'     => null
            ]
        ];
    }

If you want to keep it the way it is, split it into two separate tests. You can check whether the somethingSemantic attribute exists on your view object:

    public function test_it_should_create_something_semantic_when_magic_is_enabled()
    {
        // ...
        
        self::assertEquals(
            [
                'Something else semantic' => '12345',
                'Love semantics'          => 'https://www.someurl.com'
            ],
            $view->somethingSemantic
        );
    }

    public function test_it_should_not_create_something_semantic_when_magic_is_enabled()
    {
        // ...
        
        self::assertObjectNotHasAttribute('somethingSemantic', $view);
    }