Skip to content

Instantly share code, notes, and snippets.

@polevaultweb
Last active October 28, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polevaultweb/b82a609a882e18c58562 to your computer and use it in GitHub Desktop.
Save polevaultweb/b82a609a882e18c58562 to your computer and use it in GitHub Desktop.
<?php
/**
* Class API
*/
class API extends \WP_UnitTestCase {
/**
* @var \DeliciousBrains\Offload\AWS\API
*/
private $api;
/**
* @var \WP_Error
*/
private $wp_error_response;
/**
* Before each test
*/
public function setUp() {
/*
* Mock the API class so we don't make remote calls
* This is a partial mock, in that we are only mocking certain methods
*/
$this->api = $this->getMockBuilder( '\DeliciousBrains\Offload\AWS\API' )
->setConstructorArgs( array( API_KEY, API_SECRET ) )
->setMethods( array( 'request' ) )
->getMock();
$this->wp_error_response = new \WP_Error( 100, 'Issue with AWS' );
}
function test_get_files() {
$successful_response = new \stdClass;
$successful_response->data = array( array( 'id' => 1, 'name' => 'image.jpg' ) );
$this->api->expects( $this->any() )
->method( 'request' )
->will( $this->onConsecutiveCalls( $successful_response, $this->wp_error_response, array() ) );
// Test for successful response
$this->assertEquals( $successful_response, $this->api->get_files() );
// Test for WP_Error (Error) response
$this->assertTrue( is_wp_error( $this->api->get_files() ) );
// Test for empty array response
$array_response = $this->api->get_files();
$this->assertTrue( is_array( $array_response ) );
$this->assertCount( 0, $array_response );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment