1 | <?php |
---|
2 | |
---|
3 | require_once(__DIR__ . '/../../../api/controller.php'); |
---|
4 | require_once(__DIR__ . '/../../../rest/oauth/OAuth2StorageUserCredential.php'); |
---|
5 | |
---|
6 | class DynamicContactResourceTest extends PHPUnit_Framework_TestCase { |
---|
7 | |
---|
8 | private $classOAuth = null; |
---|
9 | private $user = 'user2'; |
---|
10 | private $pass = 'prognus'; |
---|
11 | private $accessToken = 'aaaaaa259f553ac148f01b6bbcbb101'; |
---|
12 | private $refreshToken = 'rrrrrr03fff3b8cdc51206244529agb'; |
---|
13 | private $url = 'http://expressodev.prognus.com.br/acoutinho/expressoAPI/rest/dynamiccontacts'; |
---|
14 | private $curlOPT; |
---|
15 | private $client_id = 666; |
---|
16 | private $user_id = 666; |
---|
17 | |
---|
18 | public function setUp() { |
---|
19 | $this->classOAuth = new OAuth2StorageUserCredential(); |
---|
20 | session_id($this->refreshToken); |
---|
21 | |
---|
22 | $_SESSION['wallet']['user']['uid'] = $this->user; |
---|
23 | $_SESSION['wallet']['user']['password'] = $this->pass; |
---|
24 | $_SESSION['wallet']['user']['uidNumber'] = 42798; |
---|
25 | |
---|
26 | |
---|
27 | //insere access token |
---|
28 | $this->classOAuth->setAccessToken($this->accessToken, $this->client_id, $this->user_id, (time() + 3600), 'all', $this->refreshToken); |
---|
29 | |
---|
30 | //insere refresh token |
---|
31 | $this->classOAuth->setRefreshToken($this->refreshToken, $this->client_id, $this->user_id, (time() + 3600), 'all'); |
---|
32 | $this->curlOPT = array(CURLOPT_URL => $this->url, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_TIMEOUT => 4, CURLOPT_HTTPHEADER => array('Authorization: OAUTH Bearer aaaaaa259f553ac148f01b6bbcbb101')); |
---|
33 | } |
---|
34 | |
---|
35 | public function testcreateResource() { |
---|
36 | Controller::service('PostgreSQL')->execResultSql("INSERT INTO expressomail_dynamic_contact (owner, name, mail, timestamp) values |
---|
37 | ('42798', 'nameTeste', 'name@teste.com', '" . time() . "000');"); |
---|
38 | |
---|
39 | Controller::commit(array('service' => 'PostgreSQL')); |
---|
40 | } |
---|
41 | |
---|
42 | public function testCheckMaxResource() { |
---|
43 | |
---|
44 | $contact = Controller::service('PostgreSQL')->execResultSql("SELECT id from expressomail_dynamic_contact where owner = 42798"); |
---|
45 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL] . '/' . $contact[0]['id']; |
---|
46 | $c = curl_init(); |
---|
47 | curl_setopt_array($c, $this->curlOPT); |
---|
48 | $r = curl_exec($c); |
---|
49 | curl_close($c); |
---|
50 | |
---|
51 | $body = json_decode($r, true); |
---|
52 | $this->assertCount(1, $body['collection']['items']); |
---|
53 | } |
---|
54 | |
---|
55 | public function testCheckResource() { |
---|
56 | $contact = Controller::service('PostgreSQL')->execResultSql("SELECT id from expressomail_dynamic_contact where owner = 42798"); |
---|
57 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL] . '/' . $contact[0]['id']; |
---|
58 | |
---|
59 | $c = curl_init(); |
---|
60 | curl_setopt_array($c, $this->curlOPT); |
---|
61 | $r = curl_exec($c); |
---|
62 | curl_close($c); |
---|
63 | |
---|
64 | $body = json_decode($r, true); |
---|
65 | |
---|
66 | $this->assertArrayHasKey('collection', $body); |
---|
67 | $this->assertArrayHasKey('data', $body['collection']); |
---|
68 | $this->assertArrayHasKey('name', $body['collection']['data']); |
---|
69 | $this->assertArrayHasKey('prompt', $body['collection']['data']); |
---|
70 | $this->assertArrayHasKey('error', $body['collection']); |
---|
71 | $this->assertArrayHasKey('href', $body['collection']); |
---|
72 | $this->assertArrayHasKey('items', $body['collection']); |
---|
73 | $this->assertArrayHasKey('pagination', $body['collection']); |
---|
74 | $this->assertArrayHasKey('queries', $body['collection']); |
---|
75 | $this->assertArrayHasKey('template', $body['collection']); |
---|
76 | $this->assertArrayHasKey('dataType', $body['collection']['template']['data'][0]); |
---|
77 | $this->assertArrayHasKey('maxLength', $body['collection']['template']['data'][0]); |
---|
78 | $this->assertArrayHasKey('minLength', $body['collection']['template']['data'][0]); |
---|
79 | $this->assertArrayHasKey('name', $body['collection']['template']['data'][0]); |
---|
80 | $this->assertArrayHasKey('prompt', $body['collection']['template']['data'][0]); |
---|
81 | $this->assertArrayHasKey('value', $body['collection']['template']['data'][0]); |
---|
82 | $this->assertArrayHasKey('type', $body['collection']); |
---|
83 | $this->assertArrayHasKey('version', $body); |
---|
84 | } |
---|
85 | |
---|
86 | public function testCheckInvalidId() { |
---|
87 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL] . '/invalidIdXXX'; |
---|
88 | |
---|
89 | $c = curl_init(); |
---|
90 | curl_setopt_array($c, $this->curlOPT); |
---|
91 | $r = curl_exec($c); |
---|
92 | curl_close($c); |
---|
93 | |
---|
94 | $body = json_decode($r, true); |
---|
95 | |
---|
96 | $this->assertArrayHasKey('version', $body); |
---|
97 | $this->assertArrayHasKey('collection', $body); |
---|
98 | $this->assertArrayHasKey('error', $body['collection']); |
---|
99 | $this->assertArrayHasKey('code', $body['collection']['error']); |
---|
100 | $this->assertArrayHasKey('title', $body['collection']['error']); |
---|
101 | $this->assertArrayHasKey('description', $body['collection']['error']); |
---|
102 | } |
---|
103 | |
---|
104 | public function testCheckInvalidDelete() { |
---|
105 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL] . '/invalidXXX'; |
---|
106 | |
---|
107 | $c = curl_init(); |
---|
108 | curl_setopt_array($c, $this->curlOPT); |
---|
109 | curl_setopt($c, CURLOPT_CUSTOMREQUEST, "DELETE"); |
---|
110 | $r = curl_exec($c); |
---|
111 | curl_close($c); |
---|
112 | |
---|
113 | $body = json_decode($r, true); |
---|
114 | |
---|
115 | $this->assertArrayHasKey('version', $body); |
---|
116 | $this->assertArrayHasKey('collection', $body); |
---|
117 | $this->assertArrayHasKey('code', $body['collection']['error']); |
---|
118 | $this->assertArrayHasKey('title', $body['collection']['error']); |
---|
119 | $this->assertArrayHasKey('description', $body['collection']['error']); |
---|
120 | } |
---|
121 | |
---|
122 | public function testCheckDelete() { |
---|
123 | $contact = Controller::service('PostgreSQL')->execResultSql("SELECT id from expressomail_dynamic_contact where owner = 42798"); |
---|
124 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL] . '/' . $contact[0]['id']; |
---|
125 | |
---|
126 | $c = curl_init(); |
---|
127 | curl_setopt_array($c, $this->curlOPT); |
---|
128 | curl_setopt($c, CURLOPT_CUSTOMREQUEST, "DELETE"); |
---|
129 | $r = curl_exec($c); |
---|
130 | curl_close($c); |
---|
131 | |
---|
132 | $body = json_decode($r, true); |
---|
133 | |
---|
134 | $this->assertNull($body); |
---|
135 | } |
---|
136 | |
---|
137 | public function testCheckFinish() { |
---|
138 | Controller::service('PostgreSQL')->execResultSql("DELETE from expressomail_dynamic_contact where owner = ('42798')"); |
---|
139 | |
---|
140 | $c = curl_init(); |
---|
141 | curl_setopt_array($c, $this->curlOPT); |
---|
142 | $r = curl_exec($c); |
---|
143 | curl_close($c); |
---|
144 | $body = json_decode($r, true); |
---|
145 | |
---|
146 | $this->assertNull($body['collection']['items']); |
---|
147 | } |
---|
148 | |
---|
149 | public function tearDown() { |
---|
150 | $this->classOAuth->unsetRefreshToken($this->refreshToken); |
---|
151 | $this->classOAuth->unsetAccessToken($this->accessToken); |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | ?> |
---|