Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.lock
vendor
.php_cs.cache
.php-cs-fixer.cache
.phpunit.result.cache
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"overblog/graphiql-bundle": "^0.2",
"phpspec/phpspec": "^7.5 || ^8.0",
"ibexa/code-style": "~1.2.0",
"mikey179/vfsstream": "^1.6"
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^9.5",
"symfony/phpunit-bridge": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -78,6 +80,11 @@
"scripts": {
"fix-cs": "php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots",
"check-cs": "@fix-cs --dry-run",
"test": "phpspec run --format=pretty"
"test-phpspec": "phpspec run --format=pretty",
"test-phpunit": "phpunit -c phpunit.xml.dist",
"test": [
"@test-phpspec",
"@test-phpunit"
]
}
}
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="lib">
<directory>tests/lib</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ services:
$provider: '@ibexa.siteaccess.provider'
$siteAccessGroups: '%ibexa.site_access.groups%'

Ibexa\GraphQL\EventListener\ArgumentsExceptionListener:
tags:
- { name: monolog.logger, channel: ibexa.graphql }

Ibexa\GraphQL\Mapper\ContentImageAssetMapperStrategy:
arguments:
$assetMapper: '@Ibexa\Core\FieldType\ImageAsset\AssetMapper'
Expand Down
46 changes: 46 additions & 0 deletions src/lib/EventListener/ArgumentsExceptionListener.php

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\GraphQL\EventListener;

use Ibexa\GraphQL\DataLoader\Exception\ArgumentsException;
use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
use Overblog\GraphQLBundle\Event\Events;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class ArgumentsExceptionListener implements EventSubscriberInterface
{
private LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public static function getSubscribedEvents(): array
{
return [
Events::ERROR_FORMATTING => ['onErrorFormatting', 10],
];
}

public function onErrorFormatting(ErrorFormattingEvent $event): void
{
$exception = $event->getError()->getPrevious();

if (!$exception instanceof ArgumentsException) {
return;
}

$this->logger->debug(
sprintf('[GraphQL] %s: %s', ArgumentsException::class, $exception->getMessage()),
['exception' => $exception]
);

$event->stopPropagation();
}
}
80 changes: 80 additions & 0 deletions tests/lib/EventListener/ArgumentsExceptionListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\GraphQL\EventListener;

use GraphQL\Error\Error;
use Ibexa\GraphQL\DataLoader\Exception\ArgumentsException;
use Ibexa\GraphQL\EventListener\ArgumentsExceptionListener;
use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
use Overblog\GraphQLBundle\Event\Events;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use RuntimeException;

final class ArgumentsExceptionListenerTest extends TestCase
{
/** @var \Psr\Log\LoggerInterface&\PHPUnit\Framework\MockObject\MockObject */
private LoggerInterface $logger;

private ArgumentsExceptionListener $listener;

protected function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
$this->listener = new ArgumentsExceptionListener($this->logger);
}

public function testGetSubscribedEvents(): void
{
self::assertSame(
[Events::ERROR_FORMATTING => ['onErrorFormatting', 10]],
ArgumentsExceptionListener::getSubscribedEvents(),
);
}

public function testOnErrorFormattingLogsAndStopsPropagationForArgumentsException(): void
{
$exception = new ArgumentsException('invalid arguments');
$event = $this->createErrorFormattingEvent($exception);

$this->logger
->expects(self::once())
->method('debug')
->with(
sprintf('[GraphQL] %s: %s', ArgumentsException::class, 'invalid arguments'),
['exception' => $exception],
);

$this->listener->onErrorFormatting($event);

self::assertTrue($event->isPropagationStopped());
}

public function testOnErrorFormattingIgnoresExceptionsThatAreNotArgumentsExceptions(): void
{
$exception = new RuntimeException('some other error');
$event = $this->createErrorFormattingEvent($exception);

$this->logger
->expects(self::never())
->method('debug');

$this->listener->onErrorFormatting($event);

self::assertFalse($event->isPropagationStopped());
}

private function createErrorFormattingEvent(\Throwable $exception): ErrorFormattingEvent
{
return new ErrorFormattingEvent(
new Error('Internal server error', null, null, [], null, $exception),
[],
);
}
}