Skip to content

Commit

Permalink
Merge pull request #108 from jingu/fix-command-error
Browse files Browse the repository at this point in the history
Fix refresh command issue on ResourceObject has an error code
  • Loading branch information
koriym committed Mar 25, 2022
2 parents dbfb481 + 354f6dd commit 44be4ea
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/continuous-integration.yml
Expand Up @@ -41,6 +41,12 @@ jobs:
coverage: pcov
ini-values: zend.assertions=1

- name: Install pecl memcached
continue-on-error: true
run: |
sudo apt-get install -y libmemcached-dev
sudo pecl install memcached
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -27,7 +27,7 @@
"bamarni/composer-bin-plugin": "^1.4",
"koriym/attributes": "^1.0.1",
"madapaja/twig-module": "^2.3",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5.19",
"psr/log": "^1.1",
"ray/object-visual-grapher": "^1.0",
"symfony/process": "^4.3",
Expand Down
2 changes: 1 addition & 1 deletion src/CommandInterceptor.php
Expand Up @@ -43,7 +43,7 @@ public function invoke(MethodInvocation $invocation)
}

if ($ro->code >= Code::BAD_REQUEST) {
return $this;
return $ro;
}

foreach ($this->commands as $command) {
Expand Down
3 changes: 3 additions & 0 deletions src/CommandsProvider.php
Expand Up @@ -7,6 +7,9 @@
use BEAR\Resource\ResourceInterface;
use Ray\Di\ProviderInterface;

/**
* @implements ProviderInterface<array<CommandInterface>>
*/
final class CommandsProvider implements ProviderInterface
{
/** @var QueryRepositoryInterface */
Expand Down
5 changes: 5 additions & 0 deletions src/DonutCommandInterceptor.php
Expand Up @@ -6,6 +6,7 @@

use BEAR\QueryRepository\Exception\UnmatchedQuery;
use BEAR\Resource\AbstractUri;
use BEAR\Resource\Code;
use BEAR\Resource\ResourceObject;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
Expand Down Expand Up @@ -36,6 +37,10 @@ public function invoke(MethodInvocation $invocation): ResourceObject
{
$ro = $invocation->proceed();
assert($ro instanceof ResourceObject);
if ($ro->code >= Code::BAD_REQUEST) {
return $ro;
}

$this->refreshDonutAndState($ro);

return $ro;
Expand Down
16 changes: 16 additions & 0 deletions tests/DonutCommandInterceptorTest.php
Expand Up @@ -4,6 +4,7 @@

namespace BEAR\QueryRepository;

use BEAR\Resource\Code;
use BEAR\Resource\ResourceInterface;
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface as HttpCacheInterfaceAlias;
use Madapaja\TwigModule\TwigModule;
Expand Down Expand Up @@ -69,6 +70,21 @@ public function testCommandInterceptorRefresh(): void
$this->assertArrayHasKey('Age', $ro->headers);
}

public function testCommandInterceptorRefreshOnErrorCode(): void
{
$this->resource->get('page://self/html/comment');
$ro = $this->resource->delete('page://self/html/comment');
$this->assertSame(Code::BAD_REQUEST, $ro->code);
$ro = $this->resource->get('page://self/html/comment');
$this->assertArrayHasKey('Age', $ro->headers);

$this->resource->get('page://self/html/blog-posting?id=0');
$ro = $this->resource->delete('page://self/html/blog-posting', ['id' => 9999]);
$this->assertSame(Code::BAD_REQUEST, $ro->code);
$ro = $this->resource->get('page://self/html/blog-posting?id=0');
$this->assertArrayHasKey('Age', $ro->headers);
}

public function testCacheableResponse(): void
{
$ro = $this->resource->get('page://self/html/blog-posting-cache?id=0');
Expand Down
9 changes: 8 additions & 1 deletion tests/Fake/fake-app/src/Resource/Page/Html/BlogPosting.php
Expand Up @@ -5,6 +5,7 @@
use BEAR\QueryRepository\Header;
use BEAR\RepositoryModule\Annotation\CacheableResponse;
use BEAR\Resource\Annotation\Embed;
use BEAR\Resource\Code;
use BEAR\Resource\ResourceObject;
use Koriym\HttpConstants\CacheControl;
use Koriym\HttpConstants\ResponseHeader;
Expand All @@ -30,6 +31,12 @@ public function onGet(int $id = 0)

public function onDelete(int $id = 0)
{
if ($id !== 0) {
$this->code = Code::BAD_REQUEST;

return $this;
}

return $this;
}
}
}
10 changes: 9 additions & 1 deletion tests/Fake/fake-app/src/Resource/Page/Html/Comment.php
Expand Up @@ -4,6 +4,7 @@

use BEAR\RepositoryModule\Annotation\Cacheable;
use BEAR\Resource\Annotation\Embed;
use BEAR\Resource\Code;
use BEAR\Resource\ResourceObject;

/**
Expand All @@ -24,4 +25,11 @@ public function onGet()

return $this;
}
}

public function onDelete()
{
$this->code = Code::BAD_REQUEST;

return $this;
}
}

0 comments on commit 44be4ea

Please sign in to comment.