1: <?php
2: namespace ApiAxleTest\Api;
3:
4: require_once __DIR__.'/../../vendor/autoload.php';
5:
6: use ApiAxle\Shared\Config;
7: use ApiAxle\Api\Key;
8: use ApiAxle\Api\Api;
9: use ApiAxle\Shared\ApiException;
10:
11: class KeyTests extends \PHPUnit_Framework_TestCase
12: {
13: public static function tearDownAfterClass()
14: {
15: try {
16: $key = new Key();
17: $keyList = $key->getList();
18: foreach($keyList as $item){
19: if(strpos($item->getKey(),'test-') !== false){
20: $key->delete($item->getKey());
21: }
22: }
23: } catch(ApiException $ae){
24: echo $ae;
25: } catch(\Exception $e){
26: echo $e;
27: }
28: }
29:
30: public function testCreateKey()
31: {
32: $keyValue = 'test-'.str_replace(array(' ','.'),'',microtime());
33: $key = new Key();
34: $key->create($keyValue);
35: $this->assertEquals($keyValue, $key->getKey());
36: }
37:
38: public function testCreateUpdateKey()
39: {
40: $keyValue = 'test-'.str_replace(array(' ','.'),'',microtime());
41: $key = new Key();
42: $createData = array(
43: 'sharedSecret' => 'firstsecret'
44: );
45: $key->create($keyValue,$createData);
46: $updateData = array(
47: 'sharedSecret' => 'updatedsecret'
48: );
49: $key->update($updateData);
50: $keyData = $key->getData();
51: $this->assertEquals($updateData['sharedSecret'], $keyData['sharedSecret']);
52: }
53:
54: public function testCreateDeleteKey()
55: {
56: $keyValue = 'test-'.str_replace(array(' ','.'),'',microtime());
57: $key = new Key();
58: $key->create($keyValue);
59: $keyList = $key->getList();
60: $hasKey = false;
61: foreach($keyList as $item){
62: if($item->getKey() == $keyValue){
63: $hasKey = true;
64: break;
65: }
66: }
67: $this->assertTrue($hasKey,'Created key not found in list from server');
68: $key->delete($keyValue);
69: $keyList = $key->getList();
70: $hasKey = false;
71: foreach($keyList as $item){
72: if($item->getKey() == $keyValue){
73: $hasKey = true;
74: break;
75: }
76: }
77: $this->assertFalse($hasKey,'Created key still exists after deletion.');
78: }
79:
80: public function testListKeys()
81: {
82: $key = new Key();
83: $keyList = $key->getList();
84: $this->assertInstanceOf('ApiAxle\Shared\ItemList', $keyList);
85: }
86:
87: public function testGetApiList()
88: {
89: $config = new Config();
90: $key = new Key();
91: $key->setKey($config->getKey());
92: $apiList = $key->getApiList();
93:
94: $this->assertInstanceOf('ApiAxle\Shared\ItemList', $apiList);
95: }
96:
97: public function testGetApiCharts()
98: {
99: $config = new Config();
100: $key = new Key();
101: $key->setKey($config->getKey());
102: $apiCharts = $key->getApiCharts();
103:
104: $this->assertInstanceOf('\stdClass', $apiCharts);
105: }
106:
107: public function testGetStatsForAllApis()
108: {
109: $config = new Config();
110: $key = new Key();
111: $key->setKey($config->getKey());
112: $apiStats = $key->getStats();
113:
114: $this->assertInstanceOf('\stdClass', $apiStats);
115: }
116:
117: public function testGetStatsForSpecificApi()
118: {
119: $config = new Config();
120: $key = new Key();
121: $key->setKey($config->getKey());
122: $apiStats = $key->getStats(false,false,'minute',true,'epoch_seconds','apiaxle');
123:
124: $this->assertInstanceOf('\stdClass', $apiStats);
125: $api = new Api();
126: $api->setName('apiaxle');
127: $apiStats = $key->getStats(false,false,'minute',true,'epoch_seconds',$api);
128:
129: $this->assertInstanceOf('\stdClass', $apiStats);
130: }
131:
132: public function testGetCharts()
133: {
134: $key = new Key();
135: $charts = $key->getCharts();
136:
137: $this->assertInstanceOf('\stdClass', $charts);
138: }
139: }