Jad Joubran
Jad Joubran

Reputation: 2579

How to write integration test for an endpoint that uses Braintree with static method calls

I am using the Braintree PHP Client which relies heavily on static methods. All my endpoints in this project are covered with integration tests Something like:

Storage::shouldReceive('put')->once()->andReturn(true);

$this->post('/api/payment');

As you can see I'm also using Mockery in order to create mocks. However since the Braintree library is heavily relying on static methods, I'm not able to create methods, thus not able to test these endpoints.

Here's an example of a code written using the Braintree PHP Client:

$result = Braintree\Transaction::sale([
    'amount' => '1000.00',
    'paymentMethodNonce' => 'nonceFromTheClient',
    'options' => [ 'submitForSettlement' => true ]
]);

What options do I have here?

Upvotes: 3

Views: 802

Answers (4)

omarjebari
omarjebari

Reputation: 5519

Usually I would advocate mocking the responses from an API instead of making calls to it when running integration tests. However, I contacted Braintree and they said (at time of writing) that it's ok to run the tests vs the sandbox as long as you don't exceed 25 concurrent requests per second.

Upvotes: 2

abbood
abbood

Reputation: 23558

this answer will only work if you got mockery 1.* installed.. earlier versions won't do static method mocking. The below code works:

    $brainTreeMock = Mockery::mock('alias:Braintree_Transaction');

    $transaction = (object)[ 'id' => str_random(5) ];
    $brainTreeMock->shouldReceive('sale')->andReturn((object)[
        'success'     => true,
        'transaction' => $transaction
        ]
    );

Upvotes: 4

Fredrik Schöld
Fredrik Schöld

Reputation: 1658

You can use an alias mock to mock public static method calls. You would use it like this:

$classMock = Mockery::mock('alias:NamespaceToClass\ClassName');
$classMock->shouldReceive('someMethod')->once()->andReturn('Something');

Upvotes: 2

Alex Blex
Alex Blex

Reputation: 37048

Mocking one of the components during integration tests should be done with great care, as it defeats the purpose.

I believe Braintree provides a sanbox for integration testing, so there is no need to mock it.

Upvotes: 2

Related Questions