Skip to content

Commit

Permalink
Importer base classes tested
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Nov 15, 2024
1 parent cf6864c commit 716c16f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
4 changes: 1 addition & 3 deletions includes/ContentImport/Importers/ImportersBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace lloc\MslsTests\ContentImport\Importers\PostFields;

use Brain\Monkey\Functions;
use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\ContentImport\Importers\PostFields\Duplicating;
use lloc\MslsTests\MslsUnitTestCase;

class TestDuplicating extends MslsUnitTestCase {

public function testImport(): void {
Functions\expect( 'wp_insert_post' )->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() );
}
}
28 changes: 28 additions & 0 deletions tests/phpunit/ContentImport/Importers/TestImportersBaseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace lloc\MslsTests\ContentImport\Importers;

use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\ContentImport\Importers\ImportersBaseFactory;
use lloc\MslsTests\MslsUnitTestCase;
use Mockery\Mock;

class TestImportersBaseFactory extends MslsUnitTestCase {

public function testMake(): void {
$coordinates = \Mockery::mock( ImportCoordinates::class );
$coordinates->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() );
}
}
19 changes: 19 additions & 0 deletions tests/phpunit/ContentImport/Importers/TestMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace lloc\MslsTests\ContentImport\Importers;

use lloc\Msls\ContentImport\ImportCoordinates;
use lloc\Msls\ContentImport\Importers\Map;
use lloc\MslsTests\MslsUnitTestCase;

class TestMap extends MslsUnitTestCase {

public function testMake(): void {
$coordinates = \Mockery::mock( ImportCoordinates::class );
$coordinates->shouldReceive( 'get_importer_for' )->andReturn( 'post-fields' );

$result = ( new Map() )->make( $coordinates );

$this->assertNotEmpty( $result );
}
}
13 changes: 13 additions & 0 deletions tests/phpunit/ContentImport/Importers/TestPostFieldsImporters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 716c16f

Please sign in to comment.