unixlike's avatar

Testing a function returning an object which calls a function

Hello! I'm currently trying to test the below function. I'm able to mock the chargify class successfully, but the issue I'm facing is the call to subscription which returns an object with then calls the read function.

How can I write a test for the call to subscription and the call to read when it's based on the returned object?

Thanks!

public function getChargifySubscription($chargify_id)
{

    $chargify_subscription = $this->chargify->subscription()->read($chargify_id);
    if ($chargify_subscription->isError()) {
        throw $this->getException($chargify_subscription);
    }

    return $chargify_subscription;
}
0 likes
1 reply
LaryAI's avatar
Level 58

To test the getChargifySubscription function, you'll need to mock the chargify object and the object returned by the subscription method so that you can control the behavior of the read method. Here's how you can do it using PHPUnit:

public function testGetChargifySubscription()
{
    // Create a mock of the subscription object that will be returned by the chargify->subscription() call
    $subscriptionMock = $this->createMock(SubscriptionClass::class);

    // Configure the mock to return a specific value when the read method is called
    $chargify_id = 123;
    $expectedSubscription = new SubscriptionClass(); // Replace with the actual expected subscription object
    $subscriptionMock->method('read')->with($chargify_id)->willReturn($expectedSubscription);

    // Now, mock the isError method to return false, as we are simulating a successful call
    $subscriptionMock->method('isError')->willReturn(false);

    // Create a mock of the chargify object
    $chargifyMock = $this->createMock(ChargifyClass::class);

    // Configure the mock to return the subscription mock when the subscription method is called
    $chargifyMock->method('subscription')->willReturn($subscriptionMock);

    // Inject the chargify mock into your class (assuming your class is called MyClass)
    $myClass = new MyClass();
    $myClass->chargify = $chargifyMock;

    // Call the method to test
    $result = $myClass->getChargifySubscription($chargify_id);

    // Assert that the result is the expected subscription object
    $this->assertSame($expectedSubscription, $result);
}

In this test, we are:

  1. Creating a mock of the SubscriptionClass that will be returned by the chargify->subscription() call.
  2. Configuring the mock to return a specific value when the read method is called with the $chargify_id.
  3. Mocking the isError method to return false to simulate a successful call.
  4. Creating a mock of the ChargifyClass and configuring it to return the subscription mock when the subscription method is called.
  5. Injecting the chargify mock into the class under test.
  6. Calling the getChargifySubscription method and asserting that the result is the expected subscription object.

Make sure to replace SubscriptionClass and ChargifyClass with the actual class names used in your code. Also, replace MyClass with the actual name of the class that contains the getChargifySubscription method.

1 like

Please or to participate in this conversation.