/** * Test stats API call * * @return void */ public function testStats() { $getCallCount = 0; $this->Http->response['status']['code'] = 200; // Test normal call $params['hash'] = 'R8hVH'; // cakephp.org $expectedParams = array('http://api.bit.ly/stats', array('hash' => 'R8hVH', 'format' => 'json', 'login' => 'testLogin', 'apiKey' => 'testApiKey', 'version' => '2.0.1')); ksort($expectedParams[1]); $responseData = array('clicks' => 6, 'hash' => 'R8hVH', 'referrers' => array('' => array('direct' => 4), 'twitter.com' => array('/' => 1, '/home' => 1))); $response = json_encode(array('errorCode' => 0, 'errorMessage' => '', 'results' => $responseData, 'statusCode' => 'OK')); $this->Http->expectAt($getCallCount, 'get', $expectedParams); $this->Http->setReturnValueAt($getCallCount++, 'get', $response); $result = $this->Model->stats(compact('params')); $expected = array('result' => $responseData, 'code' => 200); $this->assertEqual($result, $expected); // Test XML call $expectedParams[1]['format'] = 'xml'; $response = '<bitly> <errorCode>0</errorCode> <errorMessage></errorMessage> <results> <referrers> <nodeKeyVal> <direct>4</direct> <nodeKey><![CDATA[]]></nodeKey> </nodeKeyVal> <nodeKeyVal> <nodeKey><![CDATA[twitter.com]]></nodeKey> <nodeKeyVal> <nodeValue><![CDATA[1]]></nodeValue> <nodeKey><![CDATA[/]]></nodeKey> </nodeKeyVal> <nodeKeyVal> <nodeValue><![CDATA[1]]></nodeValue> <nodeKey><![CDATA[/home]]></nodeKey> </nodeKeyVal> </nodeKeyVal> </referrers> <hash>R8hVH</hash> <clicks>6</clicks> </results> <statusCode>OK</statusCode> </bitly>'; $this->Http->expectAt($getCallCount, 'get', $expectedParams); $this->Http->setReturnValueAt($getCallCount++, 'get', $response); $result = $this->Model->stats(array('format' => 'xml', 'params' => $params)); $expected = array('result' => $responseData, 'code' => 200); $this->assertEqual($result, $expected); if ($this->__useTestSocket) { $this->Http->expectCallCount('get', $getCallCount); } }