最合适的回答,由SO网友:J.D. 整理而成
我想看看WordPress是如何构建附件测试的。你会发现tests/phpunit/tests/post/attachments.php
他们使用this method:
function _make_attachment( $upload, $parent_post_id = 0 ) {
$type = \'\';
if ( !empty($upload[\'type\']) ) {
$type = $upload[\'type\'];
} else {
$mime = wp_check_filetype( $upload[\'file\'] );
if ($mime)
$type = $mime[\'type\'];
}
$attachment = array(
\'post_title\' => basename( $upload[\'file\'] ),
\'post_content\' => \'\',
\'post_type\' => \'attachment\',
\'post_parent\' => $parent_post_id,
\'post_mime_type\' => $type,
\'guid\' => $upload[ \'url\' ],
);
// Save the data
$id = wp_insert_attachment( $attachment, $upload[ \'file\' ], $parent_post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload[\'file\'] ) );
return $this->ids[] = $id;
}
他们还
included sample images 通过他们的测试。这里是
an example of how they call the _make_attachment()
method 在其中一项测试中:
function test_insert_image_no_thumb() {
// this image is smaller than the thumbnail size so it won\'t have one
$filename = ( DIR_TESTDATA.\'/images/test-image.jpg\' );
$contents = file_get_contents($filename);
$upload = wp_upload_bits(basename($filename), null, $contents);
$this->assertTrue( empty($upload[\'error\']) );
$id = $this->_make_attachment($upload);
// ...
因此,您应该能够使用类似的方法为测试上传图像。您可以提供图像供自己使用,也可以只使用其中一个。