Skip to content

Commit 518be6e

Browse files
committed
Refactor tests to avoid dynamic properties
1 parent 13bb152 commit 518be6e

File tree

2 files changed

+96
-83
lines changed

2 files changed

+96
-83
lines changed

src/Providers/TwilioProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Illuminate\Support\ServiceProvider;
1717
use Twilio\Http\Client as TwilioHttpClient;
1818
use Twilio\Http\CurlClient;
19-
use Twilio\Http\GuzzleClient;
2019

2120
final class TwilioProvider extends ServiceProvider implements DeferrableProvider
2221
{
@@ -67,7 +66,7 @@ private function registerHttpClient(): void
6766
$this->app->bind(
6867
TwilioHttpClient::class,
6968
static function (Application $app): TwilioHttpClient {
70-
// If Guzzle is installed, then we will either use Laravel's native client
69+
// If Guzzle is installed, then we will use Laravel's native client
7170
if (class_exists(Guzzle::class)) {
7271
return new LaravelHttpClient($app->make(Factory::class));
7372
}

tests/TwilioClientTest.php

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,136 @@
22

33
namespace BabDev\Twilio\Tests;
44

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;
811
use Twilio\Rest\Api\V2010\Account\CallInstance;
9-
use Twilio\Rest\Api\V2010\Account\CallList;
1012
use Twilio\Rest\Api\V2010\Account\MessageInstance;
11-
use Twilio\Rest\Api\V2010\Account\MessageList;
1213
use Twilio\Rest\Client;
1314

1415
final class TwilioClientTest extends TestCase
1516
{
16-
public function testTheSdkInstanceCanBeRetrieved(): void
17+
protected function getEnvironmentSetUp($app): void
1718
{
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+
}
1929

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+
}
2239

23-
$this->assertSame($twilio, (new TwilioClient($twilio, $defaultFrom))->twilio());
40+
public function testTheSdkInstanceCanBeRetrieved(): void
41+
{
42+
$this->assertInstanceOf(Client::class, TwilioClient::twilio());
2443
}
2544

2645
public function testACallCanBeCreated(): void
2746
{
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';
4048

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+
]);
4455

45-
$this->assertInstanceOf(CallInstance::class, (new TwilioClient($twilio, $defaultFrom))->call($to, $params));
56+
$this->assertInstanceOf(CallInstance::class, TwilioClient::call($to));
4657
}
4758

4859
public function testACallCanBeCreatedWithACustomFromNumber(): void
4960
{
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';
6363

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+
]);
6770

68-
$this->assertInstanceOf(CallInstance::class, (new TwilioClient($twilio, $defaultFrom))->call($to, array_merge($params, ['from' => $customFrom])));
71+
$this->assertInstanceOf(CallInstance::class, TwilioClient::call($to));
6972
}
7073

7174
public function testAMessageCanBeSent(): void
7275
{
7376
$to = '+15558675309';
74-
$defaultFrom = '+19418675309';
7577
$message = 'Test Message';
7678

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));
9587
}
9688

9789
public function testAMessageCanBeSentWithACustomFromNumber(): void
9890
{
9991
$to = '+15558675309';
100-
$defaultFrom = '+19418675309';
10192
$customFrom = '+16518675309';
10293
$message = 'Test Message';
10394

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+
];
122136
}
123137
}

0 commit comments

Comments
 (0)