Class ExpectationToPhpSerializer

java.lang.Object
org.mockserver.serialization.code.ExpectationToPhpSerializer

public class ExpectationToPhpSerializer extends Object
Generates copy-paste-ready PHP expectation code for the MockServer PHP client (mock-server/mockserver-client).

Following the same JSON-wrap pattern as the JavaScript/Python generators, the generated code embeds each expectation's existing JSON serialization (the same bytes produced by format=JSON) in a PHP nowdoc, decodes it with json_decode(..., true), reconstructs it via Expectation::fromArray(...) and passes it to $client->upsertExpectation(...).

Verified against the PHP client source:

  • new MockServerClient('localhost', 1080) (MockServer\MockServerClient)
  • public function upsertExpectation(Expectation $expectation): array
  • Expectation::fromArray(array $data): self (added alongside this generator)

The JSON is embedded in a PHP nowdoc (<<<'JSON' ... JSON). A nowdoc performs no variable interpolation or escape processing, so the JSON is preserved verbatim. The closing delimiter is the literal token JSON alone on a line; Jackson's serialized JSON can never produce such a line (every line is part of structural JSON whose string values are escaped and quoted), so the nowdoc cannot be terminated early by expectation content. Example output:

 <?php

 use MockServer\MockServerClient;
 use MockServer\Expectation;

 $client = new MockServerClient('localhost', 1080);

 $client->upsertExpectation(Expectation::fromArray(json_decode(<<<'JSON'
 { ... }
 JSON, true)));
 
Author:
jamesdbloom
  • Constructor Details

    • ExpectationToPhpSerializer

      public ExpectationToPhpSerializer(ExpectationSerializer expectationSerializer)
  • Method Details