From 3827a4befd99f3dfa1b10ebd45747d4c9167d582 Mon Sep 17 00:00:00 2001 From: John Hooks Date: Fri, 21 Apr 2023 09:49:25 -0700 Subject: [PATCH] test: add model json serialization tests --- includes/model/class-message.php | 4 +- includes/model/class-notification.php | 4 +- includes/model/class-subscription.php | 21 +++--- .../tests/model/test-model-channel.php | 53 +++++++++++++++ .../tests/model/test-model-message.php | 66 +++++++++++++++++++ .../tests/model/test-model-notification.php | 60 +++++++++++++++++ .../tests/model/test-model-subscription.php | 52 +++++++++++++++ 7 files changed, 246 insertions(+), 14 deletions(-) create mode 100644 tests/phpunit/tests/model/test-model-channel.php create mode 100644 tests/phpunit/tests/model/test-model-message.php create mode 100644 tests/phpunit/tests/model/test-model-notification.php create mode 100644 tests/phpunit/tests/model/test-model-subscription.php diff --git a/includes/model/class-message.php b/includes/model/class-message.php index fc6e5d08..998d4bdd 100644 --- a/includes/model/class-message.php +++ b/includes/model/class-message.php @@ -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( @@ -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 ) { diff --git a/includes/model/class-notification.php b/includes/model/class-notification.php index ebca8793..7c82211a 100644 --- a/includes/model/class-notification.php +++ b/includes/model/class-notification.php @@ -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, @@ -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, diff --git a/includes/model/class-subscription.php b/includes/model/class-subscription.php index c350e854..ae708622 100644 --- a/includes/model/class-subscription.php +++ b/includes/model/class-subscription.php @@ -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, ); } @@ -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; } + } diff --git a/tests/phpunit/tests/model/test-model-channel.php b/tests/phpunit/tests/model/test-model-channel.php new file mode 100644 index 00000000..6ac83916 --- /dev/null +++ b/tests/phpunit/tests/model/test-model-channel.php @@ -0,0 +1,53 @@ +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 ); + } +} diff --git a/tests/phpunit/tests/model/test-model-message.php b/tests/phpunit/tests/model/test-model-message.php new file mode 100644 index 00000000..33057193 --- /dev/null +++ b/tests/phpunit/tests/model/test-model-message.php @@ -0,0 +1,66 @@ +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 ); + } +} diff --git a/tests/phpunit/tests/model/test-model-notification.php b/tests/phpunit/tests/model/test-model-notification.php new file mode 100644 index 00000000..2dd74871 --- /dev/null +++ b/tests/phpunit/tests/model/test-model-notification.php @@ -0,0 +1,60 @@ +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 ); + } +} diff --git a/tests/phpunit/tests/model/test-model-subscription.php b/tests/phpunit/tests/model/test-model-subscription.php new file mode 100644 index 00000000..69ef1a48 --- /dev/null +++ b/tests/phpunit/tests/model/test-model-subscription.php @@ -0,0 +1,52 @@ +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 ); + } +}