|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Test\Unit\Helper\Error; |
| 9 | + |
| 10 | +use GraphQL\Error\ClientAware; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 13 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 14 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 15 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 16 | +use Magento\Framework\Phrase; |
| 17 | +use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter; |
| 18 | +use Magento\GraphQl\Helper\Error\ExceptionMessageFormatterInterface; |
| 19 | +use PHPUnit\Framework\MockObject\Exception; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class AggregateExceptionMessageFormatterTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ExceptionMessageFormatterInterface|MockObject |
| 27 | + */ |
| 28 | + private ExceptionMessageFormatterInterface $formatter; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var LocalizedException|LocalizedException&MockObject|MockObject |
| 32 | + */ |
| 33 | + private LocalizedException $exception; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Phrase|Phrase&MockObject|MockObject |
| 37 | + */ |
| 38 | + private Phrase $phrase; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Field|Field&MockObject|MockObject |
| 42 | + */ |
| 43 | + private Field $field; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ContextInterface|ContextInterface&MockObject|MockObject |
| 47 | + */ |
| 48 | + private ContextInterface $context; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ResolveInfo|ResolveInfo&MockObject|MockObject |
| 52 | + */ |
| 53 | + private ResolveInfo $info; |
| 54 | + |
| 55 | + /** |
| 56 | + * @return void |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + $this->formatter = $this->createMock(ExceptionMessageFormatterInterface::class); |
| 62 | + $this->exception = $this->createMock(LocalizedException::class); |
| 63 | + $this->phrase = $this->createMock(Phrase::class); |
| 64 | + $this->field = $this->createMock(Field::class); |
| 65 | + $this->context = $this->createMock(ContextInterface::class); |
| 66 | + $this->info = $this->createMock(ResolveInfo::class); |
| 67 | + |
| 68 | + parent::setUp(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return void |
| 73 | + * @throws Exception |
| 74 | + */ |
| 75 | + public function testGetFormattedUsingFormatter(): void |
| 76 | + { |
| 77 | + $clientAware = $this->createMock(ClientAware::class); |
| 78 | + $this->formatter->expects($this->once()) |
| 79 | + ->method('getFormatted') |
| 80 | + ->willReturn($clientAware); |
| 81 | + $messagePrefix = 'prefix'; |
| 82 | + |
| 83 | + $aggregateFormatter = new AggregateExceptionMessageFormatter([$this->formatter]); |
| 84 | + $this->assertSame( |
| 85 | + $clientAware, |
| 86 | + $aggregateFormatter->getFormatted( |
| 87 | + $this->exception, |
| 88 | + $this->phrase, |
| 89 | + $messagePrefix, |
| 90 | + $this->field, |
| 91 | + $this->context, |
| 92 | + $this->info |
| 93 | + ) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + public function testGetFormattedExceptionMessage(): void |
| 101 | + { |
| 102 | + $exceptionCode = 1; |
| 103 | + $exceptionMessage = 'exception message'; |
| 104 | + $messagePrefix = 'prefix'; |
| 105 | + $this->formatter->expects($this->once()) |
| 106 | + ->method('getFormatted') |
| 107 | + ->willReturn(null); |
| 108 | + $paramException = new LocalizedException(__($exceptionMessage), null, $exceptionCode); |
| 109 | + |
| 110 | + $aggregateFormatter = new AggregateExceptionMessageFormatter([$this->formatter]); |
| 111 | + $exception = $aggregateFormatter->getFormatted( |
| 112 | + $paramException, |
| 113 | + $this->phrase, |
| 114 | + $messagePrefix, |
| 115 | + $this->field, |
| 116 | + $this->context, |
| 117 | + $this->info |
| 118 | + ); |
| 119 | + $this->assertInstanceOf(GraphQlInputException::class, $exception); |
| 120 | + $this->assertSame($exceptionCode, $exception->getCode()); |
| 121 | + $this->assertSame($exceptionMessage, $exception->getMessage()); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + public function testGetFormattedDefaultMessage(): void |
| 128 | + { |
| 129 | + $exceptionMessage = 'exception message'; |
| 130 | + $messagePrefix = 'prefix'; |
| 131 | + $this->formatter->expects($this->once()) |
| 132 | + ->method('getFormatted') |
| 133 | + ->willReturn(null); |
| 134 | + $paramException = new LocalizedException(__($exceptionMessage)); |
| 135 | + |
| 136 | + $this->phrase->expects($this->once()) |
| 137 | + ->method('render') |
| 138 | + ->willReturn('prefix default'); |
| 139 | + $aggregateFormatter = new AggregateExceptionMessageFormatter([$this->formatter]); |
| 140 | + $exception = $aggregateFormatter->getFormatted( |
| 141 | + $paramException, |
| 142 | + $this->phrase, |
| 143 | + $messagePrefix, |
| 144 | + $this->field, |
| 145 | + $this->context, |
| 146 | + $this->info |
| 147 | + ); |
| 148 | + $this->assertInstanceOf(GraphQlInputException::class, $exception); |
| 149 | + $this->assertSame('prefix default', $exception->getMessage()); |
| 150 | + } |
| 151 | +} |
0 commit comments