/** * Sends a message to a flow * * @param BaseMessageInterface $message A message instance to send * @param string $baseUrl A base URL * @param array $options An array of options used by request * * @return boolean */ protected function sendMessage(BaseMessageInterface $message, $baseUrl, array $options = array()) { $client = $this->createClient(sprintf('%s/%s', $baseUrl, $this->flowApiToken)); $request = $client->post(null, array('Content-Type' => 'application/json'), json_encode($message->getData()), $options); $message->setResponse($request->send()); return !$message->hasResponseErrors(); }
/** * Tests an invalid response */ public function testInvalidResponse() { $this->message->setResponse(new Response(400, null, '{"message": "Validation error", "errors": {"content": ["can\'t be blank"]}}')); $this->assertEquals(array('message' => 'Validation error', 'errors' => array('content' => array('can\'t be blank'))), $this->message->getResponseBody()); $this->assertEquals('Validation error', $this->message->getResponseMessage()); $this->assertTrue(is_array($this->message->getResponseErrors())); $this->assertEquals(array('content' => array('can\'t be blank')), $this->message->getResponseErrors()); $this->assertTrue($this->message->hasResponseErrors()); }
/** * Tests the sendMessage method succeed and failed * * @param BaseMessageInterface $message A message instance * @param string $baseUrl A base URL * @param array $options An array of options used by request * * @dataProvider getSendMessageArgs */ public function testSendMessage(BaseMessageInterface $message, $baseUrl, array $options) { $responseOk = new Response(200, array('Content-Type' => 'application/json; charset=utf-8'), '{}'); $responseKo = new Response(400, array('Content-Type' => 'application/json; charset=utf-8'), '{"message": "Validation error", "errors": {"content": ["can\'t be blank"]}}'); $request = $this->getMock('Guzzle\\Http\\Message\\RequestInterface'); $request->expects($this->exactly(2))->method('send')->will($this->onConsecutiveCalls($responseOk, $responseKo)); $client = $this->getMock('Guzzle\\Http\\ClientInterface'); $client->expects($this->exactly(2))->method('post')->with($this->equalTo(null), $this->equalTo(array('Content-Type' => 'application/json')), $this->equalTo(json_encode($message->getData())), $this->equalTo($options))->will($this->returnValue($request)); $push = $this->getMockBuilder('Mremi\\Flowdock\\Api\\Push\\Push')->setConstructorArgs(array('flow_api_token'))->setMethods(array('createClient'))->getMock(); $push->expects($this->exactly(2))->method('createClient')->with($this->equalTo(sprintf('%s/flow_api_token', $baseUrl)))->will($this->returnValue($client)); $method = new \ReflectionMethod($push, 'sendMessage'); $method->setAccessible(true); $this->assertNull($message->getResponse()); $this->assertFalse($message->hasResponseErrors()); $this->assertTrue($method->invoke($push, $message, $baseUrl, $options)); $this->assertSame($responseOk, $message->getResponse()); $this->assertFalse($message->hasResponseErrors()); $this->assertFalse($method->invoke($push, $message, $baseUrl, $options)); $this->assertSame($responseKo, $message->getResponse()); $this->assertTrue($message->hasResponseErrors()); }