Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stderr33's avatar

How to mock a function dependencies

How can I mock the dependencies of this function ?

public function login(string $email, string $password, string $ipAddress): array
    {
        $login = $this->client->login(null, [
            'applicationId' => config("fusionauth.appid", "..."),
            'ipAddress' => $ipAddress,
            'loginId' => $email,
            'password' => $password
        ]);
        
        if(!$login->wasSuccessful()) {
            return ['result' => $login->errorResponse, 'success' => false, 'status' => $login->status];
        }
        return ['result' => $login->successResponse, 'success' => true, 'status' => $login->status];
    }

I wanna mock the auth client but it's giving me this error:

Error: Call to undefined method stdClass::wasSuccessful()

0 likes
14 replies
Sinnbeck's avatar

Show your test with the mock as well :)

1 like
stderr33's avatar

@sinnbeck

public function is_authentication_working()
    {
        // client mock
        $mock = $this->getMockBuilder(FusionAuthClient::class)
        ->setConstructorArgs([$this->config['apiKey'], $this->config['baseUrl']])
        ->getMock();
         
        $mock->expects($this->once())
            ->method('login')
            ->with(null, [
                'applicationId' => $this->config['appId'],
                'loginId' => '[email protected]',
                'password' => 'password',
                "ipAddress" => "1.1.1.1"
            ])
            ->willReturn((object) ['status' => 200]);
        

        $faLaravel = new Auth($mock, User::class);

        //register the user first...
        $faLaravel->register(email: '[email protected]', password: 'password');

        // then login...
        $auth = (object) $faLaravel->login(email: '[email protected]', password: 'password', ipAddress: "1.1.1.1");
        $this->assertEquals(200, $auth->status);
    }
Sinnbeck's avatar

@stderr33 so I assume $this->client is the mock? You need to add $mock->expects for all calls made

$mock->expects($this->once())
            ->method('wasSuccessful ')
            ->andReturn(true);
stderr33's avatar

@Sinnbeck

this error showed up:

PHPUnit\Framework\MockObject\MethodCannotBeConfiguredException: Trying to configure method "wasSuccessful" which cannot be configured because it does not exist, has not been specified, is final, or is static

This is after I added your solution and added this in line 53:

 ->addMethods(['wasSuccessful'])
Sinnbeck's avatar

@stderr33 hmm yeah if you added onlyMethods it should work. Weird that login does not trigger the error

Sinnbeck's avatar

Can you show the complete updated test?

stderr33's avatar

@Sinnbeck

I added this code:

$faClientMock = $this->getMockBuilder(FusionAuthClient::class)
        ->onlyMethods(['login'])
        ->addMethods(['wasSuccessful'])
        ->setConstructorArgs([$this->config['apiKey'], $this->config['baseUrl']])
        ->getMock();

It went back to the first error.

Sinnbeck's avatar

@stderr33 does fusion auth not have wasSuccessful? I would expect it to be

$faClientMock = $this->getMockBuilder(FusionAuthClient::class)
        ->onlyMethods(['login', 'wasSuccessful'])
        ->setConstructorArgs([$this->config['apiKey'], $this->config['baseUrl']])
        ->getMock();
stderr33's avatar

it does have it but not directly. If you can see the code above, the method login returns an object that has the method wasSuccessful. I guess this is the source of the issue but the question is, How do I get around this error? do I have to mock the object that login() returns ?

public function login(string $email, string $password, string $ipAddress): array
    {
        $login = $this->client->login(null, [
            'applicationId' => config("fusionauth.appid", "..."),
            'ipAddress' => $ipAddress,
            'loginId' => $email,
            'password' => $password
        ]);
        
        if(!$login->wasSuccessful()) {
            return ['result' => $login->errorResponse, 'success' => false, 'status' => $login->status];
        }
        return ['result' => $login->successResponse, 'success' => true, 'status' => $login->status];
    }
Sinnbeck's avatar

@stderr33 ah ok. I assumed it returned $this. Then yes you need to return a mock in the login mock. Now that I reread the code it makes sense :) sorry for the confusion

1 like
stderr33's avatar

@Sinnbeck I solved it. I created a fake class that emulates the client response and added it to willReturn() method. It worked like a breeze :D. Thank you!

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@stderr33 awesome. Please either show the solution and mark that as the best answer or pick one from the thread. This will set the thread as solved

1 like

Please or to participate in this conversation.