Skip to content

Instantly share code, notes, and snippets.

@eddiezane
Last active August 29, 2015 14:09
Show Gist options
  • Save eddiezane/69f8ee3ed9da20c4e3a4 to your computer and use it in GitHub Desktop.
Save eddiezane/69f8ee3ed9da20c4e3a4 to your computer and use it in GitHub Desktop.
<?php
public function send(SendGrid\Email $email) {
$form = $email->toWebFormat();
$form['api_user'] = $this->api_user;
$form['api_key'] = $this->api_key;
$response = $this->makeRequest($form);
if ($response->code != 200) {
throw new SendGrid\Exception($response->raw_body);
}
return $response->body;
}
public function makeRequest($form) {
return \Unirest::post($this->url, array('User-Agent' => 'sendgrid/' . $this->version . ';php'), $form);
}
<?php
/**
* @expectedException SendGrid\Exception
*/
public function testSendGridExceptionThrownWhenNot200() {
$mockResponse = (object)array("code" => 400, "raw_body" => '{"message": "error", "errors": ["Bad username / password"]}');
$sendgrid = m::mock("SendGrid[makeRequest]", array("foo", "bar"));
$sendgrid->shouldReceive('makeRequest')->once()->andReturn($mockResponse);
$email = new SendGrid\Email();
$email->setFrom("bar@foo.com")->
setSubject("foobar subject")->
setText("foobar text")->
addTo("foo@bar.com");
$response = $sendgrid->send($email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment