Skip to content

Commit

Permalink
Merge pull request #260 from koriym/wrong_type_bad_request
Browse files Browse the repository at this point in the history
Wrong type bad request
  • Loading branch information
koriym authored Jan 26, 2022
2 parents 6798bc7 + 332072b commit 8a0bdca
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 213 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5.10",
"bamarni/composer-bin-plugin": "^1.4",
"rector/rector": "^0.12.12"
"bamarni/composer-bin-plugin": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 0 additions & 26 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,26 +0,0 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
__DIR__ . '/tests'
]);

// Define what rule sets will be applied
$containerConfigurator->import(LevelSetList::UP_TO_PHP_74);

// get services (needed for register a single rule)
// $services = $containerConfigurator->services();

// register a single rule
// $services->set(TypedPropertyRector::class);
};
11 changes: 10 additions & 1 deletion src/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace BEAR\Resource;

use BEAR\Resource\Exception\BadRequestException;
use Throwable;
use function call_user_func_array;
use function is_callable;
use function ucfirst;
Expand Down Expand Up @@ -37,7 +39,14 @@ public function invoke(AbstractRequest $request): ResourceObject

$params = $this->params->getParameters($callable, $request->query);
/** @psalm-suppress MixedAssignment */
$response = call_user_func_array($callable, $params);
try {
$response = call_user_func_array($callable, $params);
} catch (Throwable $e) {
if (get_class($e) === 'TypeError') {
throw new BadRequestException('Invalid parameter type', Code::BAD_REQUEST, $e);
}
throw $e;
}
if (! $response instanceof ResourceObject) {
$request->resourceObject->body = $response;
$response = $request->resourceObject;
Expand Down
2 changes: 1 addition & 1 deletion src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function getUri(string $class): string
{
$classPath = explode('\\', $class);
// $class
$this->extras[self::EXTRAS_VENDOR] = array_shift($classPath);
$this->extras[self::EXTRAS_VENDOR] = array_shift($classPath); // @phpstan-ignore-line
$this->extras[self::EXTRAS_PACKAGE] = array_shift($classPath); // @phpstan-ignore-line
array_shift($classPath); // "/Resource/"
$scheme = array_shift($classPath);
Expand Down
17 changes: 17 additions & 0 deletions tests/Fake/FakeVendor/Sandbox/Resource/App/Stone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace FakeVendor\Sandbox\Resource\App;

use BEAR\Resource\ResourceObject;

class Stone extends ResourceObject
{
public function onGet(int $id)
{
unset($id);

return $this;
}
}
5 changes: 0 additions & 5 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use function serialize;
use function set_error_handler;

use const E_USER_WARNING;

class RequestTest extends TestCase
{
protected Request $request;
Expand Down Expand Up @@ -140,16 +138,13 @@ public function testToStringWithErrorRenderer(): void
Request::GET,
['a' => 'koriym', 'b' => 25]
);
$no = $str = '';
set_error_handler(static function (int $errno, string $errstr) use (&$no, &$str): bool {
$no = $errno;
$str = $errstr;

return true;
});
(string) $request; // @phpstan-ignore-line
$this->assertSame(E_USER_WARNING, $no);
$this->assertStringContainsString('FakeErrorRenderer->render', $str);
$this->assertSame('', (string) $request);
restore_error_handler();
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BEAR\Resource;

use BEAR\Resource\Exception\BadRequestException;
use BEAR\Resource\Exception\MethodNotAllowedException;
use BEAR\Resource\Module\HalModule;
use BEAR\Resource\Module\ResourceModule;
Expand Down Expand Up @@ -335,4 +336,10 @@ public function testSerialize(): void
{
$this->assertInstanceOf(ResourceInterface::class, unserialize(serialize($this->resource)));
}

public function testInvokeWrongType(): void
{
$this->expectException(BadRequestException::class);
$this->resource->uri('app://self/stone')(['id' => '']);
}
}
Loading

0 comments on commit 8a0bdca

Please # to comment.