source: sandbox/2.4-expresso-api/prototype/tests/rest/calendar/CalendarLastResourceTest.php @ 5888

Revision 5888, 5.6 KB checked in by cristiano, 12 years ago (diff)

Ticket #2598 - implementação base REST + oauth

  • Property svn:executable set to *
Line 
1<?php
2require_once(__DIR__ . '/../../../api/controller.php');
3//require_once(__DIR__ . '/../../../modules/calendar/interceptors/DBMapping.php');
4require_once(__DIR__ . '/../../../rest/oauth/OAuth2StorageUserCredential.php');
5
6
7class CalendarLastResourceTest extends PHPUnit_Framework_TestCase {
8
9        private $classOAuth = null;
10       
11        private $user = 'user2';
12        private $pass = 'prognus';
13        private $accessToken = 'aaaaaa259f553ac148f01b6bbcbb101';
14        private $refreshToken = 'rrrrrr03fff3b8cdc51206244529agb';
15        private $url = 'http://expressodev.prognus.com.br/cristiano/expresso-api/rest/calendarlast';
16        private $curlOPT;
17        private $client_id = 666;
18        private $user_id = 666;
19
20       
21        public function setUp(){
22            $this->classOAuth = new OAuth2StorageUserCredential();
23               
24                session_id('rrrrrr03fff3b8cdc51206244529agb');
25
26            $_SESSION['wallet']['user']['uid']  =   $this->user;
27            $_SESSION['wallet']['user']['password'] =   $this->pass;
28                $_SESSION['wallet']['user']['uidNumber'] = 42798 ;
29               
30            //insere access token
31            $this->classOAuth->setAccessToken($this->accessToken, $this->client_id, $this->user_id, (time() + 3600), 'all', $this->refreshToken);
32
33            //insere refresh token
34            $this->classOAuth->setRefreshToken($this->refreshToken, $this->client_id, $this->user_id, (time() + 3600), 'all');
35            $this->curlOPT = array( CURLOPT_URL => $this->url,  CURLOPT_HEADER => 0,  CURLOPT_RETURNTRANSFER => TRUE,  CURLOPT_TIMEOUT => 4 , CURLOPT_HTTPHEADER => array( 'Authorization: OAUTH Bearer aaaaaa259f553ac148f01b6bbcbb101'));
36        }
37
38        public function testcreateResource() {
39           
40            $calendar = Controller::service('PostgreSQL')->execResultSql("INSERT INTO calendar (location, tzid, name, dtstamp, description) values
41                ('location', 'America/Sao_Paulo', 'PHP-UNIt Teste', '".time()."000', 'Agenda de teste :D') returning id;");
42           
43            if(is_array($calendar))
44                        $calendar = $calendar[0]['id'];
45           
46            $signature = Controller::create(array('concept' => 'calendarSignature'),
47              array('calendar' => $calendar,
48                    'user' => 42798,
49                    'isOwner' => '1',
50                    'fontColor' => '000000',
51                    'backgroundColor' => '000000',
52                    'borderColor' => '000000'
53                ));
54
55                for($i = 0; $i < 10; $i++){     
56               
57                        $date   = date('Ymd\THisT');
58            $unique = substr(microtime(), 2, 4);
59            $base   = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPrRsStTuUvVxXuUvVwWzZ1234567890';
60            $start  = 0;
61            $end    = strlen( $base ) - 1;
62            $length = 6;
63            $str    = null;
64            for( $p = 0; $p < $length; $p++ )
65              $unique .= $base{mt_rand( $start, $end )};
66
67            $uid = $date.$unique.'@expresso-calendar';
68               
69               
70                        $idEvent = Controller::service('PostgreSQL')->execResultSql("INSERT INTO calendar_object (location, cal_uid, class_id,
71                                dtstamp, description, dtstart, dtend, allday, range_start, range_end, summary, type_id, last_update, tzid ) values
72                                ('PHP UNIT', '".$uid."', '1',
73                                '".time()."000', 'Evento de teste PHP-Unit',
74                                '".time()."000', '".(time() - 400)."000', '0',
75                                '".time()."000', '".(time() + 400)."000',
76                                'Eventos de teste aqui', '1', '".time()."000', 'America/Sao_Paulo') returning id;");
77               
78                        Controller::service('PostgreSQL')->execResultSql("INSERT INTO calendar_to_calendar_object (calendar_id, calendar_object_id)
79                        values ('".$calendar."', '".$idEvent[0]['id']."')");
80               
81                }
82               
83                Controller::commit( array( 'service' => 'PostgreSQL' ) );
84        }
85
86        public function testCheckMaxResource() {
87            $c = curl_init();
88            curl_setopt_array($c, $this->curlOPT);
89                $r = curl_exec($c);
90            curl_close($c);
91
92            $body = json_decode($r,true);               
93            $this->assertCount(10, $body);
94        }
95       
96        public function testCheckResource() {
97            $c = curl_init();
98            curl_setopt_array($c, $this->curlOPT);
99            $r = curl_exec($c);
100            curl_close($c);
101
102            $body = json_decode($r,true);
103 
104            foreach($body as $key => $value){
105                        $this->assertArrayHasKey('allDay', $body[$key]);
106                        $this->assertArrayHasKey('calendar', $body[$key]);
107                        $this->assertArrayHasKey('class', $body[$key]);
108                        $this->assertArrayHasKey('description', $body[$key]);
109                        $this->assertArrayHasKey('dtstamp', $body[$key]);
110                        $this->assertArrayHasKey('id', $body[$key]);
111                        $this->assertArrayHasKey('lastUpdate', $body[$key]);
112                        $this->assertArrayHasKey('location', $body[$key]);
113                        $this->assertArrayHasKey('rangeEnd', $body[$key]);
114                        $this->assertArrayHasKey('rangeStart', $body[$key]);
115                        $this->assertArrayHasKey('sequence', $body[$key]);
116            }
117
118        }
119       
120        public function testCheckFinish() {   
121           
122                $calendar = Controller::service('PostgreSQL')->execResultSql("SELECT calendar_id from calendar_signature where user_uidnumber = 42798");
123                $relation = Controller::service('PostgreSQL')->execResultSql("SELECT calendar_object_id from calendar_to_calendar_object where calendar_id = ".$calendar[0]['calendar_id']);
124                       
125                $objects = "";
126                $count = count($relation);
127               
128                for($i = 0; $i < $count; $i++)
129                        $objects .=  $relation[$i]['calendar_object_id']. ($i == ($count-1) ? '' : ',');
130
131                Controller::service('PostgreSQL')->execResultSql("DELETE from calendar_object where id IN (". $objects .")");
132
133            $c = curl_init();
134            curl_setopt_array($c, $this->curlOPT);
135            $r = curl_exec($c);
136            curl_close($c);
137                $body = json_decode($r,true);
138               
139                Controller::service('PostgreSQL')->execResultSql("DELETE from calendar_signature where user_uidnumber = 42798");
140                Controller::service('PostgreSQL')->execResultSql("DELETE from calendar where id = ".$calendar[0]['calendar_id']);
141
142            $this->assertCount(0 , $body);
143        }
144
145        public function tearDown() {
146            $this->classOAuth->unsetRefreshToken($this->refreshToken);
147            $this->classOAuth->unsetAccessToken($this->accessToken);       
148        }
149}
150?>
Note: See TracBrowser for help on using the repository browser.