1
votes

I'm trying to write a test for a method in the class below. However, when I run the test I get the error that get_b64 is never run? I don't see how this is not running.

I've had a little look into the mockery documentation for testing static methods, but as far as I can tell this error isn't due to that?

What do I need to change with my testing strategy or be able to mock the function call in the mocked object?

Class:

namespace App\Services\Steam;

use App\Services\Steam\Utils;

class Steam
{
    public function profile(string $steamID)
    {
        $b64 = Utils::get_b64($steamID);

        if ($b64 === null) {
            throw new \App\Exceptions\InvalidSteamId();
        }

        return new Profile($b64);
    }   
}

TestCase:

public function test_create_user_object()
{   
    $id = "123"
    $utilsMock  = Mockery::mock(\App\Services\Steam\Utils::class);

    $utilsMock->shouldReceive('get_b64')
                ->once()
                ->with($id)
                ->andReturn($id);

    $steam = new \App\Services\Steam\Steam();
    $steam->profile($id);
}
1

1 Answers

0
votes

You call get_b64 statically, which means it is called from the class, not an object.

To mock such calls you need to use aliases:

public function test_create_user_object()
{   
    $id = "123"
    $utilsMock  = Mockery::mock('alias:\App\Services\Steam\Utils');

    $utilsMock->shouldReceive('get_b64')
                ->once()
                ->with($id)
                ->andReturn($id);

    $steam = new \App\Services\Steam\Steam();
    $steam->profile($id);
}

Bear in mind that it completely replaces the Utils class, so if you have more static functions called from the class, you need to mock them as well.