/** * @group unit */ public function testToString() { $path = 'test'; $method = Request::POST; $query = array('no' => 'params'); $data = array('key' => 'value'); $connection = new Connection(); $connection->setHost($this->_getHost()); $connection->setPort('9200'); $request = new Request($path, $method, $data, $query, $connection); $data = $request->toArray(); $this->assertInternalType('array', $data); $this->assertArrayHasKey('method', $data); $this->assertArrayHasKey('path', $data); $this->assertArrayHasKey('query', $data); $this->assertArrayHasKey('data', $data); $this->assertArrayHasKey('connection', $data); $this->assertEquals($request->getMethod(), $data['method']); $this->assertEquals($request->getPath(), $data['path']); $this->assertEquals($request->getQuery(), $data['query']); $this->assertEquals($request->getData(), $data['data']); $this->assertInternalType('array', $data['connection']); $this->assertArrayHasKey('host', $data['connection']); $this->assertArrayHasKey('port', $data['connection']); $this->assertEquals($request->getConnection()->getHost(), $data['connection']['host']); $this->assertEquals($request->getConnection()->getPort(), $data['connection']['port']); $string = $request->toString(); $this->assertInternalType('string', $string); $string = (string) $request; $this->assertInternalType('string', $string); }
/** * Converts Request to Curl console command * * @param Request $request * @return string */ public static function convertRequestToCurlCommand(Request $request) { $message = 'curl -X' . strtoupper($request->getMethod()) . ' '; $message .= '\'http://' . $request->getConnection()->getHost() . ':' . $request->getConnection()->getPort() . '/'; $message .= $request->getPath(); $query = $request->getQuery(); if (!empty($query)) { $message .= '?' . http_build_query($query); } $message .= '\''; $data = $request->getData(); if (!empty($data)) { $message .= ' -d \'' . JSON::stringify($data) . '\''; } return $message; }