1: <?php
2: namespace ApiAxleTest\Api;
3:
4: require_once __DIR__.'/../../vendor/autoload.php';
5:
6: use ApiAxle\Shared\Config;
7: use ApiAxle\Api\Api;
8: use ApiAxle\Api\Key;
9: use ApiAxle\Shared\ApiException;
10:
11: class ApiTests extends \PHPUnit_Framework_TestCase
12: {
13:
14: public static function tearDownAfterClass()
15: {
16: try {
17: $api = new Api();
18: $apiList = $api->getList();
19: foreach($apiList as $item){
20: if(strpos($item->getName(),'test-') !== false){
21: $api->delete($item->getName());
22: }
23: }
24: } catch(ApiException $ae){
25: echo $ae;
26: } catch(\Exception $e){
27: echo $e;
28: }
29: }
30:
31: public function testConfigAutoInitialized()
32: {
33: $api = new Api();
34: $this->assertInstanceOf('ApiAxle\Shared\Config', $api->getConfig(),
35: 'Returned configuration is not of type ApiAxle\Shared\Config');
36: }
37:
38: public function testListApis()
39: {
40: $api = new Api();
41: try{
42: $apiList = $api->getList();
43:
44: $this->assertInstanceOf('ApiAxle\Shared\ItemList', $apiList);
45: } catch(ApiException $ae){
46: echo $ae;
47: } catch(\Exception $e){
48: echo $e;
49: }
50:
51: }
52:
53: public function testGetApi()
54: {
55: $apiName = 'apiaxle';
56: $api = new Api();
57: $api->get($apiName);
58:
59: $this->assertEquals($apiName, $api->getName());
60: }
61:
62: public function testCreateApi()
63: {
64: $apiName = 'test-'.str_replace(array(' ','.'),'',microtime());
65: $data = array(
66: 'endPoint' => 'localhost'
67: );
68: $api = new Api();
69: try{
70: $api->create($apiName, $data);
71:
72: $this->assertEquals($apiName,$api->getName());
73: } catch(ApiException $ae){
74: echo $ae;
75: } catch(\Exception $e){
76: echo $e;
77: }
78: }
79:
80: public function testCreateUpdateApi()
81: {
82: $apiName = 'test-'.str_replace(array(' ','.'),'',microtime());
83: $data = array(
84: 'endPoint' => 'localhost'
85: );
86: $api = new Api();
87: try{
88: $api->create($apiName, $data);
89: $data = array(
90: 'endPoint' => 'differenthost'
91: );
92: $api->update($data);
93: $apiData = $api->getData();
94: $this->assertRegExp('/[0-9]{1,}/',(string)$apiData['updatedAt']);
95: } catch(ApiException $ae){
96: echo $ae;
97: } catch(\Exception $e){
98: echo $e;
99: }
100: }
101:
102: public function testCreateDeleteApi()
103: {
104: $apiName = 'test-'.str_replace(array(' ','.'),'',microtime());
105: $data = array(
106: 'endPoint' => 'localhost'
107: );
108: $api = new Api();
109: try{
110: $api->create($apiName, $data);
111: $apiList = $api->getList();
112: $hasApi = false;
113: foreach($apiList as $item){
114: if($item->getName() == $apiName){
115: $hasApi = true;
116: break;
117: }
118: }
119: $this->assertTrue($hasApi,'Created api not found in list from server');
120: $api->delete($apiName);
121: $apiList = $api->getList();
122: $hasApi = false;
123: foreach($apiList as $item){
124: if($item->getName() == $apiName){
125: $hasApi = true;
126: break;
127: }
128: }
129: $this->assertFalse($hasApi,'Created API still exists after deletion.');
130: } catch(ApiException $ae){
131: echo $ae;
132: } catch(\Exception $e){
133: echo $e;
134: }
135: }
136:
137: public function testGetKeyCharts()
138: {
139: $apiName = 'apiaxle';
140: $api = new Api();
141: $api->setName($apiName);
142: $keycharts = $api->getKeyCharts();
143:
144: $this->assertInstanceOf('\stdClass', $keycharts);
145: }
146:
147: public function testGetKeyList()
148: {
149: $apiName = 'apiaxle';
150: $api = new Api();
151: $api->setName($apiName);
152: $keyList = $api->getKeyList(0,100,'true');
153:
154: $this->assertInstanceOf('\ApiAxle\Shared\ItemList', $keyList);
155: }
156:
157: public function testLinkUnlinkKey()
158: {
159: $apiName = 'test-'.str_replace(array(' ','.'),'',microtime());
160: $data = array(
161: 'endPoint' => 'localhost'
162: );
163: $api = new Api();
164: $api->create($apiName, $data);
165: $key = new Key();
166: $key->create($apiName);
167: $api->linkKey($key);
168: $hasAssoc = false;
169: $apiList = $key->getApiList();
170: foreach($apiList as $item){
171: if($item->getName() == $apiName){
172: $hasAssoc = true;
173: }
174: }
175: $this->assertTrue($hasAssoc,'New key is not linked to new API');
176: $api->unLinkKey($key);
177: $hasAssoc = false;
178: $apiList = $key->getApiList();
179: foreach($apiList as $item){
180: if($item->getName() == $apiName){
181: $hasAssoc = true;
182: }
183: }
184: $this->assertFalse($hasAssoc,'New key is still linked to new API');
185: }
186:
187: public function testGetStats()
188: {
189: $api = new API();
190: $api->setName('apiaxle');
191: $apiStats = $api->getStats();
192:
193: $this->assertInstanceOf('\stdClass', $apiStats);
194: }
195:
196: public function testGetCharts()
197: {
198: $api = new Api();
199: $charts = $api->getCharts('day');
200:
201: $this->assertInstanceOf('\stdClass', $charts);
202: }
203:
204: }