Skip to content

Commit

Permalink
Fix message pack tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwin-Luijten committed Jul 7, 2022
1 parent cc20a16 commit 80b1e21
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM php:8.0-cli

RUN apt-get update && apt-get install -y git unzip && pecl install pcov && docker-php-ext-enable pcov && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN apt-get update && apt-get install -y git unzip && pecl install pcov && pecl install msgpack && docker-php-ext-enable pcov && docker-php-ext-enable msgpack && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer
10 changes: 2 additions & 8 deletions src/Request/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ class Format
const FORMAT_XML = 'xml';
const FORMAT_MSGPACK = 'msgpack';

/**
* @return string
*/
public static function getDefault()
public static function getDefault(): string
{
return self::FORMAT_JSON;
}

/**
* @return array
*/
public static function getAvailable()
public static function getAvailable(): array
{
return [self::FORMAT_JSON, self::FORMAT_XML];
}
Expand Down
5 changes: 1 addition & 4 deletions src/Request/PathRequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

class PathRequestMatcher extends RegexRequestMatcher
{
/**
* @param string $path
*/
public function __construct($path)
public function __construct(string $path)
{
parent::__construct([sprintf('~^%s~', $path), sprintf('~^%s/~', $path)]);
}
Expand Down
60 changes: 13 additions & 47 deletions src/Request/RegexRequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,14 @@

class RegexRequestMatcher extends AbstractRequestMatcher
{
/**
* @var array
*/
protected $whitelist = [];

/**
* @var array
*/
protected $blacklist = [];

/**
* @param array $whitelist
* @param array $blacklist
*/
public function __construct(array $whitelist = [], array $blacklist = [])
public function __construct(protected array $whitelist = [], protected array $blacklist = [])
{
$this->whitelist = $whitelist;
$this->blacklist = $blacklist;

}

/**
* @param Request $request
* @param int $requestType
* @return bool
*/
public function matches(Request $request, $requestType = HttpKernelInterface::MASTER_REQUEST)
public function matches(Request $request, ?int $requestType = HttpKernelInterface::MAIN_REQUEST): bool
{
if ($requestType !== HttpKernelInterface::MASTER_REQUEST) {
if ($requestType !== HttpKernelInterface::MAIN_REQUEST) {
return false;
}

Expand All @@ -51,62 +31,48 @@ public function matches(Request $request, $requestType = HttpKernelInterface::MA
return true;
}

/**
* @param Request $request
*/
protected function markRequestAsMatched(Request $request)
{
$request->attributes->set(self::ATTRIBUTE_MATCHED, true);
}

/**
* @param Request $request
* @return bool
*/
protected function matchPreviouslyMatchedRequest(Request $request)
protected function matchPreviouslyMatchedRequest(Request $request): bool
{
return $request->attributes->getBoolean(self::ATTRIBUTE_MATCHED);
}

/**
* @param $requestPath
* @return bool
*/
protected function matchRequestPathAgainstLists($requestPath)
protected function matchRequestPathAgainstLists($requestPath): bool
{
if ($this->matchRequestPathAgainstBlacklist($requestPath)) {
return false;
}

if ($this->matchRequestPathAgainstWhitelist($requestPath)) {
return true;
}

return false;
}

/**
* @param $requestPath
* @return bool
*/
protected function matchRequestPathAgainstBlacklist($requestPath)
protected function matchRequestPathAgainstBlacklist(string $requestPath): bool
{
foreach ($this->blacklist as $regex) {
if (preg_match($regex, $requestPath)) {
return true;
}
}

return false;
}

/**
* @param $requestPath
* @return bool
*/
protected function matchRequestPathAgainstWhitelist($requestPath)
protected function matchRequestPathAgainstWhitelist(string $requestPath): bool
{
foreach ($this->whitelist as $regex) {
if (preg_match($regex, $requestPath)) {
return true;
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/Request/RequestMatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface RequestMatcherInterface
{
public function matches(Request $request, $requestType = null);
public function matches(Request $request, ?int $requestType);
}
24 changes: 4 additions & 20 deletions src/Request/RequestTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,33 @@

class RequestTransformer implements RequestTransformerInterface
{
/**
* @var SerializerInterface
*/
protected $serializer;

/**
* @param SerializerInterface $serializer
*/
public function __construct(SerializerInterface $serializer)
public function __construct(protected SerializerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* @param Request $request
*/
public function transform(Request $request)
{
$this->acceptJsonBody($request);
$this->setRequestFormat($request);
}

/**
* @param Request $request
*/
protected function acceptJsonBody(Request $request)
{
if (strpos($request->headers->get('Content-Type'), 'application/json') === 0) {
if (str_starts_with($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
}

/**
* @param Request $request
*/
protected function setRequestFormat(Request $request)
{
$default = Format::getDefault();
$format = $request->getRequestFormat($request->query->get('_format', $default));

if (!in_array($format, $this->serializer->getSupportedFormats())) {
$format = $this->serializer->getDefaultFormat();
}

$request->setRequestFormat($format);
}
}
5 changes: 1 addition & 4 deletions tests/Exception/JsonSerializableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

class JsonSerializableException extends Exception implements JsonSerializable
{
/**
* @inheritDoc
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return ['code' => 0, 'message' => 'json_serialized_message', 'fields' => []];
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Serializer/MsgpackSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
if (!function_exists('msgpack_pack')) {
function msgpack_pack($value)
{
return 'bar';
// From https://github.com/rybakit/msgpack.php/blob/master/tests/Unit/MessagePackTest.php:24
return "\x91\x01";
}
}
}
Expand Down Expand Up @@ -38,8 +39,8 @@ public function test_supports()
public function test_serialize()
{
$serializer = new MsgpackSerializer();
$output = $serializer->serialize('foo', 'msgpack');
$this->assertEquals('bar', $output);
$output = $serializer->serialize([0 => 1], 'msgpack');
$this->assertEquals("\x91\x01", $output);
}
}
}

0 comments on commit 80b1e21

Please sign in to comment.