Created
May 2, 2011 20:24
-
-
Save chartjes/952286 to your computer and use it in GitHub Desktop.
Mock-testing web services in Zend Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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