Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring psr7 trait back #482

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions test/Psr7AssertionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php

declare(strict_types=1);

namespace VonageTest;

use PHPUnit\Framework\ExpectationFailedException;
use Psr\Http\Message\RequestInterface;

use function http_build_query;
use function is_array;
use function json_decode;
use function json_encode;
use function parse_str;

trait Psr7AssertionTrait
{
/**
* @param $expected
* @param RequestInterface $request
*/
public static function assertRequestMethod($expected, RequestInterface $request): void
{
self::assertEquals($expected, $request->getMethod());
}

public static function assertRequestBodyIsEmpty(RequestInterface $request): void
{
$request->getBody()->rewind();
$body = $request->getBody()->getContents();
$request->getBody()->rewind();

self::assertEmpty($body);
}

/**
* @param $expected
* @param RequestInterface $request
*/
public static function assertRequestBodyIsJson($expected, RequestInterface $request): void
{
$request->getBody()->rewind();
$body = $request->getBody()->getContents();
$request->getBody()->rewind();

self::assertJsonStringEqualsJsonString($expected, $body);
}

/**
* @param $host
* @param $path
* @param $method
* @param RequestInterface $request
*/
public static function assertRequestUrl($host, $path, $method, RequestInterface $request): void
{
self::assertEquals($host, $request->getUri()->getHost());
self::assertEquals($path, $request->getUri()->getPath());
self::assertEquals($method, $request->getMethod());
}

/**
* @param $key
* @param RequestInterface $request
*/
public static function assertRequestQueryNotContains($key, RequestInterface $request): void
{
$query = $request->getUri()->getQuery();
$params = [];
parse_str($query, $params);

self::assertArrayNotHasKey($key, $params, 'query string has key when it should not: ' . $key);
}

/**
* @param $key
* @param $value
* @param RequestInterface $request
*/
public static function assertRequestQueryContains($key, $value, RequestInterface $request): void
{
$query = $request->getUri()->getQuery();
$params = [];
parse_str($query, $params);

self::assertArrayHasKey($key, $params, 'query string does not have key: ' . $key);

$errorValue = $value;

if (is_array($errorValue)) {
$errorValue = json_encode($errorValue);
}

self::assertSame($value, $params[$key], 'query string does not have value: ' . $errorValue);
}

/**
* @param $key
* @param RequestInterface $request
*/
public static function assertRequestQueryHas($key, RequestInterface $request): void
{
$query = $request->getUri()->getQuery();
$params = [];
parse_str($query, $params);
self::assertArrayHasKey($key, $params, 'query string does not have key: ' . $key);
}

/**
* @param $key
* @param $value
* @param RequestInterface $request
*/
public static function assertRequestFormBodyContains($key, $value, RequestInterface $request): void
{
self::assertEquals(
'application/x-www-form-urlencoded',
$request->getHeaderLine('content-type'),
'incorrect `Content-Type` for POST body'
);

$request->getBody()->rewind();
$data = $request->getBody()->getContents();
$params = [];
parse_str($data, $params);

self::assertArrayHasKey($key, $params, 'body does not have key: ' . $key);
self::assertSame($value, $params[$key], 'body does not have value: ' . $value);
}

public static function assertRequestJsonBodyContains(
$key,
$value,
RequestInterface $request,
bool $nested = false
): void
{
self::assertEquals(
'application/json',
$request->getHeaderLine('content-type'),
'incorrect `Content-Type` for JSON body'
);

$request->getBody()->rewind();
$params = json_decode($request->getBody()->getContents(), true);

if (!$nested) {
self::assertArrayHasKey($key, $params, 'body does not have key: ' . $key);
self::assertSame($value, $params[$key]);

return;
}

try {
$keyValue = self::findNestedKey($params, $key, $value);
self::assertSame($value, $keyValue);
} catch (\OutOfBoundsException $e) {
throw new ExpectationFailedException('Body does not have nested key: ' . $key);
}
}

protected static function findNestedKey(array $params, string $searchKey, mixed $searchValue)
{
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($params));

while ($iterator->valid()) {
if ($iterator->key() === $searchKey && $iterator->current() === $searchValue) {
return $iterator->current();
}

$iterator->next();
}

throw new \OutOfBoundsException('Cannot find given Key');
}

/**
* @param $key
* @param $value
* @param RequestInterface $request
*/
public static function assertRequestJsonBodyMissing($key, RequestInterface $request): void
{
self::assertEquals(
'application/json',
$request->getHeaderLine('content-type'),
'incorrect `Content-Type` for JSON body'
);

$request->getBody()->rewind();
$params = json_decode($request->getBody()->getContents(), true);

self::assertArrayNotHasKey($key, $params, 'body does not have key: ' . $key);
}

/**
* @param $url
* @param RequestInterface $request
*/
public static function assertRequestMatchesUrl($url, RequestInterface $request): void
{
self::assertEquals($url, $request->getUri()->withQuery('')->__toString(), 'url did not match request');
}

/**
* @param $url
* @param RequestInterface $request
*/
public static function assertRequestMatchesUrlWithQueryString($url, RequestInterface $request): void
{
$query = [];

parse_str($request->getUri()->getQuery(), $query);

unset($query['api_key'], $query['api_secret']);

$query = http_build_query($query);

self::assertEquals($url, $request->getUri()->withQuery($query)->__toString(), 'url did not match request');
}
}
Loading