Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Mock-testing web services in Zend Framework
public function testGetTracks()
{
// Create mocks for the SoundCloud class for testing purposes
$webservice = $this->getMockBuilder('\Soundcloud\Services\Soundcloud')
->setConstructorArgs(array(
$this->config->soundcloud->clientId,
$this->config->soundcloud->clientSecret))
->setMethods(array('get', 'setAccessToken'))
->getMock();
$myInfo = file_get_contents(FIXTURE_PATH . '/soundcloud_my_info.txt');
$playlists = file_get_contents(FIXTURE_PATH . '/soundcloud_playlists.txt');
$tracks = file_get_contents(FIXTURE_PATH . '/soundcloud_tracks.txt');
$inputs = array('me', 'playlists', 'tracks');
$outputs = array($myInfo, $playlists, $tracks);
$expectation = $webservice->expects($this->exactly(3))->method('get');
$this->setMultipleMatching($expectation, $inputs, $outputs);
$this->soundcloud = new \Soundcloud\Client(
$webservice,
$this->config->soundcloud->clientId,
$this->config->soundcloud->clientSecret
);
$this->soundcloud->setAccessToken(
'<user>',
'<password>',
$this->config->soundcloud->tokenUrl
);
$myInfo = $this->soundcloud->getMyInfo();
$mySoundcloudId = $myInfo['id'];
$playlists = $this->soundcloud->getPlaylists($this->config->soundcloud->consumer_key);
$this->assertTrue(count($playlists) > 0, 'Found one or more SoundCloud playlists');
$tracks = $this->soundcloud->getAllTracks($mySoundcloudId);
$this->assertTrue(count($tracks) > 0, 'Found one or more tracks from SoundCloud');
}
public function setMultipleMatching($expectation, array $inputs, array $outputs)
{
$testCase = $this;
$callback = function() use ($inputs, $outputs, $testCase) {
$args = func_get_args();
$testCase->assertContains($args[0], $inputs);
$index = array_search($args[0], $inputs);
return $outputs[$index];
};
$expectation->will($this->returnCallback($callback));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.