Skip to content

Commit 7fde3f7

Browse files
committed
ACP2E-3399: GraphQL Response for Order placement does not include the exception message
1 parent 40dcba7 commit 7fde3f7

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Test\Unit\Model;
9+
10+
use Magento\QuoteGraphQl\Model\ErrorMapper;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class ErrorMapperTest extends TestCase
14+
{
15+
/**
16+
* @var ErrorMapper
17+
*/
18+
private ErrorMapper $errorMapper;
19+
20+
/**
21+
* @return void
22+
*/
23+
protected function setUp(): void
24+
{
25+
$this->errorMapper = new ErrorMapper();
26+
parent::setUp();
27+
}
28+
29+
/**
30+
* @dataProvider dataProviderForTestGetErrorMessageId
31+
* @param $message
32+
* @param $expectedId
33+
* @return void
34+
*/
35+
public function testGetErrorMessageId($message, $expectedId): void
36+
{
37+
$this->assertEquals($expectedId, $this->errorMapper->getErrorMessageId($message));
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function dataProviderForTestGetErrorMessageId(): array
44+
{
45+
$data = [];
46+
foreach (ErrorMapper::MESSAGE_IDS as $code => $id) {
47+
$data[] = [$code, $id];
48+
}
49+
$data[] = ['Some random message', ErrorMapper::ERROR_UNDEFINED_ID];
50+
return $data;
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Test\Unit\Model;
9+
10+
use Magento\QuoteGraphQl\Model\ErrorMapper;
11+
use Magento\QuoteGraphQl\Model\QuoteException;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class QuoteExceptionTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider quoteExceptionDataProvider
18+
* @param int $errorId
19+
* @param string $code
20+
* @return void
21+
*/
22+
public function testGetExtensions(int $errorId, string $code): void
23+
{
24+
$exception = new QuoteException(__('test'), null, $errorId);
25+
$this->assertEquals($code, $exception->getExtensions()['error_code']);
26+
}
27+
28+
/**
29+
* @return array
30+
*/
31+
public function quoteExceptionDataProvider(): array
32+
{
33+
$data = [];
34+
foreach (ErrorMapper::MESSAGE_CODE_IDS as $id => $code) {
35+
$data[] = [$id, $code];
36+
}
37+
$data[] = [777, ErrorMapper::ERROR_UNDEFINED];
38+
39+
return $data;
40+
}
41+
}

0 commit comments

Comments
 (0)