1use FindBin;
2use lib "$FindBin::Bin/lib";
3use JSON::MaybeXS qw( JSON );
4use Pithub::Test::Factory;
5use Test::Most import => [ qw( done_testing eq_or_diff is isa_ok ok throws_ok use_ok ) ];
6
7BEGIN {
8    use_ok('Pithub::PullRequests');
9    use_ok('Pithub::PullRequests::Comments');
10    use_ok('Pithub::PullRequests::Reviewers');
11}
12
13# Pithub::PullRequests->commits
14{
15    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
16
17    throws_ok { $obj->commits } qr{Missing key in parameters: pull_request_id}, 'No parameters';
18
19    isa_ok $obj, 'Pithub::PullRequests';
20
21    {
22        my $result = $obj->commits( pull_request_id => 1 );
23        is $result->request->method, 'GET', 'HTTP method';
24        is $result->request->uri->path, '/repos/foo/bar/pulls/1/commits', 'HTTP path';
25        my $http_request = $result->request;
26        is $http_request->content, '', 'HTTP body';
27    }
28}
29
30# Pithub::PullRequests->create
31{
32    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
33
34    isa_ok $obj, 'Pithub::PullRequests';
35
36    throws_ok { $obj->create } qr{Missing key in parameters: data \(hashref\)}, 'No data parameter';
37    throws_ok { $obj->create( data => { foo => 'bar' } ); } qr{Access token required for: POST /repos/foo/bar/pulls}, 'Token required';
38
39    ok $obj->token(123), 'Token set';
40
41    {
42        my $json   = JSON->new;
43        my $result = $obj->create(
44            data => {
45                base  => 'master',
46                body  => 'Please pull this in!',
47                head  => 'octocat:new-feature',
48                title => 'Amazing new feature',
49            }
50        );
51        is $result->request->method, 'POST', 'HTTP method';
52        is $result->request->uri->path, '/repos/foo/bar/pulls', 'HTTP path';
53        my $http_request = $result->request;
54        eq_or_diff $json->decode( $http_request->content ),
55          { 'body' => 'Please pull this in!', 'base' => 'master', 'head' => 'octocat:new-feature', 'title' => 'Amazing new feature' }, 'HTTP body';
56    }
57}
58
59# Pithub::PullRequests->files
60{
61    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
62
63    throws_ok { $obj->files } qr{Missing key in parameters: pull_request_id}, 'No parameters';
64
65    isa_ok $obj, 'Pithub::PullRequests';
66
67    {
68        my $result = $obj->files( pull_request_id => 1 );
69        is $result->request->method, 'GET', 'HTTP method';
70        is $result->request->uri->path, '/repos/foo/bar/pulls/1/files', 'HTTP path';
71        my $http_request = $result->request;
72        is $http_request->content, '', 'HTTP body';
73    }
74}
75
76# Pithub::PullRequests->get
77{
78    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
79
80    throws_ok { $obj->get } qr{Missing key in parameters: pull_request_id}, 'No parameters';
81
82    isa_ok $obj, 'Pithub::PullRequests';
83
84    {
85        my $result = $obj->get( pull_request_id => 1 );
86        is $result->request->method, 'GET', 'HTTP method';
87        is $result->request->uri->path, '/repos/foo/bar/pulls/1', 'HTTP path';
88        my $http_request = $result->request;
89        is $http_request->content, '', 'HTTP body';
90    }
91}
92
93# Pithub::PullRequests->is_merged
94{
95    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
96
97    throws_ok { $obj->is_merged } qr{Missing key in parameters: pull_request_id}, 'No parameters';
98
99    isa_ok $obj, 'Pithub::PullRequests';
100
101    {
102        my $result = $obj->is_merged( pull_request_id => 1 );
103        is $result->request->method, 'GET', 'HTTP method';
104        is $result->request->uri->path, '/repos/foo/bar/pulls/1/merge', 'HTTP path';
105        my $http_request = $result->request;
106        is $http_request->content, '', 'HTTP body';
107    }
108}
109
110# Pithub::PullRequests->merge
111{
112    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
113
114    isa_ok $obj, 'Pithub::PullRequests';
115
116    throws_ok { $obj->merge } qr{Missing key in parameters: pull_request_id}, 'No parameters';
117    throws_ok { $obj->merge( pull_request_id => 123 ); } qr{Access token required for: PUT /repos/foo/bar/pulls/123/merge}, 'Token required';
118
119    ok $obj->token(123), 'Token set';
120
121    {
122        my $result = $obj->merge( pull_request_id => 123 );
123        is $result->request->method, 'PUT', 'HTTP method';
124        is $result->request->uri->path, '/repos/foo/bar/pulls/123/merge', 'HTTP path';
125        my $http_request = $result->request;
126        is $http_request->content, '', 'HTTP body';
127    }
128}
129
130# Pithub::PullRequests->update
131{
132    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests', user => 'foo', repo => 'bar' );
133
134    isa_ok $obj, 'Pithub::PullRequests';
135
136    throws_ok { $obj->update } qr{Missing key in parameters: pull_request_id}, 'No parameter';
137    throws_ok { $obj->update( pull_request_id => 1 ) } qr{Missing key in parameters: data \(hashref\)}, 'No data parameter';
138    throws_ok { $obj->update( pull_request_id => 5, data => { foo => 'bar' } ); } qr{Access token required for: PATCH /repos/foo/bar/pulls/5}, 'Token required';
139
140    ok $obj->token(123), 'Token set';
141
142    {
143        my $json   = JSON->new;
144        my $result = $obj->update(
145            pull_request_id => 123,
146            data            => {
147                base  => 'master',
148                body  => 'Please pull this in!',
149                head  => 'octocat:new-feature',
150                title => 'Amazing new feature',
151            }
152        );
153        is $result->request->method, 'PATCH', 'HTTP method';
154        is $result->request->uri->path, '/repos/foo/bar/pulls/123', 'HTTP path';
155        my $http_request = $result->request;
156        eq_or_diff $json->decode( $http_request->content ),
157          { 'body' => 'Please pull this in!', 'base' => 'master', 'head' => 'octocat:new-feature', 'title' => 'Amazing new feature' }, 'HTTP body';
158    }
159}
160
161# Pithub::PullRequests::Comments->create
162{
163    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Comments', user => 'foo', repo => 'bar' );
164
165    isa_ok $obj, 'Pithub::PullRequests::Comments';
166
167    throws_ok { $obj->create } qr{Missing key in parameters: pull_request_id}, 'No parameters';
168    throws_ok { $obj->create( pull_request_id => 123 ) } qr{Missing key in parameters: data \(hashref\)}, 'No data parameter';
169    throws_ok { $obj->create( pull_request_id => 123, data => 5 ) } qr{Missing key in parameters: data \(hashref\)}, 'Wrong type';
170    throws_ok {
171        $obj->create( pull_request_id => 123, data => { foo => 'bar' } );
172    }
173    qr{Access token required for: POST /repos/foo/bar/pulls/123/comments}, 'Token required';
174
175    ok $obj->token(123), 'Token set';
176
177    {
178        my $json = JSON->new;
179        my $result = $obj->create( pull_request_id => 123, data => { body => 'some comment' } );
180        is $result->request->method, 'POST', 'HTTP method';
181        is $result->request->uri->path, '/repos/foo/bar/pulls/123/comments', 'HTTP path';
182        my $http_request = $result->request;
183        eq_or_diff $json->decode( $http_request->content ), { 'body' => 'some comment' }, 'HTTP body';
184    }
185}
186
187# Pithub::PullRequests::Comments->delete
188{
189    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Comments', user => 'foo', repo => 'bar' );
190
191    isa_ok $obj, 'Pithub::PullRequests::Comments';
192
193    throws_ok { $obj->delete } qr{Missing key in parameters: comment_id}, 'No parameters';
194    throws_ok { $obj->delete( comment_id => 123 ); } qr{Access token required for: DELETE /repos/foo/bar/pulls/comments/123}, 'Token required';
195
196    ok $obj->token(123), 'Token set';
197
198    {
199        my $result = $obj->delete( comment_id => 456 );
200        is $result->request->method, 'DELETE', 'HTTP method';
201        is $result->request->uri->path, '/repos/foo/bar/pulls/comments/456', 'HTTP path';
202        my $http_request = $result->request;
203        is $http_request->content, '', 'HTTP body';
204    }
205}
206
207# Pithub::PullRequests::Comments->get
208{
209    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Comments', user => 'foo', repo => 'bar' );
210
211    isa_ok $obj, 'Pithub::PullRequests::Comments';
212
213    throws_ok { $obj->get } qr{Missing key in parameters: comment_id}, 'No parameters';
214
215    {
216        my $result = $obj->get( comment_id => 456 );
217        is $result->request->method, 'GET', 'HTTP method';
218        is $result->request->uri->path, '/repos/foo/bar/pulls/comments/456', 'HTTP path';
219        my $http_request = $result->request;
220        is $http_request->content, '', 'HTTP body';
221    }
222}
223
224# Pithub::PullRequests::Comments->list
225{
226    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Comments', user => 'foo', repo => 'bar' );
227
228    isa_ok $obj, 'Pithub::PullRequests::Comments';
229
230    throws_ok { $obj->list } qr{Missing key in parameters: pull_request_id}, 'No parameters';
231
232    {
233        my $result = $obj->list( pull_request_id => 456 );
234        is $result->request->method, 'GET', 'HTTP method';
235        is $result->request->uri->path, '/repos/foo/bar/pulls/456/comments', 'HTTP path';
236        my $http_request = $result->request;
237        is $http_request->content, '', 'HTTP body';
238    }
239}
240
241# Pithub::PullRequests::Comments->update
242{
243    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Comments', user => 'foo', repo => 'bar' );
244
245    isa_ok $obj, 'Pithub::PullRequests::Comments';
246
247    throws_ok { $obj->update } qr{Missing key in parameters: comment_id}, 'No parameters';
248    throws_ok { $obj->update( comment_id => 123 ) } qr{Missing key in parameters: data \(hashref\)}, 'No data parameter';
249    throws_ok { $obj->update( comment_id => 123, data => 5 ) } qr{Missing key in parameters: data \(hashref\)}, 'Wrong type';
250    throws_ok {
251        $obj->update( comment_id => 123, data => { foo => 'bar' } );
252    }
253    qr{Access token required for: PATCH /repos/foo/bar/pulls/comments/123}, 'Token required';
254
255    ok $obj->token(123), 'Token set';
256
257    {
258        my $json = JSON->new;
259        my $result = $obj->update( comment_id => 123, data => { body => 'some comment' } );
260        is $result->request->method, 'PATCH', 'HTTP method';
261        is $result->request->uri->path, '/repos/foo/bar/pulls/comments/123', 'HTTP path';
262        my $http_request = $result->request;
263        eq_or_diff $json->decode( $http_request->content ), { 'body' => 'some comment' }, 'HTTP body';
264    }
265}
266
267# Pithub::PullRequests::Reviewers->delete
268{
269    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Reviewers', user => 'foo', repo => 'bar' );
270
271    isa_ok $obj, 'Pithub::PullRequests::Reviewers';
272
273    throws_ok { $obj->delete } qr{Missing key in parameters: pull_request_id}, 'No parameters';
274    throws_ok { $obj->delete( pull_request_id => 123 ); } qr{Access token required for: DELETE /repos/foo/bar/pulls/123/requested_reviewers}, 'Token required';
275
276    ok $obj->token(123), 'Token set';
277
278    {
279        my $result = $obj->delete( pull_request_id => 456 );
280        is $result->request->method, 'DELETE', 'HTTP method';
281        is $result->request->uri->path, '/repos/foo/bar/pulls/456/requested_reviewers', 'HTTP path';
282        my $http_request = $result->request;
283        is $http_request->content, '', 'HTTP body';
284    }
285}
286
287# Pithub::PullRequests::Reviewers->list
288{
289    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Reviewers', user => 'foo', repo => 'bar' );
290
291    isa_ok $obj, 'Pithub::PullRequests::Reviewers';
292
293    throws_ok { $obj->list } qr{Missing key in parameters: pull_request_id}, 'No parameters';
294
295    {
296        my $result = $obj->list( pull_request_id => 456 );
297        is $result->request->method, 'GET', 'HTTP method';
298        is $result->request->uri->path, '/repos/foo/bar/pulls/456/requested_reviewers', 'HTTP path';
299        my $http_request = $result->request;
300        is $http_request->content, '', 'HTTP body';
301    }
302}
303
304# Pithub::PullRequests::Reviewers->update
305{
306    my $obj = Pithub::Test::Factory->create( 'Pithub::PullRequests::Reviewers', user => 'foo', repo => 'bar' );
307
308    isa_ok $obj, 'Pithub::PullRequests::Reviewers';
309
310    throws_ok { $obj->update } qr{Missing key in parameters: pull_request_id}, 'No parameters';
311    throws_ok { $obj->update( pull_request_id => 123 ) } qr{Missing key in parameters: data \(hashref\)}, 'No data parameter';
312    throws_ok { $obj->update( pull_request_id => 123, data => 5 ) } qr{Missing key in parameters: data \(hashref\)}, 'Wrong type';
313    throws_ok {
314        $obj->update( pull_request_id => 123, data => { reviewers => ['bar'] } );
315    }
316    qr{Access token required for: POST /repos/foo/bar/pulls/123/requested_reviewers}, 'Token required';
317
318    ok $obj->token(123), 'Token set';
319
320    {
321        my $json = JSON->new;
322        my $result = $obj->update( pull_request_id => 123, data => { reviewers => ['baz'] } );
323        is $result->request->method, 'POST', 'HTTP method';
324        is $result->request->uri->path, '/repos/foo/bar/pulls/123/requested_reviewers', 'HTTP path';
325        my $http_request = $result->request;
326        eq_or_diff $json->decode( $http_request->content ), { 'reviewers' => ['baz'] }, 'HTTP body';
327    }
328}
329
330done_testing;
331