Installation Add the package via Composer:
composer require champs-libres/wopi-test-bundle
Register the bundle in config/app.php under providers:
ChampsLibres\WopiTestBundle\WopiTestBundle::class,
First Use Case: Mocking WOPI Server
Configure the bundle in config/packages/champs_libres_wopi_test.yaml:
wopi_test:
enabled: true
mock_server_url: 'http://localhost:8000/wopi'
Use the WopiTestClient in a test:
use ChampsLibres\WopiTestBundle\Client\WopiTestClient;
public function testWopiIntegration()
{
$client = new WopiTestClient();
$response = $client->getFileInfo('test-file-id');
$this->assertEquals(200, $response->getStatusCode());
}
Key Files to Review
src/Client/WopiTestClient.php: Core client logic.src/DependencyInjection/Configuration.php: Configuration schema.tests/: Example test cases.Mocking WOPI Endpoints
Use WopiTestClient to simulate WOPI server responses (e.g., getFileInfo, lock, unlock):
$client = new WopiTestClient();
$client->mockGetFileInfo(['size' => 1024, 'userId' => 'test-user']);
$response = $client->getFileInfo('file-id');
Integration with wopi-bundle
Replace the real WOPI client with the test client in tests:
$kernel = self::bootKernel();
$container = $kernel->getContainer();
$container->set('wopi.client', function () use ($client) {
return $client;
});
Custom Mock Responses
Extend WopiTestClient to add custom endpoints:
class CustomWopiTestClient extends WopiTestClient
{
public function mockCustomEndpoint(array $data)
{
$this->mockResponse('/custom', $data);
}
}
$this->app->bind(WopiTestClient::class, function () {
return new WopiTestClient();
});
trait WopiTestTrait
{
protected function mockWopiResponse(string $endpoint, array $data)
{
$client = new WopiTestClient();
$client->mockResponse($endpoint, $data);
return $client;
}
}
Configuration Overrides
Ensure wopi_test.enabled is false in production to avoid unintended mocking:
# config/packages/champs_libres_wopi_test.yaml
wopi_test:
enabled: false # Disable in production!
Debugging Tip: Check config('wopi_test.enabled') in tests to confirm mocking is active.
Endpoint Mismatches
The bundle assumes WOPI endpoints follow standard paths (e.g., /files/{id}). Custom WOPI setups may require extending WopiTestClient:
$client->setBaseUrl('http://custom-wopi-server');
Stateful Mocks Mock responses are stateless by default. For stateful tests (e.g., locks), use a custom client:
$client->setState(['locks' => ['file-id' => true]]);
dd($client->getMockedResponses()) to inspect active mocks.WopiTestClient logs for malformed requests:
$client->setDebug(true);
Custom Responses
Override mockResponse() to add logic (e.g., dynamic data):
$client->mockResponse('/files/{id}', function ($id) {
return ['size' => rand(1, 1024), 'id' => $id];
});
Middleware Simulation Simulate WOPI middleware (e.g., auth) by extending the client:
class AuthWopiTestClient extends WopiTestClient
{
public function __construct()
{
$this->mockResponse('/files/*', [], 401);
}
}
Event Listeners Hook into the bundle’s events (if added in future versions) to log test interactions:
$client->on('request', function ($request) {
\Log::info('WOPI Test Request:', $request->toArray());
});
How can I help you explore Laravel packages today?