public function testAccessorsMutators() { $obj = new Query(); $this->assertEquals(sprintf('%s/?', Consumer::API_ENDPOINT), $obj->getUrl(), 'Url do not match'); $this->assertEquals('GET', $obj->getHttpMethod(), 'http_method should be GET'); $this->assertEquals($obj, $obj->setHttpMethod('POST'), 'Fluent interface not working'); $this->assertEquals('POST', $obj->getHttpMethod(), 'http_method should be POST'); $this->assertEquals(sprintf('%s/', Consumer::API_ENDPOINT), $obj->getUrl(), 'Url do not match'); $this->assertNull($obj->getApiMethod(), 'api_method should be null'); $this->assertEquals($obj, $obj->setApiMethod('1.1/api/method'), 'Fluent interface not working'); $this->assertEquals('1.1/api/method', $obj->getApiMethod(), 'api_method should be "1.1/api/method"'); $this->assertEquals(array(), $obj->getHeaders(), 'headers should be empty array'); $this->assertEquals($obj, $obj->setHeaders(array('X-Header', 'yes')), 'Fluent interface not working'); $this->assertEquals(array('X-Header', 'yes'), $obj->getHeaders(), 'headers should be array - but not empty'); $this->assertEquals('', $obj->getContent(), 'content should be empty string'); $this->assertEquals($obj, $obj->setContent('test string'), 'Fluent interface not working'); $this->assertEquals('test string', $obj->getContent(), 'content should be "test string"'); $this->assertEquals(array(), $obj->getQueryString(), 'query_string should be empty array'); $this->assertEquals($obj, $obj->setQueryString(array('key' => 'val')), 'Fluent interface not working'); $this->assertEquals(array('key' => 'val'), $obj->getQueryString(), 'query_string should be non empty array'); $obj->setQueryString('key1=val1&key2=val2'); $this->assertEquals(array('key1' => 'val1', 'key2' => 'val2'), $obj->getQueryString(), 'query_string should be non empty array'); }
public function testPrepareExecute() { $client = $this->getClient(); $headers = array('User-Agent' => 'Twitter API Consumer (https://github.com/nixilla/twitter-api-consumer)', 'Authorization' => sprintf('Basic %s', 'YWFhYTpiYmJi')); $client->expects($this->at(0))->method('post')->with(sprintf('%s/oauth2/token', Consumer::API_ENDPOINT), $headers, 'grant_type=client_credentials')->will($this->returnValue($this->getResponse(200, array('token_type' => 'bearer', 'access_token' => 'abc1234')))); $query = new Query(); $query->setHttpMethod('GET'); $query->setApiMethod('1.1/search/tweets.json'); $query->setHeaders(array('User-Agent' => 'Twitter API Consumer (https://github.com/nixilla/twitter-api-consumer)', 'Authorization' => sprintf('Bearer %s', 'abc1234'))); $query->setQueryString(array('q' => urlencode('@nixilla'))); $client->expects($this->at(1))->method('call')->with(sprintf('%s/1.1/search/tweets.json?%s', Consumer::API_ENDPOINT, http_build_query(array('q' => urlencode('@nixilla')))), 'GET', array('User-Agent' => 'Twitter API Consumer (https://github.com/nixilla/twitter-api-consumer)', 'Authorization' => sprintf('Bearer %s', 'abc1234')), '')->will($this->returnValue($this->getResponse(200, array('some value')))); $consumer = new Consumer($client, 'aaaa', 'bbbb'); $q = $consumer->prepare('/1.1/search/tweets.json', RequestInterface::METHOD_GET, array('q' => urlencode('@nixilla'))); $this->assertEquals($query, $q, 'These 2 objects are not the same'); $result = $consumer->execute($q); $this->assertTrue($result instanceof Result, '$result is not instance of Twitter\\Result'); }
/** * @param $api_method * @param string $http_method * @param array $query_string * @param array $headers * @param string $content * @return QueryInterface */ public function prepare($api_method, $http_method = 'GET', array $query_string = array(), $headers = array(), $content = '') { if (!$this->access_token) { $this->obtainBearerToken(); } $query = new Query(); $query->setApiMethod($this->normalizeApiMethod($api_method)); $query->setHttpMethod($http_method); $query->setHeaders($this->getHeaders($headers)); $query->setContent($content); $query->setQueryString($query_string); return $query; }