Summer Sale! All accounts are 50% off this week.

eskiesirius's avatar

Problem about mocking

I really don't know what im doing right now.. in my class

use App\Paymongo\Payment;

/**
	 * @param  array  $data   
	 * @param  string $method 
	 * @return string         
	 */
	public function topUp(array $data, $method)
	{
		if ($method == 'gcash') {
			$checkout = Payment::payWithGcash($data['amount']);
		}

		$attributes = $checkout->getAttributes();

		event(new TopUpRequested($attributes['id'],$attributes['amount']));

		return $attributes['redirect']['checkout_url'];
	}

inside App\Paymongo\Payment

<?php

namespace App\Paymongo;

use Luigel\Paymongo\Facades\Paymongo;

class Payment extends Paymongo
{

    /**
     * @param  float $amount 
     * @return Paymongo         
     */
    public function payWithGcash($amount)
    {
        return Paymongo::source()->create([
            'type' => 'gcash',
            'amount' => $amount,
            'currency' => 'PHP',
            'redirect' => [
                'success' => route('top-up.success'),
                'failed' => route('top-up.failed')
            ]
        ]);
    }
}

in my test file i have this snippet

use App\Paymongo\Payment;
use Luigel\Paymongo\Paymongo;

/** @test */
    public function test_can_create_transaction_when_topup_gcash()
    {
        $paymongo = Mockery::mock(Paymongo::class);

        Payment::shouldReceive('payWithGcash')->once()
            ->shouldReceive('source')->andReturn($paymongo)
            ->shouldReceive('create')
            ->andReturn(json_decode('
            {
                "data": {
                    "id": "gggggg",
                    "type": "source",
                    "attributes": {
                        "amount": 10000,
                        "billing": null,
                        "currency": "PHP",
                        "description": null,
                        "livemode": false,
                        "redirect": {
                            "checkout_url": "link.com",
                            "failed": "http://localhost/failed",
                            "success": "http://localhost/success"
                            },
                        "statement_descriptor": null,
                        "status": "pending",
                        "type": "gcash",
                        "created_at": 1648140991,
                        "updated_at": 1648140991
                    }
                }
            }',true));
    }

i got this error Received Mockery_2_Luigel_Paymongo_Paymongo::create(), but no expectations were specified but when i remove ->andReturn($paymongo), the error will be Call to a member function create() on null

0 likes
3 replies
Sergiu17's avatar
$paymongo = Mockery::mock(Paymongo::class);

$paymongo->shouldReceive('create')
  ->andReturn(json_decode(''));

Payment::shouldReceive('payWithGcash')
  ->once()
  ->shouldReceive('source')->andReturn($paymongo);

this should work I think

eskiesirius's avatar

@Sergiu17 I managed to fix it with

Payment::shouldReceive('payWithGcash')->once()
            ->shouldReceive('source->create')
            ->andReturn(json_decode('
            {
                "data": {
                    "id": "gggggg",
                    "type": "source",
                    "attributes": {
                        "amount": 10000,
                        "billing": null,
                        "currency": "PHP",
                        "description": null,
                        "livemode": false,
                        "redirect": {
                            "checkout_url": "link.com",
                            "failed": "http://localhost/failed",
                            "success": "http://localhost/success"
                            },
                        "statement_descriptor": null,
                        "status": "pending",
                        "type": "gcash",
                        "created_at": 1648140991,
                        "updated_at": 1648140991
                    }
                }
            }',true));

thanks for the reply

1 like
MichalOravec's avatar

@eskiesirius Fix your username on twitter and github.

Just pass eskiesirius to both of them. This will fix your avatar.

Please or to participate in this conversation.