Skip to content

Commit

Permalink
test: add model json serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 21, 2023
1 parent 5141476 commit 3827a4b
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 14 deletions.
4 changes: 2 additions & 2 deletions includes/model/class-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function __construct(
* @return mixed Data which can be serialized by json_encode, which is a
* value of any type other than a resource.
*/
public function jsonSerialize(): mixed {
public function jsonSerialize() {
return array_merge(
$this->collect_meta(),
array(
Expand Down Expand Up @@ -300,7 +300,7 @@ protected function validate_message() {
*
* @return mixed The metadata of the message.
*/
protected function collect_meta(): mixed {
protected function collect_meta() {
$metadata = array();

foreach ( self::$meta_keys as $key ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/model/class-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __construct(
$channel_name = null,
$message_id = null,
$user_id = null,
$context = 'admin',
$context = 'adminbar',
$created_at = null,
$dismissed_at = null,
$displayed_at = null,
Expand All @@ -108,7 +108,7 @@ public function __construct(
* @return mixed Data which can be serialized by json_encode, which is a
* value of any type other than a resource.
*/
public function jsonSerialize(): mixed {
public function jsonSerialize() {
return array(
'channel_name' => $this->channel_name,
'context' => $this->context,
Expand Down
21 changes: 11 additions & 10 deletions includes/model/class-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public function __construct(
* @return mixed Data which can be serialized by json_encode, which is a
* value of any type other than a resource.
*/
public function jsonSerialize(): mixed {
public function jsonSerialize() {
return array(
'channel_name' => $this->channel_name,
'created_at' => Helper\Serde::maybe_serialize_json_date( $this->created_at ),
'user_id' => $this->user_id,
'snoozed_until' => Helper\Serde::maybe_serialize_json_date( $this->snoozed_until ),
'user_id' => $this->user_id,
);
}

Expand All @@ -107,20 +107,21 @@ public function get_created_at(): ?DateTime {
}

/**
* Get the user ID.
* Get the snoozed until option.
*
* @return ?int The user ID of the subscription.
* @return ?DateTime The snoozed until option of the subscription.
*/
public function get_user_id(): ?int {
return $this->user_id;
public function get_snoozed_until(): ?DateTime {
return $this->snoozed_until;
}

/**
* Get the snoozed until option.
* Get the user ID.
*
* @return ?DateTime The snoozed until option of the subscription.
* @return ?int The user ID of the subscription.
*/
public function get_snoozed_until(): ?DateTime {
return $this->snoozed_until;
public function get_user_id(): ?int {
return $this->user_id;
}

}
53 changes: 53 additions & 0 deletions tests/phpunit/tests/model/test-model-channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace WP\Notifications\Test;

use WP_UnitTestCase;
use WP\Notifications\Model;

class Test_Model_Channel extends WP_UnitTestCase {

/**
* Test channel model.
*/
private ?Model\Channel $model = null;

/**
* Set up each test method.
*/
public function set_up() {
parent::set_up();

$this->model = new Model\Channel(
'core/test',
'Test channel',
'test',
'A test case channel',
'WordPress'
);
}

/**
* Tear down each test method.
*/
public function tear_down() {
$this->model = null;

parent::tear_down();
}

/**
* Should be JSON serializable.
*/
public function test_json_serializable() {
$actual = json_encode( $this->model, JSON_PRETTY_PRINT );
$expected = '{
"context": "test",
"description": "A test case channel",
"icon": "WordPress",
"name": "core\/test",
"title": "Test channel"
}';
$this->assertEquals( $actual, $expected );
}
}
66 changes: 66 additions & 0 deletions tests/phpunit/tests/model/test-model-message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace WP\Notifications\Test;

use WP_UnitTestCase;
use WP\Notifications\Model;

class Test_Model_Message extends WP_UnitTestCase {

/**
* Test message model.
*/
private ?Model\Message $model = null;

/**
* Set up each test method.
*/
public function set_up() {
parent::set_up();

$this->model = new Model\Message(
'Testing, testings... 1, 2, 3... testing',
'Ok',
null,
'Test channel',
null,
'Nope',
null,
'hammer',
null,
true,
'warning',
'Message model test'
);
}

/**
* Tear down each test method.
*/
public function tear_down() {
$this->model = null;

parent::tear_down();
}

/**
* Should be JSON serializable.
*/
public function test_json_serializable() {
$actual = json_encode( $this->model, JSON_PRETTY_PRINT );
$expected = '{
"accept_label": "Ok",
"channel_title": "Test channel",
"dismiss_label": "Nope",
"icon": "hammer",
"is_dismissible": true,
"severity": "warning",
"created_at": null,
"expires_at": null,
"id": null,
"message": "Testing, testings... 1, 2, 3... testing",
"title": "Message model test"
}';
$this->assertEquals( $actual, $expected );
}
}
60 changes: 60 additions & 0 deletions tests/phpunit/tests/model/test-model-notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace WP\Notifications\Test;

use DateTime;
use WP_UnitTestCase;
use WP\Notifications\Model;

class Test_Model_Notification extends WP_UnitTestCase {

/**
* Test message model.
*/
private ?Model\Notification $model = null;

/**
* Set up each test method.
*/
public function set_up() {
parent::set_up();

$this->model = new Model\Notification(
'core/test',
1,
1,
'adminbar',
null,
null,
null,
new DateTime( '2023-12-31' )
);
}

/**
* Tear down each test method.
*/
public function tear_down() {
$this->model = null;

parent::tear_down();
}

/**
* Should be JSON serializable.
*/
public function test_json_serializable() {
$actual = json_encode( $this->model, JSON_PRETTY_PRINT );
$expected = '{
"channel_name": "core\/test",
"context": "adminbar",
"created_at": null,
"dismissed_at": null,
"displayed_at": null,
"expires_at": "2023-12-31T00:00:00+00:00",
"message_id": 1,
"user_id": 1
}';
$this->assertEquals( $actual, $expected );
}
}
52 changes: 52 additions & 0 deletions tests/phpunit/tests/model/test-model-subscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace WP\Notifications\Test;

use DateTime;
use WP_UnitTestCase;
use WP\Notifications\Model;

class Test_Model_Subscription extends WP_UnitTestCase {

/**
* Test message model.
*/
private ?Model\Subscription $model = null;

/**
* Set up each test method.
*/
public function set_up() {
parent::set_up();

$this->model = new Model\Subscription(
'core/test',
1,
null,
new DateTime( '2023-12-31' )
);
}

/**
* Tear down each test method.
*/
public function tear_down() {
$this->model = null;

parent::tear_down();
}

/**
* Should be JSON serializable.
*/
public function test_json_serializable() {
$actual = json_encode( $this->model, JSON_PRETTY_PRINT );
$expected = '{
"channel_name": "core\/test",
"created_at": null,
"snoozed_until": "2023-12-31T00:00:00+00:00",
"user_id": 1
}';
$this->assertEquals( $actual, $expected );
}
}

0 comments on commit 3827a4b

Please sign in to comment.