From 8a4e3cd4784890d12a850e796eaba2c4041014a8 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 20 Jun 2024 10:34:30 +0200 Subject: [PATCH] Service tested --- includes/ContentImport/Service.php | 71 +++++++++++++-------- tests/phpunit/ContentImport/TestService.php | 29 +++++++++ 2 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 tests/phpunit/ContentImport/TestService.php diff --git a/includes/ContentImport/Service.php b/includes/ContentImport/Service.php index cccd5a0f..4685571a 100644 --- a/includes/ContentImport/Service.php +++ b/includes/ContentImport/Service.php @@ -36,35 +36,56 @@ public function register() { * Differently from the `register` method this method will not check for options to hook. */ public function hook() { - add_action( 'load-post.php', function () { - return ContentImporter::instance()->handle_import(); - } ); - add_action( 'load-post.php', function () { - add_action( 'admin_notices', function () { - AdminNoticeLogger::instance()->show_last_log(); - } ); - } ); - add_action( 'load-post-new.php', function () { - return ContentImporter::instance()->handle_import(); - } ); - add_filter( 'wp_insert_post_empty_content', function ( $empty ) { - return ContentImporter::instance()->filter_empty( $empty ); - } ); - add_filter( 'wp_get_attachment_url', function ( $url, $post_id ) { - return AttachmentPathFinder::instance()->filter_attachment_url( $url, $post_id ); - }, 99, 2 ); - add_filter( 'wp_calculate_image_srcset', + add_action( + 'load-post.php', + function () { + return ContentImporter::instance()->handle_import(); + } + ); + add_action( + 'load-post.php', + function () { + add_action( + 'admin_notices', + function () { + AdminNoticeLogger::instance()->show_last_log(); + } + ); + } + ); + add_action( + 'load-post-new.php', + function () { + return ContentImporter::instance()->handle_import(); + } + ); + add_filter( + 'wp_insert_post_empty_content', + function ( $empty ) { + return ContentImporter::instance()->filter_empty( $empty ); + } + ); + add_filter( + 'wp_get_attachment_url', + function ( $url, $post_id ) { + return AttachmentPathFinder::instance()->filter_attachment_url( $url, $post_id ); + }, + 99, + 2 + ); + add_filter( + 'wp_calculate_image_srcset', function ( $sources, $sizeArray, $imageSrc, $imageMeta, $attachmentId ) { - return AttachmentPathFinder::instance()->filter_srcset( $sources, + return AttachmentPathFinder::instance()->filter_srcset( + $sources, $sizeArray, $imageSrc, $imageMeta, - $attachmentId ); + $attachmentId + ); }, 99, - 5 ); - - if ( is_admin() ) { - } + 5 + ); } -} \ No newline at end of file +} diff --git a/tests/phpunit/ContentImport/TestService.php b/tests/phpunit/ContentImport/TestService.php new file mode 100644 index 00000000..e67cfadc --- /dev/null +++ b/tests/phpunit/ContentImport/TestService.php @@ -0,0 +1,29 @@ +activate_content_import = false; + + Functions\expect( 'msls_options' )->once()->andReturn( $options ); + + $this->assertFalse( ( new Service() )->register() ); + } + + public function test_register_active_true(): void { + $options = \Mockery::mock( MslsOptions::class ); + $options->activate_content_import = true; + + Functions\expect( 'msls_options' )->once()->andReturn( $options ); + + $this->assertTrue( ( new Service() )->register() ); + } +}