public function testSendRequest() { $sr = new SendRequest('464bed09-9875-45b3-9111-44f633de9d30'); $sr->setChannel('Sysout'); $sr->setTemplate('Dummy'); $sr->setRecipient(new Address('*****@*****.**', 'weweave GbR')); $sr->setEcho(true); $sr->setKeyword('UnitTest'); $sr->setDetails('php unit test testSendSuccess()'); $sr->addModelParam('firstname', 'John'); $sr->addModelParam('code', '123456'); $attachment = new Attachment('test.txt'); $attachment->setPayloadFromFile('test.txt'); $attachment->setContentType('text/plain'); $sr->addAttachment($attachment); $o = $sr->getObject(); $this->assertNotEmpty($o); $this->assertEquals('464bed09-9875-45b3-9111-44f633de9d30', $o['token']); $this->assertEquals('Sysout', $o['channel']); $this->assertEquals('Dummy', $o['template']); $this->assertEquals(true, $o['echo']); $this->assertEquals('UnitTest', $o['keyword']); $this->assertEquals('php unit test testSendSuccess()', $o['details']); $this->assertNotEmpty($o['recipient']); $this->assertEquals('*****@*****.**', $o['recipient']['address']); $this->assertEquals('weweave GbR', $o['recipient']['name']); $this->assertEquals('firstname', $o['model'][0]['key']); $this->assertEquals('John', $o['model'][0]['value']); $this->assertEquals('code', $o['model'][1]['key']); $this->assertEquals('123456', $o['model'][1]['value']); $this->assertEquals('test.txt', $o['attachments'][0]['filename']); $this->assertEquals('text/plain', $o['attachments'][0]['contentType']); $this->assertEquals('VGhpcyBpcyBqdXN0IGEgdGVzdCBjYXNl', $o['attachments'][0]['payload']); }
/** * @param SendRequest $request * @return SendResponse * @throws Exception */ public function send(SendRequest $request) { $ch = curl_init($this->getUri() . 'rs/send'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request->getObject())); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $result = curl_exec($ch); if ($result === false) { throw new Exception('Could not connect to server'); } $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($status != 200) { throw new Exception('Invalid http code ' . $status); } $json = json_decode($result, true); return SendResponse::createFromObject($json); }