phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
568 stars 211 forks source link

Mocking the ShopifySDK #274

Closed whobutsb closed 1 year ago

whobutsb commented 1 year ago

Does anyone have any experience using Mockery with the ShopifySDK? Can anyone share any tips or code to start mocking the API? Thank you for the help.

whobutsb commented 1 year ago

I asked ChatGPT to create a Mockery object for me of the Shopify SDK. Here is what it came up with for those who would be interested.

use Mockery;
use PHPShopify\ShopifyResource;

$shopifyMock = Mockery::mock(ShopifyResource::class);

$orderMock = Mockery::mock(Order::class);

$orderMock->shouldReceive('get')
          ->with(['status' => 'cancelled', 'created_at_min' => '2016-06-25T16:15:47-04:00', 'fields' => 'id,line_items,name,total_price'])
          ->once()
          ->andReturn(['order1', 'order2']);

$shopifyMock->shouldReceive('Order')
            ->withNoArgs()
            ->once()
            ->andReturn($orderMock);

// Now you can use the mock object in your test case
$params = array(
    'status' => 'cancelled',
    'created_at_min' => '2016-06-25T16:15:47-04:00',
    'fields' => 'id,line_items,name,total_price'
);

$orders = $shopifyMock->Order->get($params);