fileLocation = $fileLocation;
$this->fileName = $fileName;
$this->fail = $fail;
}
public function buildContent(): string
{
$content = '';
if ($this->buildFlags & self::FLAG_BUILD_HTML) {
$content .= '
HTML content
';
}
if ($this->buildFlags & self::FLAG_BUILD_PHP) {
// base64 encoded representation of 'PHP content'
$content .= '';
}
if ($this->buildFlags & self::FLAG_BUILD_SVG) {
$content .= 'SVG content';
}
if ($this->buildFlags & self::FLAG_BUILD_SVG_DOCUMENT) {
return sprintf(
'',
$content
);
}
return sprintf(
'%s',
$content
);
}
public function matches(ResponseInterface $response): bool
{
return $this->getMismatches($response) === [];
}
/**
* @return StatusMessage[]
*/
public function getMismatches(ResponseInterface $response): array
{
$mismatches = [];
if ($this->handler instanceof \Closure) {
$result = $this->handler->call($this, $this, $response);
if ($result !== null) {
$mismatches[] = $result;
}
return $mismatches;
}
$body = (string)$response->getBody();
$contentType = $response->getHeaderLine('content-type');
if ($this->expectedContent !== null && !str_contains($body, $this->expectedContent)) {
$mismatches[] = new StatusMessage(
'content mismatch %s',
$this->expectedContent,
$body
);
}
if ($this->unexpectedContent !== null && str_contains($body, $this->unexpectedContent)) {
$mismatches[] = new StatusMessage(
'unexpected content %s',
$this->unexpectedContent,
$body
);
}
if ($this->expectedContentType !== null
&& !str_starts_with($contentType . ';', $this->expectedContentType . ';')) {
$mismatches[] = new StatusMessage(
'content-type mismatch %s, got %s',
$this->expectedContentType,
$contentType
);
}
if ($this->unexpectedContentType !== null
&& str_starts_with($contentType . ';', $this->unexpectedContentType . ';')) {
$mismatches[] = new StatusMessage(
'unexpected content-type %s',
$this->unexpectedContentType,
$contentType
);
}
return $mismatches;
}
public function withExpectedContentType(string $contentType): self
{
$target = clone $this;
$target->expectedContentType = $contentType;
return $target;
}
public function withUnexpectedContentType(string $contentType): self
{
$target = clone $this;
$target->unexpectedContentType = $contentType;
return $target;
}
public function withExpectedContent(string $content): self
{
$target = clone $this;
$target->expectedContent = $content;
return $target;
}
public function withUnexpectedContent(string $content): self
{
$target = clone $this;
$target->unexpectedContent = $content;
return $target;
}
public function withHandler(\Closure $handler): self
{
$target = clone $this;
$target->handler = $handler;
return $target;
}
public function withBuildFlags(int $buildFlags): self
{
$target = clone $this;
$target->buildFlags = $buildFlags;
return $target;
}
public function getFileLocation(): FileLocation
{
return $this->fileLocation;
}
public function getFileName(): string
{
return $this->fileName;
}
public function getUrl(ServerRequestInterface $request): string
{
return $this->fileLocation->getBaseUrl($request) . $this->fileName;
}
public function shallFail(): bool
{
return $this->fail;
}
public function getExpectedContentType(): ?string
{
return $this->expectedContentType;
}
public function getUnexpectedContentType(): ?string
{
return $this->unexpectedContentType;
}
public function getExpectedContent(): ?string
{
return $this->expectedContent;
}
public function getUnexpectedContent(): ?string
{
return $this->unexpectedContent;
}
}