Skip to content

Commit

Permalink
Merge branch 'next-26632/make-annotate-package-handler-a-processor' i…
Browse files Browse the repository at this point in the history
…nto 'trunk'

NEXT-26632 - Refactor annotate package into a processor

See merge request shopware/6/product/platform!10986
  • Loading branch information
pweyck committed Jul 7, 2023
2 parents 0e0dca5 + 3620f43 commit 086770f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Shopware\Core\Framework\Log\Monolog;

use Monolog\Handler\AbstractHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Shopware\Core\Framework\Log\Package;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -14,26 +13,27 @@
* @internal
*/
#[Package('core')]
class AnnotatePackageHandler extends AbstractHandler
class AnnotatePackageProcessor implements ProcessorInterface
{
/**
* @internal
*/
public function __construct(
private readonly HandlerInterface $handler,
private readonly RequestStack $requestStack,
private readonly ContainerInterface $container,
) {
parent::__construct();
}

public function handle(LogRecord $record): bool
/**
* {@inheritdoc}
*/
public function __invoke(LogRecord $record)
{
$packages = [];

$exception = $record->context['exception'] ?? null;
if ($exception instanceof \ErrorException && str_starts_with($exception->getMessage(), 'User Deprecated:')) {
return $this->handler->handle($record);
return $record;
}

if ($controllerPackage = $this->getControllerPackage()) {
Expand All @@ -55,21 +55,10 @@ public function handle(LogRecord $record): bool
}

if ($packages !== []) {
$context = $record->context;
$context[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = $packages;

$record = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context,
$record->extra,
$record->formatted
);
$record->extra[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = $packages;
}

return $this->handler->handle($record);
return $record;
}

private function getControllerPackage(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Log\Monolog\AnnotatePackageHandler;
use Shopware\Core\Framework\Log\Monolog\AnnotatePackageProcessor;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\ShopwareHttpException;
use Symfony\Component\Console\Command\Command;
Expand All @@ -18,19 +18,19 @@
use Symfony\Component\HttpFoundation\Response;

/**
* @covers \Shopware\Core\Framework\Log\Monolog\AnnotatePackageHandler
* @covers \Shopware\Core\Framework\Log\Monolog\AnnotatePackageProcessor
*
* @internal
*/
#[Package('cause')]
class AnnotatePackageHandlerTest extends TestCase
class AnnotatePackageProcessorTest extends TestCase
{
public function testOnlyController(): void
{
$requestStack = new RequestStack();
$inner = $this->createMock(AbstractHandler::class);
$container = $this->createMock(ContainerInterface::class);
$handler = new AnnotatePackageHandler($inner, $requestStack, $container);
$handler = new AnnotatePackageProcessor($requestStack, $container);

$request = new Request();
$request->attributes->set('_controller', TestController::class . '::load');
Expand All @@ -43,30 +43,26 @@ public function testOnlyController(): void
'Some message'
);

$context[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'controller',
];

$expected = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context
[]
);
$expected->extra[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'controller',
];

$inner->expects(static::once())
->method('handle')
->with($expected);
$handler->handle($record);
static::assertEquals($expected, $handler($record));
}

public function testOnlyControllerWithNonClassServiceId(): void
{
$requestStack = new RequestStack();
$inner = $this->createMock(AbstractHandler::class);
$container = $this->createMock(ContainerInterface::class);
$handler = new AnnotatePackageHandler($inner, $requestStack, $container);
$handler = new AnnotatePackageProcessor($requestStack, $container);

$request = new Request();
$request->attributes->set('_controller', 'test.controller::load');
Expand All @@ -83,30 +79,26 @@ public function testOnlyControllerWithNonClassServiceId(): void
'Some message'
);

$context[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'controller',
];

$expected = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context
[]
);
$expected->extra[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'controller',
];

$inner->expects(static::once())
->method('handle')
->with($expected);
$handler->handle($record);
static::assertEquals($expected, $handler($record));
}

public function testOnlyControllerWithInvalidServiceId(): void
{
$requestStack = new RequestStack();
$inner = $this->createMock(AbstractHandler::class);
$container = $this->createMock(ContainerInterface::class);
$handler = new AnnotatePackageHandler($inner, $requestStack, $container);
$handler = new AnnotatePackageProcessor($requestStack, $container);

$request = new Request();
$request->attributes->set('_controller', 'test.controller::load');
Expand All @@ -123,18 +115,15 @@ public function testOnlyControllerWithInvalidServiceId(): void
'Some message'
);

$inner->expects(static::once())
->method('handle')
->with($record);
$handler->handle($record);
static::assertEquals($record, $handler($record));
}

public function testExceptionInController(): void
{
$requestStack = new RequestStack();
$inner = $this->createMock(AbstractHandler::class);
$container = $this->createMock(ContainerInterface::class);
$handler = new AnnotatePackageHandler($inner, $requestStack, $container);
$handler = new AnnotatePackageProcessor($requestStack, $container);

$request = new Request();
$request->attributes->set('_controller', TestController::class . '::load');
Expand All @@ -158,32 +147,28 @@ public function testExceptionInController(): void
$context
);

$context[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'controller',
'exception' => 'exception',
'causingClass' => 'cause',
];

$expected = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context
);
$expected->extra[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'controller',
'exception' => 'exception',
'causingClass' => 'cause',
];

$inner->expects(static::once())
->method('handle')
->with($expected);
$handler->handle($record);
static::assertEquals($expected, $handler($record));
}

public function testNoPackageAttributes(): void
{
$requestStack = new RequestStack();
$inner = $this->createMock(AbstractHandler::class);
$container = $this->createMock(ContainerInterface::class);
$handler = new AnnotatePackageHandler($inner, $requestStack, $container);
$handler = new AnnotatePackageProcessor($requestStack, $container);

$request = new Request();
$request->attributes->set('_controller', TestControllerNoPackage::class . '::load');
Expand All @@ -207,22 +192,18 @@ public function testNoPackageAttributes(): void
$context
);

$context[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'causingClass' => 'cause',
];

$expected = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context
);
$expected->extra[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'causingClass' => 'cause',
];

$inner->expects(static::once())
->method('handle')
->with($expected);
$handler->handle($record);
static::assertEquals($expected, $handler($record));
}

public function testAnnotateCommand(): void
Expand All @@ -238,7 +219,7 @@ public function testAnnotateCommand(): void

$inner = $this->createMock(AbstractHandler::class);
$container = $this->createMock(ContainerInterface::class);
$handler = new AnnotatePackageHandler($inner, $this->createMock(RequestStack::class), $container);
$handler = new AnnotatePackageProcessor($this->createMock(RequestStack::class), $container);

$context = [
'exception' => $exception,
Expand All @@ -252,24 +233,20 @@ public function testAnnotateCommand(): void
$context
);

$context[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'command',
'exception' => 'exception',
'causingClass' => 'command',
];

$expected = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context
);
$expected->extra[Package::PACKAGE_TRACE_ATTRIBUTE_KEY] = [
'entrypoint' => 'command',
'exception' => 'exception',
'causingClass' => 'command',
];

$inner->expects(static::once())
->method('handle')
->with($expected);
$handler->handle($record);
static::assertEquals($expected, $handler($record));
}
}

Expand Down

0 comments on commit 086770f

Please sign in to comment.