Skip to content

Commit

Permalink
Change JPush report to fetch responses easier
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstoop authored and pprkut committed Jun 19, 2023
1 parent 15d6746 commit e088022
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
17 changes: 7 additions & 10 deletions src/Lunr/Vortex/JPush/JPushReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/Lunr/Vortex/JPush/Tests/JPushReportGetReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []);

Expand Down Expand Up @@ -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 ]);
}
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 13 additions & 23 deletions src/Lunr/Vortex/JPush/Tests/JPushReportGetStatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Lunr\Vortex\JPush\Tests;

use Lunr\Vortex\PushNotificationStatus;

/**
* This class contains test for the constructor of the JPushReport class.
*
Expand All @@ -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);
}

}
Expand Down

0 comments on commit e088022

Please sign in to comment.