Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d867a45

Browse filesBrowse files
committed
bug #43408 [Notifier] Fix 'Undefined array key' error in FirebaseTransport (villfa)
This PR was merged into the 5.3 branch. Discussion ---------- [Notifier] Fix 'Undefined array key' error in FirebaseTransport | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #42841 | License | MIT | Doc PR | n/a This modification fixes a remaining `Undefined array key` error in *FirebaseTransport*. It also add a test to cover #42690. <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry --> Commits ------- 53f836e [Notifier] Fix 'Undefined array key' error in FirebaseTransport
2 parents 3579fe2 + 53f836e commit d867a45
Copy full SHA for d867a45

File tree

2 files changed

+42
-5
lines changed
Filter options

2 files changed

+42
-5
lines changed

‎src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseTransport.php
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,17 @@ protected function doSend(MessageInterface $message): SentMessage
8383

8484
$contentType = $response->getHeaders(false)['content-type'][0] ?? '';
8585
$jsonContents = 0 === strpos($contentType, 'application/json') ? $response->toArray(false) : null;
86+
$errorMessage = null;
8687

87-
if (200 !== $statusCode) {
88-
$errorMessage = $jsonContents ? $jsonContents['results']['error'] : $response->getContent(false);
88+
if ($jsonContents && isset($jsonContents['results'][0]['error'])) {
89+
$errorMessage = $jsonContents['results'][0]['error'];
90+
} elseif (200 !== $statusCode) {
91+
$errorMessage = $response->getContent(false);
92+
}
8993

94+
if (null !== $errorMessage) {
9095
throw new TransportException('Unable to post the Firebase message: '.$errorMessage, $response);
9196
}
92-
if ($jsonContents && isset($jsonContents['results'][0]['error'])) {
93-
throw new TransportException('Unable to post the Firebase message: '.$jsonContents['results'][0]['error'], $response);
94-
}
9597

9698
$success = $response->toArray(false);
9799

‎src/Symfony/Component/Notifier/Bridge/Firebase/Tests/FirebaseTransportTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Firebase/Tests/FirebaseTransportTest.php
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Firebase\Tests;
1313

14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
16+
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
1417
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransport;
18+
use Symfony\Component\Notifier\Exception\TransportException;
1519
use Symfony\Component\Notifier\Message\ChatMessage;
1620
use Symfony\Component\Notifier\Message\MessageInterface;
1721
use Symfony\Component\Notifier\Message\SmsMessage;
1822
use Symfony\Component\Notifier\Test\TransportTestCase;
1923
use Symfony\Component\Notifier\Transport\TransportInterface;
2024
use Symfony\Contracts\HttpClient\HttpClientInterface;
25+
use Symfony\Contracts\HttpClient\ResponseInterface;
2126

2227
/**
2328
* @author Oskar Stark <oskarstark@googlemail.com>
@@ -47,4 +52,34 @@ public function unsupportedMessagesProvider(): iterable
4752
yield [new SmsMessage('0611223344', 'Hello!')];
4853
yield [$this->createMock(MessageInterface::class)];
4954
}
55+
56+
/**
57+
* @dataProvider sendWithErrorThrowsExceptionProvider
58+
*/
59+
public function testSendWithErrorThrowsTransportException(ResponseInterface $response)
60+
{
61+
$this->expectException(TransportException::class);
62+
63+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
64+
return $response;
65+
});
66+
$options = new class('recipient-id', []) extends FirebaseOptions {};
67+
68+
$transport = $this->createTransport($client);
69+
70+
$transport->send(new ChatMessage('Hello!', $options));
71+
}
72+
73+
public function sendWithErrorThrowsExceptionProvider(): iterable
74+
{
75+
yield [new MockResponse(
76+
json_encode(['results' => [['error' => 'testErrorCode']]]),
77+
['response_headers' => ['content-type' => ['application/json']], 'http_code' => 200]
78+
)];
79+
80+
yield [new MockResponse(
81+
json_encode(['results' => [['error' => 'testErrorCode']]]),
82+
['response_headers' => ['content-type' => ['application/json']], 'http_code' => 400]
83+
)];
84+
}
5085
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.