Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
#41 add integrations test for attachment fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
derpixler committed Jan 21, 2016
1 parent dc325c7 commit 850a5d9
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/Import/Service/WpPostImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public function import_post( Type\ImportPostInterface $import_post ) {
*/
private function meta_result( $meta_result, $attribute ){


if ( $meta_result !== TRUE ) {

$meta_result = new WP_Error( 'meta_update_failed', "Cant add or update postmeta." );
Expand Down Expand Up @@ -308,6 +309,7 @@ public function import_attachment( $attachment_id, Type\ImportPostInterface $imp
return;
}

file_put_contents( $upload['file'], $response['body'] );

// Generate the metadata for the attachment, and update the database record.
$attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $upload['file'] );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php # -*- coding: utf-8 -*-

namespace W2M\Test\Unit\Import\Service;

use Brain;
use W2M\Import\Service;
use W2M\Test\Helper;

class WpAttachmentImporterTest extends Helper\MonkeyTestCase {

private $fs_helper;

/**
* runs before each test
*/
public function setUp() {

if ( ! $this->fs_helper ) {
$this->fs_helper = new Helper\FileSystem;
}

parent::setUp();

}

/**
* @group import
*/
public function test_import_post() {

$id_mapper_mock = $this->mock_builder->data_multi_type_id_mapper();

$testee = new Service\WpPostImporter( $id_mapper_mock, $http_mock );

$post_mock = $this->getMockBuilder( 'W2M\Import\Type\ImportPostInterface' )
->getMock();

$postmeta_mock_single = $this->mock_builder->type_wp_import_meta();
$postmeta_mock_single->method( 'key' )->willReturn( 'mocky' );
$postmeta_mock_single->method( 'value' )->willReturn( 'mocky' );
$postmeta_mock_single->method( 'is_single' )->willReturn( TRUE );

$postmeta_mock_array = $this->mock_builder->type_wp_import_meta();
$postmeta_mock_array->method( 'key' )->willReturn( 'mocky' );
$postmeta_mock_array->method( 'value' )->willReturn( array( 'mocky', 'mreed' ) );
$postmeta_mock_array->method( 'is_single' )->willReturn( FALSE );

$term_mock = $this->mock_builder->type_wp_term_reference();
$term_mock->method( 'origin_id' )->willReturn( 113 );
$term_mock->method( 'taxonomy' )->willReturn( 'category' );

/**
* Now define the behaviour of the mock object. Each of the specified
* methods ( @see ImportPostInterface ) should return a proper value!
*/
$postdata = array(
'title' => 'Mocky test fight',
'origin_author_id' => 12,
'status' => 'draft',
'guid' => 'mocky',
'date' => ( new \DateTime( 'NOW' ) )->format( 'Y-m-d H:i:s' ),
'comment_status' => 'open',
'ping_status' => 'open',
'type' => 'attachment',
'excerpt' => 'Mocky the fighter',
'content' => 'Mock will go for a greate fight.',
'name' => 'mocky',
'origin_parent_post_id' => 42,
'menu_order' => 1,
'password' => 'mocky',
'origin_link' => 'http://wpml2mlp.test/mocky',
'terms' => array( $term_mock ),
'meta' => array( $postmeta_mock_single, $postmeta_mock_array ),
'origin_attachment_url' => 'http://inpsyde.com/wp-content/themes/i/assets/img/logo.png'
);

$new_parent_id = 15;
$new_author_id = 1;

$id_mapper_mock->expects( $this->atLeast( 2 ) )
->method( 'local_id' )
->withConsecutive(
array( 'post', $postdata[ 'origin_parent_post_id' ] ),
array( 'user', $postdata[ 'origin_author_id' ] )
)->will( $this->onConsecutiveCalls( $new_parent_id, $new_author_id ) );

foreach ( $postdata as $method => $return_value ) {

$post_mock->expects( $this->atLeast( 1 ) )
->method( $method )
->willReturn( $return_value );

}

$testee->import_post( $post_mock );

}

}

0 comments on commit 850a5d9

Please sign in to comment.