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

add OptionsMethodModule #96

Merged
merged 12 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}
},
"scripts" :{
"test": ["phpunit"],
"test": ["@cs", "phpunit"],
"coverage": ["php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"],
"cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=./phpcs.xml src"],
"cs-fix": ["php-cs-fixer fix -v", "phpcbf src"]
Expand Down
12 changes: 6 additions & 6 deletions src/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function invoke(AbstractRequest $request)
{
$onMethod = 'on' . ucfirst($request->method);
if (method_exists($request->resourceObject, $onMethod) !== true) {
return $this->extraMethod($request->resourceObject, $request, $onMethod);
return $this->invokeOptions($request->resourceObject, $request, $onMethod);
}
if ($request->resourceObject->uri instanceof AbstractUri) {
$request->resourceObject->uri->query = $request->query;
Expand Down Expand Up @@ -76,14 +76,14 @@ private function postRequest(AbstractRequest $request, $result)
*
* @throws Exception\MethodNotAllowedException
*
* @return ResourceObject
* @return string
*/
private function extraMethod(ResourceObject $ro, AbstractRequest $request, $method)
private function invokeOptions(ResourceObject $ro, AbstractRequest $request, $method)
{
if ($request->method !== Request::OPTIONS) {
throw new MethodNotAllowedException(get_class($request->resourceObject) . "::$method()", 405);
if ($request->method == Request::OPTIONS) {
return $this->optionsRenderer->render($ro);
}

return $this->optionsRenderer->render($ro);
throw new MethodNotAllowedException(get_class($request->resourceObject) . "::$method()", 405);
}
}
22 changes: 22 additions & 0 deletions src/Module/OptionsMethodModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of the BEAR.Resource package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\Resource\Module;

use BEAR\Resource\OptionsRenderer;
use BEAR\Resource\RenderInterface;
use Ray\Di\AbstractModule;

class OptionsMethodModule extends AbstractModule
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->bind(RenderInterface::class)->annotatedWith('options')->to(OptionsRenderer::class);
}
}
22 changes: 22 additions & 0 deletions src/Module/VoidOptionsMethodModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of the BEAR.Resource package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\Resource\Module;

use BEAR\Resource\RenderInterface;
use BEAR\Resource\VoidOptionsRenderer;
use Ray\Di\AbstractModule;

class VoidOptionsMethodModule extends AbstractModule
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->bind(RenderInterface::class)->annotatedWith('options')->to(VoidOptionsRenderer::class);
}
}
20 changes: 20 additions & 0 deletions src/VoidOptionsRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This file is part of the BEAR.Resource package.
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace BEAR\Resource;

use BEAR\Resource\Exception\MethodNotAllowedException;

final class VoidOptionsRenderer implements RenderInterface
{
/**
* {@inheritdoc}
*/
public function render(ResourceObject $ro)
{
throw new MethodNotAllowedException(get_class($ro) . '::options', 405);
}
}
19 changes: 16 additions & 3 deletions tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace BEAR\Resource;

use BEAR\Resource\Exception\MethodNotAllowedException;
use BEAR\Resource\Exception\ParameterException;
use BEAR\Resource\Interceptor\FakeLogInterceptor;
use BEAR\Resource\Interceptor\Log;
Expand Down Expand Up @@ -167,18 +166,32 @@ public function testOptionsWeaver()
$this->assertSame($actual, $expected);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvokeExceptionHandle()
{
$this->setExpectedException(\InvalidArgumentException::class);
$outOfRangeId = 4;
$request = new Request($this->invoker, new User, Request::GET, ['id' => $outOfRangeId]);
$this->invoker->invoke($request);
}

/**
* @expectedException \BEAR\Resource\Exception\MethodNotAllowedException
*/
public function testInvalidMethod()
{
$this->setExpectedException(MethodNotAllowedException::class);
$request = new Request($this->invoker, new Order, Request::DELETE);
$this->invoker->invoke($request);
}

/**
* @expectedException \BEAR\Resource\Exception\MethodNotAllowedException
*/
public function testOptionsNotAllowed()
{
$invoker = new Invoker(new NamedParameter(new ArrayCache, new VoidParamHandler), new VoidOptionsRenderer);
$request = new Request($this->invoker, new Order, Request::DELETE);
$invoker->invoke($request);
}
}