├── .github └── FUNDING.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── ApcTokenStorage.php ├── AppNexusClient.php ├── ArrayTokenStorage.php ├── HttpClient.php ├── HttpMethod.php ├── MemcachedTokenStorage.php ├── ServerException.php ├── TokenExpiredException.php └── TokenStorage.php └── test ├── ApcTokenStorageTest.php ├── AppNexusClientTest.php ├── ArrayTokenStorageTest.php ├── HttpClientTest.php ├── MemcachedTokenStorageTest.php └── ServerExceptionTest.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: "https://www.paypal.me/f3ath" 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2016 Alexey Karapetov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AppNexusClient 2 | [![Total Downloads](https://img.shields.io/packagist/dt/f3ath/appnexus.svg)](https://packagist.org/packages/f3ath/appnexus) 3 | [![Latest Stable Version](https://img.shields.io/packagist/v/f3ath/appnexus.svg)](https://packagist.org/packages/f3ath/appnexus) 4 | [![Travis Build](https://travis-ci.org/f3ath/appnexusclient.svg?branch=master)](https://travis-ci.org/f3ath/appnexusclient) 5 | [![SensioLabs Insight](https://img.shields.io/sensiolabs/i/3637a8cf-8735-465a-b528-a4ad1edff017.svg)](https://insight.sensiolabs.com/projects/3637a8cf-8735-465a-b528-a4ad1edff017) 6 | 7 | A simple Appnexus API client 8 | 9 | # You can help development of this library by providing me an API key 10 | 11 | # Install 12 | Via [composer](https://getcomposer.org): 13 | `$ composer require "f3ath/appnexus"` 14 | 15 | # Use 16 | ```php 17 | $storage = new F3\AppNexusClient\ArrayTokenStorage(); // Memcached and Apc storage are also available 18 | $appnexus = new F3\AppNexusClient\AppNexusClient('username', 'password', "http://api-console.client-testing.adnxs.net/", $storage); 19 | var_dump($appnexus->call(F3\AppNexusClient\HttpMethod::GET, '/user')); 20 | ``` 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "f3ath/appnexus", 3 | "description": "Simple AppNexus (appnexus.com) client", 4 | "keywords": ["appnexus", "appnexus.com", "api", "client"], 5 | "homepage": "https://github.com/f3ath/AppNexusClient", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Alexey Karapetov", 10 | "email": "karapetov@gmail.com", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.0", 16 | "phpcurl/curlwrapper": "^1.0" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^4.8 || ^5.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "F3\\AppNexusClient\\": "src/" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./test 16 | 17 | 18 | 19 | 20 | ./src/ 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/ApcTokenStorage.php: -------------------------------------------------------------------------------- 1 | 10 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 11 | */ 12 | class ApcTokenStorage implements TokenStorage 13 | { 14 | private $prefix; 15 | private $ttl; 16 | 17 | public function __construct($prefix = '', $ttl = 0 ) 18 | { 19 | $this->prefix = $prefix; 20 | $this->ttl = $ttl; 21 | } 22 | 23 | /** 24 | * set token 25 | * 26 | * @param string $username 27 | * @param string $token 28 | * @return bool 29 | */ 30 | public function set($username, $token) 31 | { 32 | return apc_store($this->prefix.$username, $token, $this->ttl); 33 | } 34 | 35 | /** 36 | * get token 37 | * 38 | * @param string $username 39 | * @return string|false 40 | */ 41 | public function get($username) 42 | { 43 | return apc_fetch($this->prefix.$username); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/AppNexusClient.php: -------------------------------------------------------------------------------- 1 | username = $username; 24 | $this->password = $password; 25 | $this->host = $host; 26 | $this->tokenStorage = $tokenStorage; 27 | $this->http = $http ?: new HttpClient(); 28 | } 29 | 30 | /** 31 | * Get a new auth token from server 32 | * 33 | * @return string 34 | */ 35 | public function getNewToken() 36 | { 37 | $post = array( 38 | 'auth' => array( 39 | 'username' => $this->username, 40 | 'password' => $this->password, 41 | ), 42 | ); 43 | return $this->http->call(HttpMethod::POST, $this->host.'/auth', $post)->token; 44 | } 45 | 46 | /** 47 | * Call the server, (re)authenticating if necessary 48 | * 49 | * @param string $method (GET|POST|PUT|DELETE) 50 | * @param string $path 51 | * @param array $post POST data 52 | * @return object Response object 53 | */ 54 | public function call($method, $path, array $post = array()) 55 | { 56 | $useCachedToken = true; 57 | $token = $this->tokenStorage->get($this->username); 58 | do { 59 | if ( ! $token) { // expired or no token 60 | $token = $this->getNewToken(); 61 | $this->tokenStorage->set($this->username, $token); 62 | $useCachedToken = false; 63 | } 64 | try { 65 | return $this->http->call($method, $this->host.$path, $post, array(sprintf('Authorization: %s', $token))); 66 | } catch (TokenExpiredException $tokenExpired) { 67 | $token = null; // drop the cached token 68 | } 69 | } while ($useCachedToken); // retry if a cached token has been used 70 | throw $tokenExpired; // this means we have a fresh token just expired 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/ArrayTokenStorage.php: -------------------------------------------------------------------------------- 1 | 9 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 10 | */ 11 | class ArrayTokenStorage implements TokenStorage 12 | { 13 | private $storage = array(); 14 | 15 | /** 16 | * set token 17 | * 18 | * @param string $username 19 | * @param string $token 20 | * @return void 21 | */ 22 | public function set($username, $token) 23 | { 24 | $this->storage[$username] = $token; 25 | } 26 | 27 | /** 28 | * get token 29 | * 30 | * @param string $username 31 | * @return string|false 32 | */ 33 | public function get($username) 34 | { 35 | return isset($this->storage[$username]) ? $this->storage[$username] : false; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/HttpClient.php: -------------------------------------------------------------------------------- 1 | curl = $curl ?: new Curl(); 27 | } 28 | 29 | /** 30 | * Do raw HTTP call 31 | * 32 | * @param string $method 33 | * @param string $url 34 | * @param array $post POST data 35 | * @param array $headers 36 | * 37 | * @return object response 38 | */ 39 | public function call($method, $url, array $post = array(), array $headers = array()) 40 | { 41 | $options = array( 42 | CURLOPT_URL => $url, 43 | CURLOPT_RETURNTRANSFER => true, 44 | CURLOPT_HTTPHEADER => $headers, 45 | ); 46 | switch ($method) { 47 | case HttpMethod::GET: 48 | $options[CURLOPT_POST] = false; 49 | break; 50 | case HttpMethod::POST: 51 | $options[CURLOPT_POST] = true; 52 | $options[CURLOPT_POSTFIELDS] = json_encode($post); 53 | break; 54 | case HttpMethod::PUT: 55 | $options[CURLOPT_CUSTOMREQUEST] = 'PUT'; 56 | $options[CURLOPT_POSTFIELDS] = json_encode($post); 57 | break; 58 | case HttpMethod::DELETE: 59 | $options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; 60 | $options[CURLOPT_POSTFIELDS] = json_encode($post); 61 | break; 62 | default: 63 | throw new InvalidArgumentException(sprintf('Invalid method: %s', $method)); 64 | } 65 | 66 | $this->curl->init(); 67 | $this->curl->setOptArray($options); 68 | $rawResponse = $this->curl->exec(1, true); 69 | $contentType = $this->curl->getInfo(CURLINFO_CONTENT_TYPE); 70 | if (strpos($contentType, self::CONTENT_TYPE_JSON) === false) { 71 | return $rawResponse; 72 | } 73 | $response = json_decode($rawResponse); 74 | if (!isset($response->response)) { 75 | throw new RuntimeException(sprintf('Unexpected response: %s', $rawResponse)); 76 | } 77 | $response = $response->response; 78 | if ('OK' == @$response->status) { 79 | return $response; 80 | } elseif ('NOAUTH' == @$response->error_id || 'Authentication failed - not logged in' == @$response->error) { 81 | throw new TokenExpiredException($response); 82 | } 83 | throw new ServerException($response); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/HttpMethod.php: -------------------------------------------------------------------------------- 1 | 11 | * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) 12 | */ 13 | class MemcachedTokenStorage implements TokenStorage 14 | { 15 | /** 16 | * @var Memcached 17 | */ 18 | private $memcached; 19 | 20 | /** 21 | * @var string 22 | */ 23 | private $prefix; 24 | 25 | public function __construct(Memcached $memcached, $prefix = '') 26 | { 27 | $this->memcached = $memcached; 28 | $this->prefix = $prefix; 29 | } 30 | 31 | /** 32 | * set token 33 | * 34 | * @param string $username 35 | * @param string $token 36 | * @return void 37 | */ 38 | public function set($username, $token) 39 | { 40 | $this->memcached->set($this->prefix.$username, $token); 41 | } 42 | 43 | /** 44 | * get token 45 | * 46 | * @param string $username 47 | * @return string|false 48 | */ 49 | public function get($username) 50 | { 51 | return $this->memcached->get($this->prefix.$username); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/ServerException.php: -------------------------------------------------------------------------------- 1 | error); 22 | $this->response = $response; 23 | } 24 | 25 | /** 26 | * getResponse 27 | * 28 | * @return object 29 | */ 30 | public function getResponse() 31 | { 32 | return $this->response; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/TokenExpiredException.php: -------------------------------------------------------------------------------- 1 | markTestSkipped('APC extension is not loaded'); 23 | } 24 | $this->storage = new ApcTokenStorage('pref_', 0); 25 | } 26 | 27 | public function testInterface() 28 | { 29 | $this->assertInstanceOf('F3\\AppNexusClient\\TokenStorage', $this->storage); 30 | } 31 | 32 | public function testSet() 33 | { 34 | $this->assertTrue($this->storage->set('foo', 'bar')); 35 | $this->assertEquals('bar', $this->storage->get('foo')); 36 | 37 | } 38 | 39 | public function testGet() 40 | { 41 | $this->assertEquals('bar', $this->storage->get('foo')); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/AppNexusClientTest.php: -------------------------------------------------------------------------------- 1 | http = $this->getMockBuilder('F3\\AppNexusClient\\HttpClient') 14 | ->setMethods(array('call')) 15 | ->disableOriginalConstructor() 16 | ->getMock(); 17 | $this->storage = $this->getMockBuilder('F3\\AppNexusClient\\TokenStorage') 18 | ->getMock(); 19 | } 20 | 21 | public function testGetNewToken() 22 | { 23 | $client = new AppNexusClient('user', 'pass', 'http://example.com', $this->storage, $this->http); 24 | $this->http->expects($this->once()) 25 | ->method('call') 26 | ->with( 27 | HttpMethod::POST, 28 | 'http://example.com/auth', 29 | array( 30 | 'auth' => array( 31 | 'username' => 'user', 32 | 'password' => 'pass', 33 | ), 34 | ) 35 | ) 36 | ->will($this->returnValue((object) array( 37 | 'status' => 'OK', 38 | 'token' => 'my_token', 39 | ))); 40 | $this->assertEquals('my_token', $client->getNewToken()); 41 | } 42 | 43 | public function testHotCache() 44 | { 45 | $this->storage->expects($this->once()) 46 | ->method('get') 47 | ->with('user') 48 | ->will($this->returnValue('my_token')); 49 | 50 | $client = $this->getMockBuilder('F3\\AppNexusClient\\AppNexusClient') 51 | ->setMethods(array('getNewToken')) 52 | ->setConstructorArgs(array('user', 'pass', 'http://example.com', $this->storage, $this->http)) 53 | ->getMock(); 54 | $client->expects($this->never()) 55 | ->method('getNewToken'); 56 | $response = (object) array('status' => 'OK', 'foo' => 'bar'); 57 | $this->http->expects($this->once()) 58 | ->method('call') 59 | ->with('PUT', 'http://example.com/my_path', array('my_foo' => 'my_bar'), array('Authorization: my_token')) 60 | ->will($this->returnValue($response)); 61 | $this->assertEquals($response, $client->call('PUT', '/my_path', array('my_foo' => 'my_bar'))); 62 | } 63 | 64 | public function testColdCache() 65 | { 66 | $this->storage->expects($this->once()) 67 | ->method('get') 68 | ->with('user') 69 | ->will($this->returnValue(false)); 70 | $this->storage->expects($this->once()) 71 | ->method('set') 72 | ->with('user', 'my_token'); 73 | 74 | $client = $this->getMockBuilder('F3\\AppNexusClient\\AppNexusClient') 75 | ->setMethods(array('getNewToken')) 76 | ->setConstructorArgs(array('user', 'pass', 'http://example.com', $this->storage, $this->http)) 77 | ->getMock(); 78 | $client->expects($this->once()) 79 | ->method('getNewToken') 80 | ->will($this->returnValue('my_token')); 81 | $response = (object) array('status' => 'OK', 'foo' => 'bar'); 82 | $this->http->expects($this->once()) 83 | ->method('call') 84 | ->with('PUT', 'http://example.com/my_path', array('my_foo' => 'my_bar'), array('Authorization: my_token')) 85 | ->will($this->returnValue($response)); 86 | $this->assertEquals($response, $client->call('PUT', '/my_path', array('my_foo' => 'my_bar'))); 87 | } 88 | 89 | public function testHotCacheExpiredToken() 90 | { 91 | $this->storage->expects($this->once()) 92 | ->method('get') 93 | ->with('user') 94 | ->will($this->returnValue('old_token')); 95 | 96 | $this->storage->expects($this->once()) 97 | ->method('set') 98 | ->with('user', 'new_token'); 99 | 100 | $client = $this->getMockBuilder('F3\\AppNexusClient\\AppNexusClient') 101 | ->setMethods(array('getNewToken')) 102 | ->setConstructorArgs(array('user', 'pass', 'http://example.com', $this->storage, $this->http)) 103 | ->getMock(); 104 | $client->expects($this->once()) 105 | ->method('getNewToken') 106 | ->will($this->returnValue('new_token')); 107 | $response = (object) array('status' => 'OK', 'foo' => 'bar'); 108 | $this->http->expects($this->at(0)) 109 | ->method('call') 110 | ->with('PUT', 'http://example.com/my_path', array('my_foo' => 'my_bar'), array('Authorization: old_token')) 111 | ->will($this->throwException(new TokenExpiredException(new stdClass))); 112 | $this->http->expects($this->at(1)) 113 | ->method('call') 114 | ->with('PUT', 'http://example.com/my_path', array('my_foo' => 'my_bar'), array('Authorization: new_token')) 115 | ->will($this->returnValue($response)); 116 | 117 | $this->assertEquals($response, $client->call('PUT', '/my_path', array('my_foo' => 'my_bar'))); 118 | } 119 | 120 | /** 121 | * @expectedException \F3\AppNexusClient\TokenExpiredException 122 | */ 123 | public function testFreshTokenExpired() 124 | { 125 | $this->storage->expects($this->once()) 126 | ->method('get') 127 | ->with('user') 128 | ->will($this->returnValue('old_token')); 129 | 130 | $this->storage->expects($this->once()) 131 | ->method('set') 132 | ->with('user', 'new_token'); 133 | 134 | $client = $this->getMockBuilder('F3\\AppNexusClient\\AppNexusClient') 135 | ->setMethods(array('getNewToken')) 136 | ->setConstructorArgs(array('user', 'pass', 'http://example.com', $this->storage, $this->http)) 137 | ->getMock(); 138 | $client->expects($this->once()) 139 | ->method('getNewToken') 140 | ->will($this->returnValue('new_token')); 141 | $this->http->expects($this->at(0)) 142 | ->method('call') 143 | ->with('PUT', 'http://example.com/my_path', array('my_foo' => 'my_bar'), array('Authorization: old_token')) 144 | ->will($this->throwException(new TokenExpiredException(new stdClass))); 145 | $this->http->expects($this->at(1)) 146 | ->method('call') 147 | ->with('PUT', 'http://example.com/my_path', array('my_foo' => 'my_bar'), array('Authorization: new_token')) 148 | ->will($this->throwException(new TokenExpiredException(new stdClass))); 149 | 150 | $client->call('PUT', '/my_path', array('my_foo' => 'my_bar')); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /test/ArrayTokenStorageTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('F3\\AppNexusClient\\TokenStorage', $storage); 10 | $this->assertFalse($storage->get('foo')); 11 | $storage->set('foo', 'bar'); 12 | $this->assertEquals('bar', $storage->get('foo')); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/HttpClientTest.php: -------------------------------------------------------------------------------- 1 | url = 'http://example.com/test'; 13 | $this->curl = $this->getMockBuilder('PHPCurl\\CurlWrapper\\Curl') 14 | ->getMock(); 15 | $this->http = new HttpClient($this->curl); 16 | } 17 | 18 | public function httpMethods() 19 | { 20 | $url = 'http://example.com/test'; 21 | return array( 22 | array( 23 | HttpMethod::GET, 24 | array( 25 | CURLOPT_URL => $url, 26 | CURLOPT_RETURNTRANSFER => true, 27 | CURLOPT_POST => false, 28 | CURLOPT_HTTPHEADER => array(), 29 | ), 30 | ), 31 | array( 32 | HttpMethod::POST, 33 | array( 34 | CURLOPT_URL => $url, 35 | CURLOPT_RETURNTRANSFER => true, 36 | CURLOPT_POST => true, 37 | CURLOPT_POSTFIELDS => '{"foo":"bar"}', 38 | CURLOPT_HTTPHEADER => array(), 39 | ), 40 | ), 41 | array( 42 | HttpMethod::PUT, 43 | array( 44 | CURLOPT_URL => $url, 45 | CURLOPT_RETURNTRANSFER => true, 46 | CURLOPT_CUSTOMREQUEST => 'PUT', 47 | CURLOPT_POSTFIELDS => '{"foo":"bar"}', 48 | CURLOPT_HTTPHEADER => array(), 49 | ), 50 | ), 51 | array( 52 | HttpMethod::DELETE, 53 | array( 54 | CURLOPT_URL => $url, 55 | CURLOPT_RETURNTRANSFER => true, 56 | CURLOPT_CUSTOMREQUEST => 'DELETE', 57 | CURLOPT_POSTFIELDS => '{"foo":"bar"}', 58 | CURLOPT_HTTPHEADER => array(), 59 | ), 60 | ), 61 | ); 62 | } 63 | 64 | private function initCurlMock($options, $response, $contentType = HttpClient::CONTENT_TYPE_JSON) 65 | { 66 | $this->curl->expects($this->once()) 67 | ->method('setOptArray') 68 | ->with($options); 69 | $this->curl->expects($this->once()) 70 | ->method('exec') 71 | ->will($this->returnValue($response)); 72 | $this->curl->expects($this->once()) 73 | ->method('getInfo') 74 | ->with(CURLINFO_CONTENT_TYPE) 75 | ->will($this->returnValue($contentType)); 76 | } 77 | 78 | /** 79 | * @dataProvider httpMethods 80 | */ 81 | public function testCall($method, $options) 82 | { 83 | $response = (object) array('status' => 'OK'); 84 | $this->initCurlMock($options, json_encode(array('response' => $response))); 85 | $this->assertEquals($response, $this->http->call($method, $this->url, array('foo' => 'bar'))); 86 | } 87 | 88 | /** 89 | * @dataProvider httpMethods 90 | */ 91 | public function testCallWhenResponseIsNotJSON($method, $options) 92 | { 93 | $response ='foo' ; 94 | $this->initCurlMock($options, $response, 'text/html'); 95 | $this->assertEquals($response, $this->http->call($method, $this->url, array('foo' => 'bar'))); 96 | } 97 | 98 | /** 99 | * @dataProvider httpMethods 100 | */ 101 | public function testCallWhenResponseIsJSONWithCharset($method, $options) 102 | { 103 | $response = (object) array('status' => 'OK'); 104 | $contentType = 'application/json; charset=utf-8'; 105 | $this->initCurlMock($options, json_encode(array('response' => $response)), $contentType); 106 | $this->assertEquals($response, $this->http->call($method, $this->url, array('foo' => 'bar'))); 107 | } 108 | 109 | /** 110 | * @dataProvider httpMethods 111 | * @expectedException \RuntimeException 112 | * @expectedExceptionMessage Unexpected response: Pew-pew! 113 | */ 114 | public function testInvalidResponse($method, $options) 115 | { 116 | $this->initCurlMock($options, 'Pew-pew!'); 117 | $this->http->call($method, $this->url, array('foo' => 'bar')); 118 | } 119 | 120 | /** 121 | * @dataProvider httpMethods 122 | * @expectedException \F3\AppNexusClient\ServerException 123 | * @expectedExceptionMessage Foo error 124 | */ 125 | public function testServerError($method, $options) 126 | { 127 | $response = (object) array( 128 | 'status' => 'OMG', 129 | 'error' => 'Foo error', 130 | ); 131 | $this->initCurlMock($options, json_encode(array('response' => $response))); 132 | $this->http->call($method, $this->url, array('foo' => 'bar')); 133 | } 134 | 135 | /** 136 | * @dataProvider httpMethods 137 | * @expectedException \F3\AppNexusClient\TokenExpiredException 138 | */ 139 | public function testTokenExpired($method, $options) 140 | { 141 | $response = (object) array( 142 | 'status' => 'OMG', 143 | 'error_id' => 'NOAUTH', 144 | ); 145 | $this->initCurlMock($options, json_encode(array('response' => $response))); 146 | $this->http->call($method, $this->url, array('foo' => 'bar')); 147 | } 148 | 149 | /** 150 | * @dataProvider httpMethods 151 | * @expectedException \F3\AppNexusClient\TokenExpiredException 152 | */ 153 | public function testTokenExpiredWithErrorMessage($method, $options) 154 | { 155 | $response = (object) array( 156 | 'status' => 'OMG', 157 | 'error_id' => 'SYNTAX', 158 | 'error' => 'Authentication failed - not logged in' 159 | ); 160 | $this->initCurlMock($options, json_encode(array('response' => $response))); 161 | $this->http->call($method, $this->url, array('foo' => 'bar')); 162 | } 163 | 164 | /** 165 | * @expectedException \InvalidArgumentException 166 | * @expectedExceptionMessage Invalid method: BOO 167 | */ 168 | public function testInvalidMethod() 169 | { 170 | $this->http->call('BOO', $this->url); 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /test/MemcachedTokenStorageTest.php: -------------------------------------------------------------------------------- 1 | mc = $this->getMockBuilder('Memcached') 12 | ->setMethods(array('set', 'get')) 13 | ->getMock(); 14 | $this->storage = new MemcachedTokenStorage($this->mc, 'pref_'); 15 | } 16 | 17 | public function testInterface() 18 | { 19 | $this->assertInstanceOf('F3\\AppNexusClient\\TokenStorage', $this->storage); 20 | } 21 | 22 | public function testSet() 23 | { 24 | $this->mc->expects($this->once()) 25 | ->method('set') 26 | ->with('pref_foo', 'bar'); 27 | $this->storage->set('foo', 'bar'); 28 | } 29 | 30 | public function testGet() 31 | { 32 | $this->mc->expects($this->once()) 33 | ->method('get') 34 | ->with('pref_foo') 35 | ->will($this->returnValue('bar')); 36 | $this->assertEquals('bar', $this->storage->get('foo')); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/ServerExceptionTest.php: -------------------------------------------------------------------------------- 1 | assertSame($response, $e->getResponse()); 14 | } 15 | 16 | public function testMessage() 17 | { 18 | $response = new stdClass; 19 | $response->error = 'foo'; 20 | 21 | $e = new ServerException($response); 22 | $this->assertEquals('foo', $e->getMessage()); 23 | } 24 | } 25 | --------------------------------------------------------------------------------