From e088022b89b2c21254691739b762d7fdf85468ed Mon Sep 17 00:00:00 2001 From: Brian Stoop Date: Tue, 13 Jun 2023 14:33:45 +0200 Subject: [PATCH] Change JPush report to fetch responses easier Reviewed at https://reviews.lunr.nl/r/961/ --- src/Lunr/Vortex/JPush/JPushReport.php | 17 ++++----- .../JPush/Tests/JPushReportGetReportTest.php | 9 ++--- .../Tests/JPushReportGetStatusesTest.php | 36 +++++++------------ 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/src/Lunr/Vortex/JPush/JPushReport.php b/src/Lunr/Vortex/JPush/JPushReport.php index a20ce0e..ef8af1c 100644 --- a/src/Lunr/Vortex/JPush/JPushReport.php +++ b/src/Lunr/Vortex/JPush/JPushReport.php @@ -80,7 +80,7 @@ public function __destruct() * * @return void */ - private function get_report($message_id, $endpoints): void + public function get_report($message_id, $endpoints): void { $payload = [ 'msg_id' => $message_id, @@ -123,18 +123,15 @@ private function get_report($message_id, $endpoints): void } /** - * Get notification delivery statuses. + * Get notification delivery status for an endpoint. * - * @return array Delivery statuses for the endpoints + * @param string $endpoint Endpoint + * + * @return PushNotificationStatus::* Delivery status for the endpoint */ - public function get_statuses(): array + public function get_status(string $endpoint): int { - if ($this->statuses === []) - { - $this->get_report(); - } - - return $this->statuses; + return $this->statuses[$endpoint] ?? PushNotificationStatus::UNKNOWN; } /** diff --git a/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php b/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php index cec5a73..6cc50a1 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php @@ -44,8 +44,7 @@ public function testGetReportReturnsWhenHttpRequestFails(): void $this->expectOutputString('400'); - $method = $this->get_accessible_reflection_method('get_report'); - $method->invokeArgs($this->class, [ 1453658564165, [ 'endpoint1' ] ]); + $this->class->get_report(1453658564165, [ 'endpoint1' ]); $this->assertPropertyEquals('statuses', []); @@ -76,8 +75,7 @@ public function testGetReportWithCurlErrors(): void ->method('warning') ->with('Getting JPush notification report failed: {error}', $context); - $method = $this->get_accessible_reflection_method('get_report'); - $method->invokeArgs($this->class, [ 1453658564165, [ 'endpoint1' ] ]); + $this->class->get_report(1453658564165, [ 'endpoint1' ]); $this->assertPropertyEquals('statuses', [ 'endpoint1' => 5 ]); } @@ -115,8 +113,7 @@ public function testGetReportWillFetchUpstreamMixedErrorSuccess(): void [ $log_message, [ 'endpoint' => 'endpoint6','error' => 6 ]] ); - $method = $this->get_accessible_reflection_method('get_report'); - $method->invokeArgs($this->class, [ 1453658564165, $endpoints ]); + $this->class->get_report(1453658564165, $endpoints); $this->assertPropertyEquals('statuses', [ 'endpoint1' => 0, diff --git a/src/Lunr/Vortex/JPush/Tests/JPushReportGetStatusesTest.php b/src/Lunr/Vortex/JPush/Tests/JPushReportGetStatusesTest.php index ee97cd0..042f669 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushReportGetStatusesTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushReportGetStatusesTest.php @@ -11,6 +11,8 @@ namespace Lunr\Vortex\JPush\Tests; +use Lunr\Vortex\PushNotificationStatus; + /** * This class contains test for the constructor of the JPushReport class. * @@ -20,43 +22,31 @@ class JPushReportGetStatusesTest extends JPushReportTest { /** - * Test that get_statuses calls get report and returns statuses. + * Test that get_status returns status. * - * @covers \Lunr\Vortex\JPush\JPushReport::get_statuses + * @covers \Lunr\Vortex\JPush\JPushReport::get_status */ - public function testGetStatusesCallsGetReport(): void + public function testGetStatusReturnsStatus(): void { - $this->mock_method([ $this->class, 'get_report' ], function () { echo 'get_report'; }); - - $this->set_reflection_property_value('statuses', []); - - $this->expectOutputString('get_report'); - - $result = $this->class->get_statuses(); + $this->set_reflection_property_value('statuses', [ 'endpoint1' => 1 ]); - $this->assertSame([], $result); + $result = $this->class->get_status('endpoint1'); - $this->unmock_method([ $this->class, 'get_report' ]); + $this->assertSame(PushNotificationStatus::SUCCESS, $result); } /** - * Test that get_statuses does not call get_report but returns statuses + * Test that get_status returns UNKNOWN status if endpoint does not exists * - * @covers \Lunr\Vortex\JPush\JPushReport::get_statuses + * @covers \Lunr\Vortex\JPush\JPushReport::get_status */ - public function testGetStatusesDoesNotCallGetReportButReturnsStatuses(): void + public function testGetStatusReturnsUnknownIfEndpointDoesNotExists(): void { - $this->mock_method([ $this->class, 'get_report' ], function () { echo 'get_report'; }); - $this->set_reflection_property_value('statuses', [ 'endpoint1' => 1 ]); - $this->expectOutputString(''); - - $result = $this->class->get_statuses(); - - $this->assertSame([ 'endpoint1' => 1 ], $result); + $result = $this->class->get_status('endpoint_unknown'); - $this->unmock_method([ $this->class, 'get_report' ]); + $this->assertSame(PushNotificationStatus::UNKNOWN, $result); } }