PHP code example of ranierif / api-mocker

1. Go to this page and download the library: Download ranierif/api-mocker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ranierif / api-mocker example snippets




namespace Tests\Feature\Payment;

use Illuminate\Http\Response;
use Mockery;
use Ranierif\Traits\ApiMockerTrait;
use Tests\TestCase;
use Tests\ApiMocker\stripe\StripeApiMocker;

class PaymentTest extends TestCase
{
    use ApiMockerTrait;

    public function test_successful_customer_creation(): void
    {
        // Arrange
        $mockResponseBody = $this->getMockResponseAsArray(
            provider: 'stripe', 
            endpoint: 'customers', 
            scenario: 'success',
        );
                
        $this->instance(
            StripeApiService::class,
            Mockery::mock(StripeApiService::class, function (MockInterface $mock) use ($mockResponseBody) {
                return $mock->shouldReceive('requestCreateCustomer')->andReturn([
                    'status_code' => Response::HTTP_OK,
                    'body' => $mockResponseBody,
                ]);
            })
        );
        
        // Act
        $response = $this
            ->postJson(
                '/api/customers',
                [
                    'name' => 'Test Customer',
                    'email' => '[email protected]',
                ]
            );
        
        // Assert
        $this->assertJson($response);
        $this->assertEquals($mockResponseBody['id'], $response['id']);
    }
}



namespace Tests\ApiMocker\Stripe;

use Ranierif\ApiMocker\AbstractApiMocker;

class StripeApiMocker extends AbstractApiMocker
{
    protected function getProviderName(): string
    {
        return 'stripe';
    }

    public function getCustomerSuccessResponse(): string
    {
        return $this->getResponse('customers', 'success');
    }

    public function getCustomerErrorResponse(): string
    {
        return $this->getResponse('customers', 'error');
    }
}

// Test success scenario
$successResponse = $mocker->getResponse('payments', 'success');

// Test error scenario
$errorResponse = $mocker->getResponse('payments', 'error');

// Test validation failure
$validationResponse = $mocker->getResponse('payments', 'validation_error');
markdown
tests/ 
└── ApiMocker/ 
    └── Stripe/ 
        ├── StripeApiMocker.php 
        └── json/