From 716c16fe1c43b4a98b14e40673ba21e1b0901901 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Fri, 15 Nov 2024 13:29:31 +0100 Subject: [PATCH] Importer base classes tested --- .../Importers/ImportersBaseFactory.php | 4 +- .../Importers/PostFields/TestDuplicating.php | 49 +++++++++++++++++++ .../Importers/TestImportersBaseFactory.php | 28 +++++++++++ .../ContentImport/Importers/TestMap.php | 19 +++++++ .../Importers/TestPostFieldsImporters.php | 13 +++++ 5 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php create mode 100644 tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php create mode 100644 tests/phpunit/ContentImport/Importers/TestMap.php diff --git a/includes/ContentImport/Importers/ImportersBaseFactory.php b/includes/ContentImport/Importers/ImportersBaseFactory.php index e76558c8..2d7e2ba0 100644 --- a/includes/ContentImport/Importers/ImportersBaseFactory.php +++ b/includes/ContentImport/Importers/ImportersBaseFactory.php @@ -56,9 +56,7 @@ public function make( ImportCoordinates $import_coordinates ) { $slug = $import_coordinates->get_importer_for( $type ) ?: $first; // if there is some incoherence return the null-doing base importer - $class = ! empty( $slug ) && isset( $map[ $slug ] ) - ? $map[ $slug ] - : BaseImporter::class; + $class = ! empty( $slug ) && isset( $map[ $slug ] ) ? $map[ $slug ] : BaseImporter::class; return new $class( $import_coordinates ); } diff --git a/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php b/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php new file mode 100644 index 00000000..056513a7 --- /dev/null +++ b/tests/phpunit/ContentImport/Importers/PostFields/TestDuplicating.php @@ -0,0 +1,49 @@ +once(); + + $post = \Mockery::mock( \WP_Post::class ); + $post->post_excerpt = 'excerpt'; + $post->post_title = 'title'; + $post->post_content = 'content'; + $post->post_content_filtered = 'content_filtered'; + + $coordinates = \Mockery::mock( ImportCoordinates::class ); + $coordinates->source_post = $post; + + $result = array( + 'post_type' => 'post', + 'post_content' => 'content', + 'post_content_filtered' => 'content_filtered', + 'post_title' => 'title', + 'post_excerpt' => 'excerpt', + ); + + $this->assertEquals( $result, ( new Duplicating( $coordinates ) )->import( array() ) ); + } + + public function testFilterFields(): void { + $coordinates = \Mockery::mock( ImportCoordinates::class ); + + $test = new Duplicating( $coordinates ); + + $result = array( + 'post_content', + 'post_content_filtered', + 'post_title', + 'post_excerpt', + ); + + $this->assertEquals( $result, $test->filter_fields() ); + } +} diff --git a/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php b/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php new file mode 100644 index 00000000..92fbab14 --- /dev/null +++ b/tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php @@ -0,0 +1,28 @@ +shouldReceive( 'get_importer_for' )->andReturn( 'post-fields' ); + + $test = \Mockery::mock( ImportersBaseFactory::class )->makePartial(); + + $this->expectException( \RuntimeException::class ); + + $test->make( $coordinates ); + } + + public function testDetails(): void { + $test = \Mockery::mock( ImportersBaseFactory::class )->makePartial(); + + $this->assertNotEmpty( $test->details() ); + } +} diff --git a/tests/phpunit/ContentImport/Importers/TestMap.php b/tests/phpunit/ContentImport/Importers/TestMap.php new file mode 100644 index 00000000..93db70e7 --- /dev/null +++ b/tests/phpunit/ContentImport/Importers/TestMap.php @@ -0,0 +1,19 @@ +shouldReceive( 'get_importer_for' )->andReturn( 'post-fields' ); + + $result = ( new Map() )->make( $coordinates ); + + $this->assertNotEmpty( $result ); + } +} diff --git a/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php b/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php index ec7c56a7..fa3dcb86 100644 --- a/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php +++ b/tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php @@ -2,11 +2,24 @@ namespace lloc\MslsTests\ContentImport\Importers; +use brain\Monkey\Filters; +use lloc\Msls\ContentImport\ImportCoordinates; +use lloc\Msls\ContentImport\Importers\PostFields\Duplicating; use lloc\Msls\ContentImport\Importers\PostFieldsImporters; use lloc\MslsTests\MslsUnitTestCase; class TestPostFieldsImporters extends MslsUnitTestCase { + public function testMake(): void { + $importer = \Mockery::mock( Duplicating::class ); + + $test = new PostFieldsImporters(); + + Filters\expectApplied( 'msls_content_import_post-fields_importer' )->once()->andReturn( $importer ); + + $this->assertEquals( $importer, $test->make( new ImportCoordinates() ) ); + } + public function testDetails(): void { $test = new PostFieldsImporters();