Skip to content

Commit

Permalink
FCM: Change get/set platform payload to reference
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstoop committed Apr 12, 2024
1 parent 6d113ea commit a7a7481
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 158 deletions.
54 changes: 8 additions & 46 deletions src/Lunr/Vortex/FCM/FCMPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,65 +192,27 @@ public function set_token(string $token): self
}

/**
* Get the android payload element.
* Returns a reference of the android payload element.
*
* @return FCMAndroidPayload
*/
public function get_android_payload(): FCMAndroidPayload
public function android_payload(): FCMAndroidPayload
{
return $this->android_payload ?? new FCMAndroidPayload();
}

/**
* Set the android payload element.
*
* @param array|FCMAndroidPayload $payload The android payload element
*
* @return self self Reference
*/
public function set_android_payload(array|FCMAndroidPayload $payload): self
{
if (is_array($payload))
{
$this->elements['android'] = $payload;

return $this;
}

$this->android_payload = $payload;
$this->android_payload ??= new FCMAndroidPayload();

return $this;
return $this->android_payload;
}

/**
* Get the apns payload element.
* Returns a reference the apns payload element.
*
* @return FCMApnsPayload
*/
public function get_apns_payload(): FCMApnsPayload
public function apns_payload(): FCMApnsPayload
{
return $this->apns_payload ?? new FCMApnsPayload();
}

/**
* Set the apns payload element.
*
* @param array|FCMApnsPayload $payload The android payload element
*
* @return self self Reference
*/
public function set_apns_payload(array|FCMApnsPayload $payload): self
{
if (is_array($payload))
{
$this->elements['apns'] = $payload;

return $this;
}

$this->apns_payload = $payload;
$this->apns_payload ??= new FCMApnsPayload();

return $this;
return $this->apns_payload;
}

}
Expand Down
40 changes: 26 additions & 14 deletions src/Lunr/Vortex/FCM/Tests/FCMPayloadGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,47 +151,59 @@ public function testGetJsonPayloadWithApns(): void
}

/**
* Test get_android_payload() returns new payload.
* Test android_payload() returns new payload.
*
* @covers Lunr\Vortex\FCM\FCMPayload::get_android_payload
* @covers Lunr\Vortex\FCM\FCMPayload::android_payload
*/
public function testGetAndroidPayloadReturnsNewPayload(): void
public function testAndroidPayloadReturnsNewPayload(): void
{
$this->assertInstanceOf(FCMAndroidPayload::class, $this->class->get_android_payload());
$return = $this->class->android_payload();

$this->assertInstanceOf(FCMAndroidPayload::class, $return);

$property_value = $this->get_reflection_property_value('android_payload');

$this->assertSame($return, $property_value);
}

/**
* Test get_android_payload() returns saved payload.
* Test android_payload() returns saved payload.
*
* @covers Lunr\Vortex\FCM\FCMPayload::get_android_payload
* @covers Lunr\Vortex\FCM\FCMPayload::android_payload
*/
public function testGetAndroidPayloadReturnsSavedPayload(): void
public function testAndroidPayloadReturnsSavedPayload(): void
{
$this->set_reflection_property_value('android_payload', $this->android_payload);

$this->assertSame($this->android_payload, $this->class->get_android_payload());
$this->assertSame($this->android_payload, $this->class->android_payload());
}

/**
* Test get_apns_payload() returns new payload.
* Test apns_payload() returns new payload.
*
* @covers Lunr\Vortex\FCM\FCMPayload::get_apns_payload
* @covers Lunr\Vortex\FCM\FCMPayload::apns_payload
*/
public function testGetApnsPayloadReturnsNewPayload(): void
{
$this->assertInstanceOf(FCMApnsPayload::class, $this->class->get_apns_payload());
$return = $this->class->apns_payload();

$this->assertInstanceOf(FCMApnsPayload::class, $return);

$property_value = $this->get_reflection_property_value('apns_payload');

$this->assertSame($return, $property_value);
}

/**
* Test get_apns_payload() returns saved payload.
* Test apns_payload() returns saved payload.
*
* @covers Lunr\Vortex\FCM\FCMPayload::get_apns_payload
* @covers Lunr\Vortex\FCM\FCMPayload::apns_payload
*/
public function testGetApnsPayloadReturnsSavedPayload(): void
{
$this->set_reflection_property_value('apns_payload', $this->apns_payload);

$this->assertSame($this->apns_payload, $this->class->get_apns_payload());
$this->assertSame($this->apns_payload, $this->class->apns_payload());
}

}
Expand Down
98 changes: 0 additions & 98 deletions src/Lunr/Vortex/FCM/Tests/FCMPayloadSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,104 +169,6 @@ public function testSetTokenReturnsSelfReference()
$this->assertSame($this->class, $this->class->set_token('endpoint_token'));
}

/**
* Test set_android_payload() works correctly.
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_android_payload
*/
public function testSetAndroidPayloadWorksCorrectlyWithArray()
{
$this->class->set_android_payload([ 'notification' => 'test' ]);

$value = $this->get_reflection_property_value('elements');

$this->assertArrayHasKey('android', $value);
$this->assertEquals([ 'notification' => 'test' ], $value['android']);
}

/**
* Test fluid interface of set_android_payload().
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_android_payload
*/
public function testSetAndroidPayloadReturnsSelfReferenceWithArray()
{
$this->assertSame($this->class, $this->class->set_android_payload([ 'notification' => 'test' ]));
}

/**
* Test set_android_payload() works correctly.
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_android_payload
*/
public function testSetAndroidPayloadWorksCorrectlyWithAndroidPayload()
{
$this->class->set_android_payload($this->android_payload);

$value = $this->get_reflection_property_value('android_payload');

$this->assertEquals($this->android_payload, $value);
}

/**
* Test fluid interface of set_android_payload().
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_android_payload
*/
public function testSetAndroidPayloadReturnsSelfReferenceWithAndroidPayload()
{
$this->assertSame($this->class, $this->class->set_android_payload($this->android_payload));
}

/**
* Test set_apns_payload() works correctly.
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_apns_payload
*/
public function testSetApnsPayloadWorksCorrectlyWithArray()
{
$this->class->set_apns_payload([ 'notification' => 'test' ]);

$value = $this->get_reflection_property_value('elements');

$this->assertArrayHasKey('apns', $value);
$this->assertEquals([ 'notification' => 'test' ], $value['apns']);
}

/**
* Test fluid interface of set_apns_payload().
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_apns_payload
*/
public function testSetApnsPayloadReturnsSelfReferenceWithArray()
{
$this->assertSame($this->class, $this->class->set_apns_payload([ 'notification' => 'test' ]));
}

/**
* Test set_apns_payload() works correctly.
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_apns_payload
*/
public function testSetApnsPayloadWorksCorrectlyWithApnsPayload()
{
$this->class->set_apns_payload($this->apns_payload);

$value = $this->get_reflection_property_value('apns_payload');

$this->assertEquals($this->apns_payload, $value);
}

/**
* Test fluid interface of set_apns_payload().
*
* @covers \Lunr\Vortex\FCM\FCMPayload::set_apns_payload
*/
public function testSetApnsPayloadReturnsSelfReferenceWithApnsPayload()
{
$this->assertSame($this->class, $this->class->set_apns_payload($this->apns_payload));
}

}

?>

0 comments on commit a7a7481

Please sign in to comment.