PHP code example of elkady / laravel-pact

1. Go to this page and download the library: Download elkady/laravel-pact 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/ */

    

elkady / laravel-pact example snippets


Pact::consumer('checkout-service')
    ->provider('billing-service')
    ->given('invoice 42 exists')
    ->uponReceiving('a request for invoice 42')
    ->withRequest('GET', '/api/invoices/42')
    ->willRespondWith(200, [
        'id' => Pact::match()->integer(42),
        'total' => Pact::match()->decimal(19.99),
    ]);

$invoice = app(BillingClient::class)->fetchInvoice(42);   // ordinary Http:: call

Pact::verifyInteractions();

use LaravelPact\Facades\Pact;
use LaravelPact\Testing\InteractsWithPact;

class FetchInvoiceTest extends TestCase
{
    use InteractsWithPact;

    public function test_it_fetches_an_invoice(): void
    {
        Pact::consumer('checkout-service')
            ->provider('billing-service')
            ->given('invoice 42 exists')
            ->uponReceiving('a request for invoice 42')
            ->withRequest('GET', '/api/invoices/42')
            ->willRespondWith(200, [
                'id'    => Pact::match()->integer(42),
                'email' => Pact::match()->email(),
                'total' => Pact::match()->decimal(19.99),
            ]);

        $invoice = app(BillingClient::class)->fetchInvoice(42);

        $this->assertSame(42, $invoice['id']);

        Pact::verifyInteractions();
    }
}

class BillingProviderTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_honours_its_consumers(): void
    {
        Pact::provider('billing-service')
            ->withStatesFrom(InvoiceStates::class)
            ->fromBroker()
            ->verify();          // throws with a readable report on failure
    }
}

class InvoiceStates
{
    #[ProviderState('invoice 42 exists')]
    public function invoiceExists(array $params): void
    {
        Invoice::factory()->create(['id' => $params['id'] ?? 42]);
    }
}

class BillingClient
{
    #[PactConsumes(
        provider: 'billing-service',
        method: 'GET',
        path: '/api/invoices/42',
        state: 'invoice 42 exists',
        response: [
            'id'    => PactFieldType::Integer,
            'email' => PactFieldType::Email,
            'total' => PactFieldType::Decimal,
        ],
    )]
    public function fetchInvoice(int $invoiceId): array { /* ... */ }
}

Pact::messageConsumer('notification-service')
    ->provider('order-service')
    ->given('order 42 has shipped')
    ->expectsToReceive('an order shipped event')
    ->withMetadata(['event' => 'order.shipped'])
    ->withContent([
        'order_id'        => Pact::match()->integer(42),
        'tracking_number' => Pact::match()->string('TRK-99'),
    ])
    ->verifiedBy(fn (ReceivedMessage $message) => (new SendShipmentNotification)
        ->handle(OrderShipped::fromPayload($message->toArray())));

Pact::provider('order-service')
    ->producing('an order shipped event', fn (array $params) => new OrderShipped(...))
    ->fromBroker()
    ->verify();
bash
php artisan pact:publish --branch=main --build-url="$CI_URL"
php artisan pact:verify --provider=billing-service --publish
php artisan pact:can-i-deploy --to-environment=production   # exits 1 if unsafe
bash
php artisan pact:generate-tests
bash
php artisan vendor:publish --tag=pact-ci