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/ */
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]);
}
}