compatibility with 2.6.0 and 3.1.0
[m6w6/seekat] / tests / api.php
1 <?php
2
3 use seekat\API;
4 use React\Promise\PromiseInterface;
5
6 describe("API", function() {
7
8 it("should return API on property access", function() {
9 expect($this->api->users)->to->be->instanceof(API::class);
10 });
11
12 it("should return a clone on property access", function() {
13 expect($this->api->users)->to->not->equal($this->api);
14 });
15
16 it("should return PromiseInterface on function call", function() {
17 expect($this->api->users->m6w6())->to->be->instanceof(PromiseInterface::class);
18 });
19
20 it("should successfully request /users/m6w6", function() {
21 $this->api->users->m6w6()->then(function($json) use(&$m6w6) {
22 $m6w6 = $json;
23 })->otherwise(function($error) use(&$errors) {
24 $errors[] = (string) $error;
25 });
26
27 $this->api->send();
28
29 expect($errors)->to->be->empty;
30 expect($m6w6->login)->to->loosely->equal("m6w6");
31 });
32
33 it("should return the count of followers when accessing /users/m6w6->followers", function() {
34 $this->api->users->m6w6()->then(function($m6w6) use(&$followers) {
35 $followers = $m6w6->followers;
36 })->otherwise(function($error) use(&$errors) {
37 $errors[] = (string) $error;
38 });
39
40 $this->api->send();
41
42 expect($errors)->to->be->empty();
43 expect($followers->export())->to->be->an("integer");
44 });
45
46 it("should fetch followers_url when accessing /users/m6w6->followers_url", function() {
47 $this->api->users->m6w6()->then(function($m6w6) use(&$followers, &$errors) {
48 $m6w6->followers_url()->then(function($json) use(&$followers) {
49 $followers = $json;
50 })->otherwise(function($error) use(&$errors) {
51 $errors[] = (string) $error;
52 });
53 })->otherwise(function($error) use(&$errors) {
54 $errors[] = (string) $error;
55 });
56
57 $this->api->send();
58
59 expect($errors)->to->be->empty;
60 expect($followers->export())->to->be->an("array");
61 expect(count($followers))->to->be->above(0);
62 });
63
64 it("should handle a few requests in parallel", function() {
65 $this->api->users->m6w6()->then(function($m6w6) use(&$count, &$errors) {
66 foreach ($m6w6 as $key => $val) {
67 switch ($key) {
68 case "html_url":
69 case "avatar_url":
70 break;
71 default:
72 if (substr($key, -4) === "_url") {
73 $val->get()->then(function() use(&$count) {
74 ++$count;
75 })->otherwise(function($error) use(&$errors) {
76 $errors[] = (string) $error;
77 });
78 }
79 }
80 }
81 })->otherwise(function($error) use(&$errors) {
82 $errors[] = (string) $error;
83 });
84
85 $this->api->send();
86
87 expect($errors)->to->be->empty;
88 expect($count)->to->be->above(2);
89 });
90 });