|
2 | 2 |
|
3 | 3 | namespace BabDev\Twilio\Tests;
|
4 | 4 |
|
5 |
| -use BabDev\Twilio\TwilioClient; |
6 |
| -use PHPUnit\Framework\MockObject\MockObject; |
7 |
| -use PHPUnit\Framework\TestCase; |
| 5 | +use BabDev\Twilio\Facades\TwilioClient; |
| 6 | +use BabDev\Twilio\Providers\TwilioProvider; |
| 7 | +use Illuminate\Support\Facades\Date; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | +use Illuminate\Support\ServiceProvider; |
| 10 | +use Orchestra\Testbench\TestCase; |
8 | 11 | use Twilio\Rest\Api\V2010\Account\CallInstance;
|
9 |
| -use Twilio\Rest\Api\V2010\Account\CallList; |
10 | 12 | use Twilio\Rest\Api\V2010\Account\MessageInstance;
|
11 |
| -use Twilio\Rest\Api\V2010\Account\MessageList; |
12 | 13 | use Twilio\Rest\Client;
|
13 | 14 |
|
14 | 15 | final class TwilioClientTest extends TestCase
|
15 | 16 | {
|
16 |
| - public function testTheSdkInstanceCanBeRetrieved(): void |
| 17 | + protected function getEnvironmentSetUp($app): void |
17 | 18 | {
|
18 |
| - $defaultFrom = '+19418675309'; |
| 19 | + // Setup connections configuration |
| 20 | + $app['config']->set( |
| 21 | + 'twilio.connections.twilio', |
| 22 | + [ |
| 23 | + 'sid' => 'account-sid', |
| 24 | + 'token' => 'api_token', |
| 25 | + 'from' => '+15558675309', |
| 26 | + ] |
| 27 | + ); |
| 28 | + } |
19 | 29 |
|
20 |
| - /** @var MockObject&Client $twilio */ |
21 |
| - $twilio = $this->createMock(Client::class); |
| 30 | + /** |
| 31 | + * @return class-string<ServiceProvider> |
| 32 | + */ |
| 33 | + protected function getPackageProviders($app): array |
| 34 | + { |
| 35 | + return [ |
| 36 | + TwilioProvider::class, |
| 37 | + ]; |
| 38 | + } |
22 | 39 |
|
23 |
| - $this->assertSame($twilio, (new TwilioClient($twilio, $defaultFrom))->twilio()); |
| 40 | + public function testTheSdkInstanceCanBeRetrieved(): void |
| 41 | + { |
| 42 | + $this->assertInstanceOf(Client::class, TwilioClient::twilio()); |
24 | 43 | }
|
25 | 44 |
|
26 | 45 | public function testACallCanBeCreated(): void
|
27 | 46 | {
|
28 |
| - $to = '+15558675309'; |
29 |
| - $defaultFrom = '+19418675309'; |
30 |
| - $params = [ |
31 |
| - 'url' => 'https://www.babdev.com', |
32 |
| - ]; |
33 |
| - |
34 |
| - /** @var MockObject&CallList $calls */ |
35 |
| - $calls = $this->createMock(CallList::class); |
36 |
| - $calls->expects($this->once()) |
37 |
| - ->method('create') |
38 |
| - ->with($to, $defaultFrom, $params) |
39 |
| - ->willReturn($this->createMock(CallInstance::class)); |
| 47 | + $to = '+15558675309'; |
40 | 48 |
|
41 |
| - /** @var MockObject&Client $twilio */ |
42 |
| - $twilio = $this->createMock(Client::class); |
43 |
| - $twilio->calls = $calls; |
| 49 | + Http::fake([ |
| 50 | + 'https://api.twilio.com/2010-04-01/Accounts/account-sid/Calls.json' => Http::response( |
| 51 | + $this->getMessageSentResponseContent(config('twilio.connections.twilio.from'), $to), |
| 52 | + 201, |
| 53 | + ), |
| 54 | + ]); |
44 | 55 |
|
45 |
| - $this->assertInstanceOf(CallInstance::class, (new TwilioClient($twilio, $defaultFrom))->call($to, $params)); |
| 56 | + $this->assertInstanceOf(CallInstance::class, TwilioClient::call($to)); |
46 | 57 | }
|
47 | 58 |
|
48 | 59 | public function testACallCanBeCreatedWithACustomFromNumber(): void
|
49 | 60 | {
|
50 |
| - $to = '+15558675309'; |
51 |
| - $defaultFrom = '+19418675309'; |
52 |
| - $customFrom = '+16518675309'; |
53 |
| - $params = [ |
54 |
| - 'url' => 'https://www.babdev.com', |
55 |
| - ]; |
56 |
| - |
57 |
| - /** @var MockObject&CallList $calls */ |
58 |
| - $calls = $this->createMock(CallList::class); |
59 |
| - $calls->expects($this->once()) |
60 |
| - ->method('create') |
61 |
| - ->with($to, $customFrom, $params) |
62 |
| - ->willReturn($this->createMock(CallInstance::class)); |
| 61 | + $to = '+15558675309'; |
| 62 | + $customFrom = '+16518675309'; |
63 | 63 |
|
64 |
| - /** @var MockObject&Client $twilio */ |
65 |
| - $twilio = $this->createMock(Client::class); |
66 |
| - $twilio->calls = $calls; |
| 64 | + Http::fake([ |
| 65 | + 'https://api.twilio.com/2010-04-01/Accounts/account-sid/Calls.json' => Http::response( |
| 66 | + $this->getMessageSentResponseContent($customFrom, $to), |
| 67 | + 201, |
| 68 | + ), |
| 69 | + ]); |
67 | 70 |
|
68 |
| - $this->assertInstanceOf(CallInstance::class, (new TwilioClient($twilio, $defaultFrom))->call($to, array_merge($params, ['from' => $customFrom]))); |
| 71 | + $this->assertInstanceOf(CallInstance::class, TwilioClient::call($to)); |
69 | 72 | }
|
70 | 73 |
|
71 | 74 | public function testAMessageCanBeSent(): void
|
72 | 75 | {
|
73 | 76 | $to = '+15558675309';
|
74 |
| - $defaultFrom = '+19418675309'; |
75 | 77 | $message = 'Test Message';
|
76 | 78 |
|
77 |
| - /** @var MockObject&MessageList $messages */ |
78 |
| - $messages = $this->createMock(MessageList::class); |
79 |
| - $messages->expects($this->once()) |
80 |
| - ->method('create') |
81 |
| - ->with( |
82 |
| - $to, |
83 |
| - [ |
84 |
| - 'body' => $message, |
85 |
| - 'from' => $defaultFrom, |
86 |
| - ] |
87 |
| - ) |
88 |
| - ->willReturn($this->createMock(MessageInstance::class)); |
89 |
| - |
90 |
| - /** @var MockObject&Client $twilio */ |
91 |
| - $twilio = $this->createMock(Client::class); |
92 |
| - $twilio->messages = $messages; |
93 |
| - |
94 |
| - $this->assertInstanceOf(MessageInstance::class, (new TwilioClient($twilio, $defaultFrom))->message($to, $message)); |
| 79 | + Http::fake([ |
| 80 | + 'https://api.twilio.com/2010-04-01/Accounts/account-sid/Messages.json' => Http::response( |
| 81 | + $this->getMessageSentResponseContent(config('twilio.connections.twilio.from'), $to), |
| 82 | + 201, |
| 83 | + ), |
| 84 | + ]); |
| 85 | + |
| 86 | + $this->assertInstanceOf(MessageInstance::class, TwilioClient::message($to, $message)); |
95 | 87 | }
|
96 | 88 |
|
97 | 89 | public function testAMessageCanBeSentWithACustomFromNumber(): void
|
98 | 90 | {
|
99 | 91 | $to = '+15558675309';
|
100 |
| - $defaultFrom = '+19418675309'; |
101 | 92 | $customFrom = '+16518675309';
|
102 | 93 | $message = 'Test Message';
|
103 | 94 |
|
104 |
| - /** @var MockObject&MessageList $messages */ |
105 |
| - $messages = $this->createMock(MessageList::class); |
106 |
| - $messages->expects($this->once()) |
107 |
| - ->method('create') |
108 |
| - ->with( |
109 |
| - $to, |
110 |
| - [ |
111 |
| - 'body' => $message, |
112 |
| - 'from' => $customFrom, |
113 |
| - ] |
114 |
| - ) |
115 |
| - ->willReturn($this->createMock(MessageInstance::class)); |
116 |
| - |
117 |
| - /** @var MockObject&Client $twilio */ |
118 |
| - $twilio = $this->createMock(Client::class); |
119 |
| - $twilio->messages = $messages; |
120 |
| - |
121 |
| - $this->assertInstanceOf(MessageInstance::class, (new TwilioClient($twilio, $defaultFrom))->message($to, $message, ['from' => $customFrom])); |
| 95 | + Http::fake([ |
| 96 | + 'https://api.twilio.com/2010-04-01/Accounts/account-sid/Messages.json' => Http::response( |
| 97 | + $this->getMessageSentResponseContent($customFrom, $to), |
| 98 | + 201, |
| 99 | + ), |
| 100 | + ]); |
| 101 | + |
| 102 | + $this->assertInstanceOf(MessageInstance::class, TwilioClient::message($to, $message, ['from' => $customFrom])); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<string, mixed> |
| 107 | + */ |
| 108 | + private function getMessageSentResponseContent(string $from, string $to): array |
| 109 | + { |
| 110 | + $date = Date::now()->toRfc822String(); |
| 111 | + |
| 112 | + return [ |
| 113 | + 'body' => 'Test', |
| 114 | + 'num_segments' => '1', |
| 115 | + 'direction' => 'outbound-api', |
| 116 | + 'from' => $from, |
| 117 | + 'date_updated' => $date, |
| 118 | + 'price' => null, |
| 119 | + 'error_message' => null, |
| 120 | + 'uri' => '/2010-04-01/Accounts/account-sid/Messages/message-sid.json', |
| 121 | + 'account_sid' => 'account-sid', |
| 122 | + 'num_media' => '0', |
| 123 | + 'to' => $to, |
| 124 | + 'date_created' => $date, |
| 125 | + 'status' => 'queued', |
| 126 | + 'sid' => 'message-sid', |
| 127 | + 'date_sent' => null, |
| 128 | + 'messaging_service_sid' => null, |
| 129 | + 'error_code' => null, |
| 130 | + 'price_unit' => 'USD', |
| 131 | + 'api_version' => '2010-04-01', |
| 132 | + 'subresource_uris' => [ |
| 133 | + 'media' => '/2010-04-01/Accounts/account-sid/Messages/message-sid/Media.json', |
| 134 | + ], |
| 135 | + ]; |
122 | 136 | }
|
123 | 137 | }
|
0 commit comments