mcustiel/phiremock-common
Shared utilities for the Phiremock PHP ecosystem. Provides common classes, interfaces, and helpers used across related packages to simplify building and consuming Phiremock client/server components with consistent behavior and minimal duplication.
Installation
composer require mcustiel/phiremock-common
Updated Requirement: Ensure your project uses PHP 8.2+ and Laravel 9+ (verify via composer.json constraints). PHP 8.1 and below are no longer supported.
First Use Case: Mocking HTTP Responses Import the core trait and use it in a test:
use Mcustiel\PhiremockCommon\Traits\MockHttpResponse;
class MyTest extends TestCase
{
use MockHttpResponse;
public function test_mocked_response()
{
$this->mockHttpResponse('GET', 'https://api.example.com/data', 200, ['key' => 'value']);
$response = Http::get('https://api.example.com/data');
$this->assertEquals(['key' => 'value'], $response->json());
}
}
Key Classes to Explore
MockHttpResponse trait: For HTTP mocking.MockDatabase trait: For database query mocking.PhiremockServiceProvider: Registers package services (if applicable).Define Mocks in Tests
$this->mockHttpResponse(
method: 'POST',
url: 'https://api.example.com/webhooks',
status: 201,
headers: ['X-Webhook-ID' => '123'],
body: json_encode(['success' => true])
);
Conditional Mocking Use closures for dynamic responses (PHP 8.2+ features like named arguments and attributes are fully supported):
$this->mockHttpResponse('GET', 'https://api.example.com/user/{id}', function ($request) {
return response()->json(['id' => $request->id, 'name' => 'Test User']);
});
Integration with Laravel HTTP Client
Works seamlessly with Http::macro() or Http::as():
$this->mockHttpResponse('GET', 'https://api.example.com/data', 200, ['data' => []]);
$response = Http::withOptions(['debug' => false])->get('https://api.example.com/data');
Mock Eloquent Queries
use Mcustiel\PhiremockCommon\Traits\MockDatabase;
$this->mockDatabaseQuery(
User::query()->where('active', true),
['id' => 1, 'name' => 'Admin']
);
Stub Database Connections Override connections for specific tests:
$this->mockDatabaseConnection('mysql', function ($query) {
return collect([['id' => 1, 'name' => 'Mock User']]);
});
$this->resetMocks();
setUp() for reusable test suites.assertDatabaseHas() or custom assertions:
$this->assertMockWasCalled('GET', 'https://api.example.com/data');
URL Matching Strictness
$this->mockHttpResponse('GET', '/api/users/.*', 200, []);
https:\/\/api\.example\.com).Case Sensitivity
GET, Post) are case-insensitive, but headers/URLs may not be. Normalize inputs:
$this->mockHttpResponse(strtoupper('get'), 'https://api.example.com/data', ...);
Database Mocking Scope
afterApplicationCreated() in TestCase to reset mocks globally.PHP 8.2+ Breaking Changes
Performance Overhead
Verify Mocks
$this->assertMockExists('GET', 'https://api.example.com/data');
$this->assertMockWasCalled('GET', 'https://api.example.com/data');
Inspect Requests Use closures to log requests:
$this->mockHttpResponse('GET', 'https://api.example.com/data', function ($request) {
\Log::debug('Mocked request:', $request->toArray());
return response()->json(['data' => []]);
});
Clear Mocks Explicitly If tests fail due to stale mocks, force a reset:
$this->resetMocks(); // Clear all HTTP/database mocks
Custom Matchers Extend the trait to add custom logic:
protected function shouldMockRequest($method, $url, $request)
{
return $request->hasHeader('X-Custom-Header');
}
Mocking Exceptions Simulate API errors:
$this->mockHttpResponse('GET', 'https://api.example.com/fail', 500, [], 'Internal Server Error');
Integration with Pest Works with Pest via traits or mixins:
uses(MockHttpResponse::class)->in('Feature');
refreshTestingTask() in CI.How can I help you explore Laravel packages today?