Ad

How To Test A Method Who Calls An API? (Laravel)

- 1 answer

I´m testing a method that stores a credit card information and inside that method I have to call an API to request the bank to store all the card information then they return me a token to make future transactions.

The problem is when I Test the method I have this error

cURL error 60: SSL certificate problem: self signed certificate in certificate chain

This is my test method

/**
 ** @test
 **/
public function a_logged_user_can_add_a_credit_debit_card()
{
  $demoCard = [
        'card_number' => '5424180279791732',
        'month' => '05',
        'year' => '2021',
        'cvc' => '123',
    ];

    $reponse = $this->postJson(route('card.create'),
        $demoCard, $this->user->headersToken());

    $reponse->assertJson(['success' => true]);

    $this->assertEquals(1, Tarjeta::first()->user_id);
    $this->assertEquals($this->tarjetaDemo['month'], Tarjeta::first()->month);
    $this->assertEquals($this->tarjetaDemo['year'], Tarjeta::first()->year);
    $this->assertEquals($this->tarjetaDemo['cvc'], Tarjeta::first()->cvc);
    $this->assertTrue(!empty(Tarjeta::first()->token));
}

And this is my method

 private $clientEcomm;
/**
 * constructor.
 */
public function __construct()
{
    $merchant = env('MERCHANT_ECOMM');
    $apiPassword = env('API_ECOMM_PASSWORD');
    $apiUsername = env('API_ECOMM_USER');

    $this->clientEcomm = new Client([
        'base_uri' => "https://banamex.dialectpayments.com/api/rest/version/54/merchant/{$merchant}/",
        'auth' => [$apiUsername, $apiPassword]
    ]);
}

public function create(CardRequest $request)
{

    $response = $this->clientEcomm->put(
        "token",
        [
            'json' => [
                'sourceOfFunds' => [
                    'provided' => [
                        'card' => [
                            'number' => $request->get('card_number'),
                            'expiry' => [
                                'month' => $request->get('month'),
                                'year' => Str::substr($request->get('year'), -2),
                            ],
                            'securityCode' => $request->get('cvc')
                        ]
                    ],
                    'type' => 'CARD',
                ],
            ]
        ]
    );

    $response = \GuzzleHttp\json_decode($response->getBody()->getContents());

    Tarjeta::create([
        'user_id' => \Auth::user()->id,
        'mes' => $request->get('month'),
        'year' => $request->get('year'),
        'cvc' => $request->get('cvc'),
        'token' =>  $response->token,
    ]);

    return $this->successResponse();
}

How can I do this kind of tests?

Ad

Answer

When you writing a test for an endpoint, you need to be careful to mock the requests to third party API. So, in this case, you need to mock the Client.

public function __construct(Client $client)
{

}
$this->app->instance(Client::class, Mockery::mock(Client::class, function($mockery){
    $mockery->shouldReceive('put')->once()->with($parameters)->andReturn($response);
}));

This way, your code will never call the third party endpoints.

Ad
source: stackoverflow.com
Ad