public function testCompile_SuccessThenFailure()
{
$def = new DataSift_Definition($this->user, testdata('definition'));
$this->assertEquals($def->get(), testdata('definition'), 'Definition string not set correctly');
$response = array('response_code' => 200, 'data' => array('hash' => testdata('definition_hash'), 'created_at' => date('Y-m-d H:i:s', time()), 'dpu' => 10), 'rate_limit' => 200, 'rate_limit_remaining' => 150);
DataSift_MockApiClient::setResponse($response);
try {
$def->compile();
} catch (DataSift_Exception_CompileFailed $e) {
$this->fail('CompileFailed: ' . $e->getMessage() . ' (' . $e->getCode() . ')');
} catch (DataSift_Exception_InvalidData $e) {
$this->fail('InvalidData: ' . $e->getMessage() . ' (' . $e->getCode() . ')');
} catch (DataSift_Exception_APIError $e) {
$this->fail('APIError: ' . $e->getMessage() . ' (' . $e->getCode() . ')');
}
$this->assertEquals($response['data']['hash'], $def->getHash(), 'Hash is not correct');
// Now set the invalid definition in that same object
$def->set(testdata('invalid_definition'));
$this->assertEquals(testdata('invalid_definition'), $def->get(), 'Definition string not set correctly');
$response = array('response_code' => 400, 'data' => array('error' => 'The target interactin.content does not exist'), 'rate_limit' => 200, 'rate_limit_remaining' => 150);
DataSift_MockApiClient::setResponse($response);
try {
$def->compile();
$this->fail('CompileFailed exception expected, but not thrown');
} catch (DataSift_Exception_InvalidData $e) {
// Check the error message
$this->assertEquals($e->getMessage(), $response['data']['error']);
} catch (DataSift_Exception_APIError $e) {
$this->fail('APIError: ' . $e->getMessage() . ' (' . $e->getCode() . ')');
} catch (Exception $e) {
$this->fail('Unhandled exception: ' . $e->getMessage() . ' (' . $e->getCode() . ')');
}
}