diff --git a/includes/class-wp-notify-aggregate-factory.php b/includes/class-aggregate-factory.php similarity index 96% rename from includes/class-wp-notify-aggregate-factory.php rename to includes/class-aggregate-factory.php index 22343a5e..8a23d541 100644 --- a/includes/class-wp-notify-aggregate-factory.php +++ b/includes/class-aggregate-factory.php @@ -1,6 +1,8 @@ sender; @@ -122,7 +120,7 @@ public function get_sender() { /** * Gets the recipients for the notification. * - * @return WP_Notify_Recipient_Collection Notification recipients. + * @return Recipients\Collection Notification recipients. */ public function get_recipients() { return $this->recipients; @@ -131,7 +129,7 @@ public function get_recipients() { /** * Gets the message for the notification. * - * @return WP_Notify_Message Notification message. + * @return Messages\Message Notification message. */ public function get_message() { return $this->message; diff --git a/includes/class-wp-notify-factory.php b/includes/class-factory.php similarity index 77% rename from includes/class-wp-notify-factory.php rename to includes/class-factory.php index 53e540e3..8774731c 100644 --- a/includes/class-wp-notify-factory.php +++ b/includes/class-factory.php @@ -1,6 +1,12 @@ message_factory = $message_factory; $this->recipient_factory = $recipient_factory; @@ -41,14 +47,14 @@ public function __construct( * * @param $args * - * @return WP_Notify_Base_Notification + * @return Base_Notification */ public function create( $args ) { list( $message_args, $recipients_args, $sender_args ) = $this->validate( $args ); $sender = $this->sender_factory->create( $sender_args ); - $recipients = new WP_Notify_Recipient_Collection(); + $recipients = new Recipients\Collection(); $message = $this->message_factory->create( $message_args ); foreach ( $recipients_args as $type => $value ) { @@ -57,7 +63,7 @@ public function create( $args ) { ); } - return new WP_Notify_Base_Notification( $sender, $recipients, $message ); + return new Base_Notification( $sender, $recipients, $message ); } private function validate( $args ) { diff --git a/includes/demo.php b/includes/demo.php index 9de24668..d28bc33f 100644 --- a/includes/demo.php +++ b/includes/demo.php @@ -5,12 +5,17 @@ * Development demo files **/ +namespace WP\Notifications; + +use WP_Admin_Bar; +use WP_List_Table; + /** * Adds WP Notify icon after the user avatar in the top admin bar in the "secondary" position * * @param WP_Admin_Bar $wp_admin_bar Toolbar instance. */ -function wp_admin_bar_wp_notify_item( WP_Admin_Bar $wp_admin_bar ) { +function admin_bar_item( WP_Admin_Bar $wp_admin_bar ) { if ( ! is_admin() ) { return; } @@ -36,27 +41,27 @@ function wp_admin_bar_wp_notify_item( WP_Admin_Bar $wp_admin_bar ) { ); $wp_admin_bar->add_node( $args ); } -add_action( 'admin_bar_menu', 'wp_admin_bar_wp_notify_item', 1 ); +add_action( 'admin_bar_menu', '\WP\Notifications\admin_bar_item', 1 ); /** * Adds WP Notify area at the top of the dashboard */ -function wp_notify_admin_notice() { +function admin_notice() { echo '
'; } -add_action( 'admin_notices', 'wp_notify_admin_notice' ); +add_action( 'admin_notices', '\WP\Notifications\admin_notice' ); /** * Register and enqueue a wp notify scripts and stylesheet in WordPress admin. */ -function wp_notify_enqueue_admin_assets() { +function enqueue_admin_assets() { /* Load styles */ - wp_register_style( 'wp_notify_css', WP_NOTIFICATION_CENTER_PLUGIN_DIR_URL . '/build/wp-notify.css', array(), WP_NOTIFICATION_CENTER_PLUGIN_VERSION ); + wp_register_style( 'wp_notify_css', WP_FEATURE_NOTIFICATION_PLUGIN_DIR_URL . '/build/wp-notify.css', array(), WP_FEATURE_NOTIFICATION_PLUGIN_VERSION ); wp_enqueue_style( 'wp_notify_css' ); /* Load scripts */ - $asset = include WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/build/wp-notify.asset.php'; - wp_register_script( 'wp_notify_js', WP_NOTIFICATION_CENTER_PLUGIN_DIR_URL . '/build/wp-notify.js', $asset['dependencies'], WP_NOTIFICATION_CENTER_PLUGIN_VERSION, true ); + $asset = include WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/build/wp-notify.asset.php'; + wp_register_script( 'wp_notify_js', WP_FEATURE_NOTIFICATION_PLUGIN_DIR_URL . '/build/wp-notify.js', $asset['dependencies'], WP_FEATURE_NOTIFICATION_PLUGIN_VERSION, true ); wp_enqueue_script( 'wp_notify_js' ); wp_localize_script( 'wp_notify_js', @@ -67,7 +72,7 @@ function wp_notify_enqueue_admin_assets() { ); } -add_action( 'admin_enqueue_scripts', 'wp_notify_enqueue_admin_assets', 0 ); +add_action( 'admin_enqueue_scripts', '\WP\Notifications\enqueue_admin_assets', 0 ); /** @@ -75,17 +80,17 @@ function wp_notify_enqueue_admin_assets() { * * @return void */ -function wp_notify_add_admin_options_page() { - add_options_page( 'Notifications', 'Notifications', 'manage_options', 'wp-notify', 'wp_notify_render_admin_options_page' ); +function add_admin_options_page() { + add_options_page( 'Notifications', 'Notifications', 'manage_options', 'wp-notify', '\WP\Notifications\render_admin_options_page' ); } -add_action( 'admin_menu', 'wp_notify_add_admin_options_page' ); +add_action( 'admin_menu', '\WP\Notifications\add_admin_options_page' ); /** * Renders the options page. */ -function wp_notify_render_admin_options_page() { ?> +function render_admin_options_page() { ?>

@@ -227,16 +232,16 @@ function init_table() { /** * Registers our dashboard widget. */ -function wp_notify_dashboard_widget() { - add_meta_box( 'wp_notify', __( 'WP Notify' ), 'wp_notify_render_dashboard_widget', 'dashboard', 'side', 'high' ); +function dashboard_widget() { + add_meta_box( 'wp_notify', __( 'WP Notify' ), '\WP\Notifications\render_dashboard_widget', 'dashboard', 'side', 'high' ); } -add_action( 'wp_dashboard_setup', 'wp_notify_dashboard_widget' ); +add_action( 'wp_dashboard_setup', '\WP\Notifications\dashboard_widget' ); /** * Renders our dashboard widget. */ -function wp_notify_render_dashboard_widget() { +function render_dashboard_widget() { ?>
diff --git a/includes/exceptions/class-wp-notify-failed-to-add-recipient.php b/includes/exceptions/class-failed-to-add-recipient.php similarity index 70% rename from includes/exceptions/class-wp-notify-failed-to-add-recipient.php rename to includes/exceptions/class-failed-to-add-recipient.php index ca0836d7..11cc466b 100644 --- a/includes/exceptions/class-wp-notify-failed-to-add-recipient.php +++ b/includes/exceptions/class-failed-to-add-recipient.php @@ -1,6 +1,8 @@ accepts( $type ) ) { - throw WP_Notify_Invalid_Type::from_message_type( $type ); + throw Exceptions\Invalid_Type::from_message_type( $type ); } list( $type, $value ) = $this->validate( $type, $value ); @@ -25,7 +29,7 @@ public function create( $value, $type = 'standard' ) { switch ( $type ) { case self::TYPE_STANDARD: default: - return new WP_Notify_Base_Message( $value ); + return new Base_Message( $value ); } } diff --git a/includes/messages/class-wp-notify-base-message.php b/includes/messages/class-base-message.php similarity index 90% rename from includes/messages/class-wp-notify-base-message.php rename to includes/messages/class-base-message.php index f0a55678..2300b3e3 100644 --- a/includes/messages/class-wp-notify-base-message.php +++ b/includes/messages/class-base-message.php @@ -1,6 +1,8 @@ find_by_date_range( $start, $end, - WP_Notify_Order::DESCENDING, + Order::DESCENDING, $pagination, $offset ); diff --git a/includes/persistence/class-wp-notify-wpdb-notification-repository.php b/includes/persistence/class-wpdb-notification-repository.php similarity index 52% rename from includes/persistence/class-wp-notify-wpdb-notification-repository.php rename to includes/persistence/class-wpdb-notification-repository.php index 5fd834ef..67ac44c4 100644 --- a/includes/persistence/class-wp-notify-wpdb-notification-repository.php +++ b/includes/persistence/class-wpdb-notification-repository.php @@ -1,15 +1,22 @@ accepts( $type ) ) { - throw WP_Notify_Invalid_Type::from_recipient_type( $type ); + throw Exceptions\Invalid_Type::from_recipient_type( $type ); } list( $type, $value ) = $this->validate( $type, $value ); @@ -44,11 +47,11 @@ public function accepts( $type ) { * @param string $type Type to get the implementation class for. * * @return string Implementation class. - * @throws WP_Notify_Invalid_Type If the recipient type was not valid. + * @throws Exceptions\Invalid_Type If the recipient type was not valid. */ public function get_implementation_for_type( $type ) { if ( ! $this->accepts( $type ) ) { - throw WP_Notify_Invalid_Type::from_recipient_type( $type ); + throw Exceptions\Invalid_Type::from_recipient_type( $type ); } $mappings = $this->get_type_mappings(); @@ -77,10 +80,10 @@ private function validate( $type, $value ) { */ private function get_type_mappings() { return apply_filters( - 'wp_notify_recipient_type_mappings', + 'wp_feature_notifications_recipient_type_mappings', array( - self::TYPE_USER => 'WP_Notify_User_Recipient', - self::TYPE_ROLE => 'WP_Notify_Role_Recipient', + self::TYPE_USER => 'User_Recipient', + self::TYPE_ROLE => 'Role_Recipient', ) ); } diff --git a/includes/recipients/class-wp-notify-recipient-collection.php b/includes/recipients/class-collection.php similarity index 75% rename from includes/recipients/class-wp-notify-recipient-collection.php rename to includes/recipients/class-collection.php index 011d403f..d5ef6c4b 100644 --- a/includes/recipients/class-wp-notify-recipient-collection.php +++ b/includes/recipients/class-collection.php @@ -1,20 +1,29 @@ recipients[] = $recipient; } - public function count() { + public function count(): int { return count( $this->recipients ); } /** * Return the current recipient. * - * @return WP_Notify_Recipient Recipient + * @return Recipient Recipient */ public function current() { return current( $this->recipients ); diff --git a/includes/recipients/class-wp-notify-role-recipient.php b/includes/recipients/class-role.php similarity index 65% rename from includes/recipients/class-wp-notify-role-recipient.php rename to includes/recipients/class-role.php index bb6a639e..78273cd3 100644 --- a/includes/recipients/class-wp-notify-role-recipient.php +++ b/includes/recipients/class-role.php @@ -1,6 +1,8 @@ 0 ) ) { - throw WP_Notify_Invalid_Recipient::from_invalid_user_id( $user_id ); + throw Invalid_Recipient::from_invalid_user_id( $user_id ); } return $user_id; diff --git a/includes/recipients/interface-wp-notify-recipient-factory.php b/includes/recipients/interface-factory.php similarity index 85% rename from includes/recipients/interface-wp-notify-recipient-factory.php rename to includes/recipients/interface-factory.php index 348d58d5..378e1f8d 100644 --- a/includes/recipients/interface-wp-notify-recipient-factory.php +++ b/includes/recipients/interface-factory.php @@ -1,6 +1,8 @@ name = $name; @@ -59,7 +65,7 @@ public static function json_unserialize( $json ) { $image = null; - if ( ! empty( $image_data ) && is_subclass_of( $class_name, 'WP_Notify_Image' ) ) { + if ( ! empty( $image_data ) && is_subclass_of( $class_name, 'Image' ) ) { $image_reflection = new ReflectionClass( $class_name ); $image = $image_reflection->newInstanceArgs( array_values( $image_data ) ); } @@ -78,7 +84,7 @@ public function get_name() { } /** - * @return WP_Notify_Base_Image|null + * @return Base_Image|null */ public function get_image() { return $this->image; diff --git a/includes/senders/interface-wp-notify-sender-factory.php b/includes/senders/interface-factory.php similarity index 63% rename from includes/senders/interface-wp-notify-sender-factory.php rename to includes/senders/interface-factory.php index eeaa2a13..8391844b 100644 --- a/includes/senders/interface-wp-notify-sender-factory.php +++ b/includes/senders/interface-factory.php @@ -1,13 +1,15 @@ assertEquals( $expected_json, $encoded_image ); @@ -29,11 +33,11 @@ public function test_it_can_be_json_encoded( $image_data, $expected_json ) { */ public function test_it_can_be_instantiated_from_json( $image_data, $json ) { - $test_instance = WP_Notify_Base_Image::json_unserialize( $json ); + $test_instance = Image\Base_Image::json_unserialize( $json ); list( $source, $alt ) = array_values( $image_data ); - $instance = new WP_Notify_Base_Image( $source, $alt ); + $instance = new Image\Base_Image( $source, $alt ); $this->assertEquals( $instance->get_source(), $test_instance->get_source() ); $this->assertEquals( $instance->get_alt(), $test_instance->get_alt() ); diff --git a/tests/phpunit/tests/test-base-message.php b/tests/phpunit/tests/test-base-message.php new file mode 100644 index 00000000..6811b5fc --- /dev/null +++ b/tests/phpunit/tests/test-base-message.php @@ -0,0 +1,28 @@ +assertInstanceOf( '\WP\Notifications\Messages\Base_Message', $testee ); + } + + public function test_it_implements_the_interface() { + $testee = new Messages\Base_Message( 'Message' ); + $this->assertInstanceOf( '\WP\Notifications\Messages\Message', $testee ); + } + + public function test_it_can_return_its_content() { + $testee = new Messages\Base_Message( 'Message' ); + $this->assertEquals( 'Message', $testee->get_content() ); + } + + public function test_it_can_be_cast_to_string() { + $testee = new Messages\Base_Message( 'Message' ); + $this->assertEquals( 'Message', (string) $testee ); + } +} diff --git a/tests/phpunit/tests/test-base-notification.php b/tests/phpunit/tests/test-base-notification.php new file mode 100644 index 00000000..cb509f1c --- /dev/null +++ b/tests/phpunit/tests/test-base-notification.php @@ -0,0 +1,43 @@ +createMock( '\WP\Notifications\Senders\Base_Sender' ); + $recipients_mock = $this->createMock( '\WP\Notifications\Recipients\Collection' ); + $testee = new Notifications\Base_Notification( + $sender_mock, + $recipients_mock, + new Dummy_Message() + ); + $this->assertInstanceOf( '\WP\Notifications\Base_Notification', $testee ); + } + + public function test_it_implements_the_interface() { + $sender_mock = $this->createMock( '\WP\Notifications\Senders\Base_Sender' ); + $recipients_mock = $this->createMock( '\WP\Notifications\Recipients\Collection' ); + $testee = new Notifications\Base_Notification( + $sender_mock, + $recipients_mock, + new Dummy_Message() + ); + $this->assertInstanceOf( '\WP\Notifications\Notification', $testee ); + } + + public function test_it_can_return_its_content() { + $sender_mock = $this->createMock( '\WP\Notifications\Senders\Base_Sender' ); + $recipients_mock = $this->createMock( '\WP\Notifications\Recipients\Collection' ); + $dummy_message = new Dummy_Message(); + $testee = new Notifications\Base_Notification( + $sender_mock, + $recipients_mock, + $dummy_message + ); + $this->assertEquals( $recipients_mock, $testee->get_recipients() ); + $this->assertEquals( $dummy_message, $testee->get_message() ); + } +} diff --git a/tests/phpunit/tests/test-wp-notify-factory.php b/tests/phpunit/tests/test-factory.php similarity index 53% rename from tests/phpunit/tests/test-wp-notify-factory.php rename to tests/phpunit/tests/test-factory.php index 763c734b..61caaf75 100644 --- a/tests/phpunit/tests/test-wp-notify-factory.php +++ b/tests/phpunit/tests/test-factory.php @@ -1,28 +1,31 @@ createMock( 'WP_Notify_Sender' ); + $vendor_sender = $this->createMock( '\WP\Notifications\Senders\Sender' ); - $message_factory = $this->getMockBuilder( 'WP_Notify_Message_Factory' ) + $message_factory = $this->getMockBuilder( '\WP\Notifications\Messages\Factory' ) ->setMethods( array( 'create', 'accepts' ) ) ->getMock(); - $message_factory->method( 'create' )->willReturn( $this->createMock( 'WP_Notify_Message' ) ); + $message_factory->method( 'create' )->willReturn( $this->createMock( '\WP\Notifications\Messages\Message' ) ); $message_factory->method( 'accepts' )->willReturn( true ); - $sender_factory = $this->getMockBuilder( 'WP_Notify_Sender_Factory' ) + $sender_factory = $this->getMockBuilder( '\WP\Notifications\Senders\Factory' ) ->setMethods( array( 'create' ) ) ->getMock(); - $sender_factory->method( 'create' )->willReturn( $this->createMock( 'WP_Notify_Sender' ) ); + $sender_factory->method( 'create' )->willReturn( $this->createMock( '\WP\Notifications\Senders\Sender' ) ); - $factory = new WP_Notify_Factory( + $factory = new Notifications\Factory( $message_factory, - $this->createMock( 'WP_Notify_Recipient_Factory' ), + $this->createMock( '\WP\Notifications\Recipients\Factory' ), $sender_factory ); diff --git a/tests/phpunit/tests/test-wp-notify-notification-repository.php b/tests/phpunit/tests/test-notification-repository.php similarity index 66% rename from tests/phpunit/tests/test-wp-notify-notification-repository.php rename to tests/phpunit/tests/test-notification-repository.php index d112b755..19dc9e89 100644 --- a/tests/phpunit/tests/test-wp-notify-notification-repository.php +++ b/tests/phpunit/tests/test-notification-repository.php @@ -1,10 +1,16 @@ find_by_id( $id ); $this->assertFalse( $result ); } diff --git a/tests/phpunit/tests/test-recipient-collection.php b/tests/phpunit/tests/test-recipient-collection.php new file mode 100644 index 00000000..a28423b4 --- /dev/null +++ b/tests/phpunit/tests/test-recipient-collection.php @@ -0,0 +1,97 @@ +assertInstanceOf( '\WP\Notifications\Recipients\Collection', $testee ); + } + + public function test_it_is_countable() { + $testee = new Recipients\Collection(); + $this->assertInstanceOf( 'Countable', $testee ); + } + + public function test_it_is_traversable() { + $testee = new Recipients\Collection(); + $this->assertInstanceOf( 'Traversable', $testee ); + } + + public function test_it_is_empty_when_instantiated_without_arguments() { + $testee = new Recipients\Collection(); + $this->assertCount( 0, $testee ); + } + + public function test_it_can_accept_a_singular_recipient() { + $mock_recipient = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $testee = new Recipients\Collection( $mock_recipient ); + $this->assertCount( 1, $testee ); + } + + public function test_it_can_accept_an_array_of_recipients() { + $mock_recipient_1 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $mock_recipient_2 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $mock_recipient_3 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $testee = new Recipients\Collection( + array( + $mock_recipient_1, + $mock_recipient_2, + $mock_recipient_3, + ) + ); + $this->assertCount( 3, $testee ); + } + + public function test_recipients_can_be_added() { + $mock_recipient_1 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $mock_recipient_2 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $mock_recipient_3 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $testee = new Recipients\Collection( $mock_recipient_1 ); + $this->assertCount( 1, $testee ); + $testee->add( $mock_recipient_2 ); + $this->assertCount( 2, $testee ); + $testee->add( $mock_recipient_3 ); + $this->assertCount( 3, $testee ); + } + + /** @dataProvider data_provider_it_throws_on_invalid_type */ + public function test_it_throws_on_invalid_type( $invalid_recipient ) { + $this->expectException( '\WP\Notifications\Exceptions\Runtime_Exception' ); + new Recipients\Collection( $invalid_recipient ); + } + + public function data_provider_it_throws_on_invalid_type() { + $mock_recipient_1 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + $mock_recipient_2 = $this->createMock( '\WP\Notifications\Recipients\Recipient' ); + + return array( + array( null ), + array( true ), + array( new stdClass ), + array( 'invalid' ), + array( array( 1, 2, 3 ) ), + array( array( $mock_recipient_1, 'invalid', $mock_recipient_2 ) ), + ); + } + + public function test_it_can_be_json_encoded() { + $testee = new Recipients\Collection( array() ); + $this->assertEquals( + '[]', + json_encode( $testee ) + ); + } + + public function test_it_can_be_instantiated_from_json() { + $json = '[]'; + $testee = Recipients\Collection::json_unserialize( $json ); + $this->assertInstanceOf( '\WP\Notifications\Recipients\Collection', $testee ); + $this->assertEquals( 0, $testee->count() ); + } +} diff --git a/tests/phpunit/tests/test-role-recipient.php b/tests/phpunit/tests/test-role-recipient.php new file mode 100644 index 00000000..d92a6092 --- /dev/null +++ b/tests/phpunit/tests/test-role-recipient.php @@ -0,0 +1,18 @@ +assertInstanceOf( '\WP\Notifications\Recipients\Role', $testee ); + } + + public function test_it_implements_the_interface() { + $testee = new Recipients\Role( 1 ); + $this->assertInstanceOf( '\WP\Notifications\Recipients\Recipient', $testee ); + } +} diff --git a/tests/phpunit/tests/test-user-recipient.php b/tests/phpunit/tests/test-user-recipient.php new file mode 100644 index 00000000..60ed2d72 --- /dev/null +++ b/tests/phpunit/tests/test-user-recipient.php @@ -0,0 +1,42 @@ +assertInstanceOf( '\WP\Notifications\Recipients\User', $testee ); + } + + public function test_it_implements_the_recipient_interface() { + $testee = new Recipients\User( 1 ); + $this->assertInstanceOf( '\WP\Notifications\Recipients\Recipient', $testee ); + } + + public function test_it_accepts_a_user_id_as_a_string() { + $testee = new Recipients\User( '1' ); + $this->assertInstanceOf( '\WP\Notifications\Recipients\Recipient', $testee ); + } + + /** @dataProvider data_provider_it_throws_on_invalid_type */ + public function test_it_throws_on_invalid_type( $invalid_user_id ) { + $this->expectException( '\WP\Notifications\Exceptions\Invalid_Recipient' ); + new Recipients\User( $invalid_user_id ); + } + + public function data_provider_it_throws_on_invalid_type() { + return array( + array( null ), + array( - 1 ), + array( 0 ), + array( 'invalid' ), + array( new stdClass ), + array( array( 1, 2 ) ), + ); + } +} diff --git a/tests/phpunit/tests/test-wp-notify-base-message.php b/tests/phpunit/tests/test-wp-notify-base-message.php deleted file mode 100644 index 025f759a..00000000 --- a/tests/phpunit/tests/test-wp-notify-base-message.php +++ /dev/null @@ -1,41 +0,0 @@ -assertInstanceOf( 'WP_Notify_Base_Message', $testee ); - } - - public function test_it_implements_the_interface() { - $testee = new WP_Notify_Base_Message( 'Message' ); - $this->assertInstanceOf( 'WP_Notify_Message', $testee ); - } - - public function test_it_can_return_its_content() { - $testee = new WP_Notify_Base_Message( 'Message' ); - $this->assertEquals( 'Message', $testee->get_content() ); - } - - public function test_it_can_be_cast_to_string() { - $testee = new WP_Notify_Base_Message( 'Message' ); - $this->assertEquals( 'Message', (string) $testee ); - } - - public function test_it_can_be_json_encoded() { - $testee = new WP_Notify_Base_Message( 'Message' ); - $this->assertEquals( - '"Message"', - json_encode( $testee ) - ); - } - - public function test_it_can_be_instantiated_from_json() { - $json = '"Message"'; - $testee = WP_Notify_Base_Message::json_unserialize( $json ); - $this->assertInstanceOf( 'WP_Notify_Base_Message', $testee ); - $this->assertEquals( 'Message', $testee->get_content() ); - } -} diff --git a/tests/phpunit/tests/test-wp-notify-base-notification.php b/tests/phpunit/tests/test-wp-notify-base-notification.php deleted file mode 100644 index f442622a..00000000 --- a/tests/phpunit/tests/test-wp-notify-base-notification.php +++ /dev/null @@ -1,73 +0,0 @@ -createMock( 'WP_Notify_Base_Sender' ); - $recipients_mock = $this->createMock( 'WP_Notify_Recipient_Collection' ); - $testee = new WP_Notify_Base_Notification( - $sender_mock, - $recipients_mock, - new Dummy_Message() - ); - $this->assertInstanceOf( 'WP_Notify_Base_Notification', $testee ); - } - - public function test_it_implements_the_interface() { - $sender_mock = $this->createMock( 'WP_Notify_Base_Sender' ); - $recipients_mock = $this->createMock( 'WP_Notify_Recipient_Collection' ); - $testee = new WP_Notify_Base_Notification( - $sender_mock, - $recipients_mock, - new Dummy_Message() - ); - $this->assertInstanceOf( 'WP_Notify_Notification', $testee ); - } - - public function test_it_can_return_its_content() { - $sender_mock = $this->createMock( 'WP_Notify_Base_Sender' ); - $recipients_mock = $this->createMock( 'WP_Notify_Recipient_Collection' ); - $dummy_message = new Dummy_Message(); - $testee = new WP_Notify_Base_Notification( - $sender_mock, - $recipients_mock, - $dummy_message - ); - $this->assertEquals( $recipients_mock, $testee->get_recipients() ); - $this->assertEquals( $dummy_message, $testee->get_message() ); - } - - public function test_it_can_be_json_encoded() { - $sender_mock = new WP_Notify_Base_Sender( 'Name 1' ); - $empty_recipients = new WP_Notify_Recipient_Collection(); - $message = new WP_Notify_Base_Message( 'Message' ); - $testee = new WP_Notify_Base_Notification( - $sender_mock, - $empty_recipients, - $message - ); - $this->assertEquals( - self::JSON_SERIALIZED, - json_encode( $testee ) - ); - } - - public function test_it_can_be_instantiated_from_json() { - $testee = WP_Notify_Base_Notification::json_unserialize( self::JSON_SERIALIZED ); - $this->assertInstanceOf( 'WP_Notify_Base_Notification', $testee ); - $this->assertInstanceOf( - 'WP_Notify_Recipient_Collection', - $testee->get_recipients() - ); - $this->assertInstanceOf( - 'WP_Notify_Base_Message', - $testee->get_message() - ); - $this->assertInstanceOf( - 'WP_Notify_Base_Sender', - $testee->get_sender() - ); - } -} diff --git a/tests/phpunit/tests/test-wp-notify-base-sender.php b/tests/phpunit/tests/test-wp-notify-base-sender.php deleted file mode 100644 index 0581fb50..00000000 --- a/tests/phpunit/tests/test-wp-notify-base-sender.php +++ /dev/null @@ -1,70 +0,0 @@ -newInstanceArgs( array_values( $sender_params ) ); - - $sender_encoded = json_encode( $sender ); - - $this->assertEquals( $expected_json, $sender_encoded ); - } - - /** - * @param array $sender_params - * @param string $json - * - * @dataProvider data_provider_senders - */ - public function test_it_can_be_instantiated_from_json( $sender_params, $json ) { - - $testee = WP_Notify_Base_Sender::json_unserialize( $json ); - - $sender_reflection = new ReflectionClass( 'WP_Notify_Base_Sender' ); - /** @var WP_Notify_Base_Sender $sender */ - $sender = $sender_reflection->newInstanceArgs( array_values( (array) $sender_params ) ); - - $this->assertInstanceOf( 'WP_Notify_Base_Sender', $testee ); - $this->assertEquals( $sender->get_name(), $testee->get_name() ); - - $this->assertEquals( $sender->get_image(), $testee->get_image() ); - - /** - * If an Image has been sent - */ - if ( $testee->get_image() ) { - $this->assertInstanceOf( get_class( $sender->get_image() ), $testee->get_image() ); - $this->assertEquals( $sender->get_image()->get_source(), $testee->get_image()->get_source() ); - $this->assertEquals( $sender->get_image()->get_alt(), $testee->get_image()->get_alt() ); - } - } - - public function data_provider_senders() { - - return array( - - 'sender without image' => array( - array( - 'Name 1', - ), - '{"name":"Name 1"}', - ), - - 'sender with image' => array( - array( - 'name' => 'Name 2', - 'WP_Notify_Base_Image' => new WP_Notify_Base_Image( 'img-source', 'img-alt' ), - ), - '{"name":"Name 2","WP_Notify_Base_Image":{"source":"img-source","alt":"img-alt"}}', - ), - ); - } -} diff --git a/tests/phpunit/tests/test-wp-notify-recipient-collection.php b/tests/phpunit/tests/test-wp-notify-recipient-collection.php deleted file mode 100644 index c9a93c93..00000000 --- a/tests/phpunit/tests/test-wp-notify-recipient-collection.php +++ /dev/null @@ -1,93 +0,0 @@ -assertInstanceOf( 'WP_Notify_Recipient_Collection', $testee ); - } - - public function test_it_is_countable() { - $testee = new WP_Notify_Recipient_Collection(); - $this->assertInstanceOf( 'Countable', $testee ); - } - - public function test_it_is_traversable() { - $testee = new WP_Notify_Recipient_Collection(); - $this->assertInstanceOf( 'Traversable', $testee ); - } - - public function test_it_is_empty_when_instantiated_without_arguments() { - $testee = new WP_Notify_Recipient_Collection(); - $this->assertCount( 0, $testee ); - } - - public function test_it_can_accept_a_singular_recipient() { - $mock_recipient = $this->createMock( 'WP_Notify_Recipient' ); - $testee = new WP_Notify_Recipient_Collection( $mock_recipient ); - $this->assertCount( 1, $testee ); - } - - public function test_it_can_accept_an_array_of_recipients() { - $mock_recipient_1 = $this->createMock( 'WP_Notify_Recipient' ); - $mock_recipient_2 = $this->createMock( 'WP_Notify_Recipient' ); - $mock_recipient_3 = $this->createMock( 'WP_Notify_Recipient' ); - $testee = new WP_Notify_Recipient_Collection( - array( - $mock_recipient_1, - $mock_recipient_2, - $mock_recipient_3, - ) - ); - $this->assertCount( 3, $testee ); - } - - public function test_recipients_can_be_added() { - $mock_recipient_1 = $this->createMock( 'WP_Notify_Recipient' ); - $mock_recipient_2 = $this->createMock( 'WP_Notify_Recipient' ); - $mock_recipient_3 = $this->createMock( 'WP_Notify_Recipient' ); - $testee = new WP_Notify_Recipient_Collection( $mock_recipient_1 ); - $this->assertCount( 1, $testee ); - $testee->add( $mock_recipient_2 ); - $this->assertCount( 2, $testee ); - $testee->add( $mock_recipient_3 ); - $this->assertCount( 3, $testee ); - } - - /** @dataProvider data_provider_it_throws_on_invalid_type */ - public function test_it_throws_on_invalid_type( $invalid_recipient ) { - $this->expectException( 'WP_Notify_Runtime_Exception' ); - new WP_Notify_Recipient_Collection( $invalid_recipient ); - } - - public function data_provider_it_throws_on_invalid_type() { - $mock_recipient_1 = $this->createMock( 'WP_Notify_Recipient' ); - $mock_recipient_2 = $this->createMock( 'WP_Notify_Recipient' ); - - return array( - array( null ), - array( true ), - array( new stdClass ), - array( 'invalid' ), - array( array( 1, 2, 3 ) ), - array( array( $mock_recipient_1, 'invalid', $mock_recipient_2 ) ), - ); - } - - public function test_it_can_be_json_encoded() { - $testee = new WP_Notify_Recipient_Collection( array() ); - $this->assertEquals( - '[]', - json_encode( $testee ) - ); - } - - public function test_it_can_be_instantiated_from_json() { - $json = '[]'; - $testee = WP_Notify_Recipient_Collection::json_unserialize( $json ); - $this->assertInstanceOf( 'WP_Notify_Recipient_Collection', $testee ); - $this->assertEquals( 0, $testee->count() ); - } -} diff --git a/tests/phpunit/tests/test-wp-notify-role-recipient.php b/tests/phpunit/tests/test-wp-notify-role-recipient.php deleted file mode 100644 index 194e3d28..00000000 --- a/tests/phpunit/tests/test-wp-notify-role-recipient.php +++ /dev/null @@ -1,14 +0,0 @@ -assertInstanceOf( 'WP_Notify_Role_Recipient', $testee ); - } - - public function test_it_implements_the_interface() { - $testee = new WP_Notify_Role_Recipient( 1 ); - $this->assertInstanceOf( 'WP_Notify_Recipient', $testee ); - } -} diff --git a/tests/phpunit/tests/test-wp-notify-user-recipient.php b/tests/phpunit/tests/test-wp-notify-user-recipient.php deleted file mode 100644 index 5a6497f5..00000000 --- a/tests/phpunit/tests/test-wp-notify-user-recipient.php +++ /dev/null @@ -1,36 +0,0 @@ -assertInstanceOf( 'WP_Notify_User_Recipient', $testee ); - } - - public function test_it_implements_the_recipient_interface() { - $testee = new WP_Notify_User_Recipient( 1 ); - $this->assertInstanceOf( 'WP_Notify_Recipient', $testee ); - } - - public function test_it_accepts_a_user_id_as_a_string() { - $testee = new WP_Notify_User_Recipient( '1' ); - $this->assertInstanceOf( 'WP_Notify_Recipient', $testee ); - } - - /** @dataProvider data_provider_it_throws_on_invalid_type */ - public function test_it_throws_on_invalid_type( $invalid_user_id ) { - $this->expectException( 'WP_Notify_Invalid_Recipient' ); - new WP_Notify_User_Recipient( $invalid_user_id ); - } - - public function data_provider_it_throws_on_invalid_type() { - return array( - array( null ), - array( - 1 ), - array( 0 ), - array( 'invalid' ), - array( new stdClass ), - array( array( 1, 2 ) ), - ); - } -} diff --git a/wp-feature-notifications.php b/wp-feature-notifications.php index 94764b3e..0132681f 100644 --- a/wp-feature-notifications.php +++ b/wp-feature-notifications.php @@ -15,52 +15,53 @@ * @package wp-feature-notifications */ +namespace WP\Notifications; + use WP\Notifications\REST; -if ( ! defined( 'WP_NOTIFICATION_CENTER_PLUGIN_VERSION' ) ) { - define( 'WP_NOTIFICATION_CENTER_PLUGIN_VERSION', '0.0.1' ); +if ( ! defined( 'WP_FEATURE_NOTIFICATION_PLUGIN_VERSION' ) ) { + define( 'WP_FEATURE_NOTIFICATION_PLUGIN_VERSION', '0.0.1' ); } -if ( ! defined( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR' ) ) { - define( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR', dirname( __FILE__ ) ); +if ( ! defined( 'WP_FEATURE_NOTIFICATION_PLUGIN_DIR' ) ) { + define( 'WP_FEATURE_NOTIFICATION_PLUGIN_DIR', dirname( __FILE__ ) ); } -if ( ! defined( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR_URL' ) ) { - define( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) ); +if ( ! defined( 'WP_FEATURE_NOTIFICATION_PLUGIN_DIR_URL' ) ) { + define( 'WP_FEATURE_NOTIFICATION_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) ); } // Require interface/class declarations.. -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/exceptions/interface-wp-notify-exception.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/exceptions/class-wp-notify-runtime-exception.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/exceptions/class-wp-notify-invalid-recipient.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/exceptions/class-wp-notify-failed-to-add-recipient.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/interface-wp-notify-json-unserializable.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/interface-wp-notify-status.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/interface-wp-notify-notification.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-wp-notify-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-wp-notify-aggregate-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-wp-notify-base-notification.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/image/interface-wp-notify-image.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/image/class-wp-notify-base-image.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/senders/interface-wp-notify-sender.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/senders/class-wp-notify-base-sender.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/interface-wp-notify-recipient.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/class-wp-notify-recipient-collection.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/class-wp-notify-user-recipient.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/class-wp-notify-role-recipient.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/interface-wp-notify-recipient-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/class-wp-notify-base-recipient-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/recipients/class-wp-notify-aggregate-recipient-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/messages/interface-wp-notify-message.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/messages/class-wp-notify-base-message.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/messages/interface-wp-notify-message-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/messages/class-wp-notify-base-message-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/messages/class-wp-notify-aggregate-message-factory.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/persistence/interface-wp-notify-notification-repository.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/persistence/class-wp-notify-abstract-notification-repository.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/persistence/class-wp-notify-wpdb-notification-repository.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/demo.php'; -require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/restapi/class-notification-controller.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/exceptions/interface-exception.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/exceptions/class-runtime-exception.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/exceptions/class-invalid-recipient.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/exceptions/class-failed-to-add-recipient.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/interface-json-unserializable.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/interface-status.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/interface-notification.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/class-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/class-aggregate-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/class-base-notification.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/image/interface-image.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/image/class-base-image.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/senders/interface-sender.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/senders/class-base-sender.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/interface-recipient.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/class-collection.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/class-user.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/class-role.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/interface-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/class-base-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/recipients/class-aggregate-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/messages/interface-message.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/messages/class-base-message.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/messages/interface-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/messages/class-base-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/messages/class-aggregate-factory.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/persistence/interface-notification-repository.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/persistence/class-abstract-notification-repository.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/persistence/class-wpdb-notification-repository.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/demo.php'; +require_once WP_FEATURE_NOTIFICATION_PLUGIN_DIR . '/includes/restapi/class-notification-controller.php'; -// TODO: Standardise structure and/or autoloading. new REST\Notification_Controller();