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('rrrrrr03fff3b8cdc51206244529agb'); |
---|
21 | |
---|
22 | $_SESSION['wallet']['user']['uid'] = $this->user; |
---|
23 | $_SESSION['wallet']['user']['password'] = $this->pass; |
---|
24 | $_SESSION['wallet']['user']['uidNumber'] = 42798; |
---|
25 | //insere access token |
---|
26 | $this->classOAuth->setAccessToken($this->accessToken, $this->client_id, $this->user_id, (time() + 3600), 'all', $this->refreshToken); |
---|
27 | |
---|
28 | //insere refresh token |
---|
29 | $this->classOAuth->setRefreshToken($this->refreshToken, $this->client_id, $this->user_id, (time() + 3600), 'all'); |
---|
30 | $this->curlOPT = array(CURLOPT_URL => $this->url, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_TIMEOUT => 4, CURLOPT_HTTPHEADER => array('Authorization: OAUTH Bearer aaaaaa259f553ac148f01b6bbcbb101')); |
---|
31 | } |
---|
32 | |
---|
33 | public function testcreateResource() { |
---|
34 | |
---|
35 | for ($i = 0; $i < 20; $i++) { |
---|
36 | Controller::service('PostgreSQL')->execResultSql("INSERT INTO expressomail_dynamic_contact (owner, name, mail, timestamp) values |
---|
37 | ('42798', 'nameTeste" . $i . "', 'name" . $i . "@teste.com', '" . time() . "000');"); |
---|
38 | } |
---|
39 | Controller::commit(array('service' => 'PostgreSQL')); |
---|
40 | } |
---|
41 | |
---|
42 | public function testCheckMaxResource() { |
---|
43 | $c = curl_init(); |
---|
44 | curl_setopt_array($c, $this->curlOPT); |
---|
45 | $r = curl_exec($c); |
---|
46 | curl_close($c); |
---|
47 | |
---|
48 | $body = json_decode($r, true); |
---|
49 | $this->assertCount(20, $body['collection']['items']); |
---|
50 | } |
---|
51 | |
---|
52 | public function testCheckResource() { |
---|
53 | $c = curl_init(); |
---|
54 | curl_setopt_array($c, $this->curlOPT); |
---|
55 | $r = curl_exec($c); |
---|
56 | curl_close($c); |
---|
57 | |
---|
58 | $body = json_decode($r, true); |
---|
59 | |
---|
60 | $this->assertArrayHasKey('collection', $body); |
---|
61 | $this->assertArrayHasKey('data', $body['collection']); |
---|
62 | $this->assertArrayHasKey('name', $body['collection']['data']); |
---|
63 | $this->assertArrayHasKey('prompt', $body['collection']['data']); |
---|
64 | $this->assertArrayHasKey('error', $body['collection']); |
---|
65 | $this->assertArrayHasKey('href', $body['collection']); |
---|
66 | $this->assertArrayHasKey('items', $body['collection']); |
---|
67 | $this->assertArrayHasKey('pagination', $body['collection']); |
---|
68 | $this->assertArrayHasKey('queries', $body['collection']); |
---|
69 | $this->assertArrayHasKey('template', $body['collection']); |
---|
70 | $this->assertArrayHasKey('dataType', $body['collection']['template']['data'][0]); |
---|
71 | $this->assertArrayHasKey('maxLength', $body['collection']['template']['data'][0]); |
---|
72 | $this->assertArrayHasKey('minLength', $body['collection']['template']['data'][0]); |
---|
73 | $this->assertArrayHasKey('name', $body['collection']['template']['data'][0]); |
---|
74 | $this->assertArrayHasKey('prompt', $body['collection']['template']['data'][0]); |
---|
75 | $this->assertArrayHasKey('value', $body['collection']['template']['data'][0]); |
---|
76 | $this->assertArrayHasKey('type', $body['collection']); |
---|
77 | $this->assertArrayHasKey('version', $body); |
---|
78 | } |
---|
79 | |
---|
80 | public function testCreateNewResource() { |
---|
81 | |
---|
82 | $data = array('name' => 'testUnitário', 'email' => 'teste@unitario.com.br'); |
---|
83 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL]; |
---|
84 | $c = curl_init(); |
---|
85 | curl_setopt_array($c, $this->curlOPT); |
---|
86 | curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
---|
87 | curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST"); |
---|
88 | $r = curl_exec($c); |
---|
89 | curl_close($c); |
---|
90 | $body = json_decode($r, true); |
---|
91 | |
---|
92 | $this->assertNull($body); |
---|
93 | } |
---|
94 | |
---|
95 | public function testCreateInvalidNewResource() { |
---|
96 | |
---|
97 | $data = array('name' => 'testUnitário', 'emailinvalido' => 'teste@unitario.com.br'); |
---|
98 | $this->curlOPT[CURLOPT_URL] = $this->curlOPT[CURLOPT_URL]; |
---|
99 | $c = curl_init(); |
---|
100 | curl_setopt_array($c, $this->curlOPT); |
---|
101 | curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
---|
102 | curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST"); |
---|
103 | $r = curl_exec($c); |
---|
104 | curl_close($c); |
---|
105 | $body = json_decode($r, true); |
---|
106 | |
---|
107 | $this->assertArrayHasKey('version', $body); |
---|
108 | $this->assertArrayHasKey('collection', $body); |
---|
109 | $this->assertArrayHasKey('code', $body['collection']['error']); |
---|
110 | $this->assertArrayHasKey('title', $body['collection']['error']); |
---|
111 | $this->assertArrayHasKey('description', $body['collection']['error']); |
---|
112 | } |
---|
113 | |
---|
114 | public function testCheckFinish() { |
---|
115 | Controller::service('PostgreSQL')->execResultSql("DELETE from expressomail_dynamic_contact where owner = ('42798')"); |
---|
116 | |
---|
117 | $c = curl_init(); |
---|
118 | curl_setopt_array($c, $this->curlOPT); |
---|
119 | $r = curl_exec($c); |
---|
120 | curl_close($c); |
---|
121 | $body = json_decode($r, true); |
---|
122 | |
---|
123 | $this->assertNull($body['collection']['items']); |
---|
124 | } |
---|
125 | |
---|
126 | public function tearDown() { |
---|
127 | $this->classOAuth->unsetRefreshToken($this->refreshToken); |
---|
128 | $this->classOAuth->unsetAccessToken($this->accessToken); |
---|
129 | } |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | ?> |
---|