Skip to content

Commit fe08478

Browse files
authored
[7.x] Minor changes to HTTP client tests (#31692)
* Add minor changes to HTTP client tests. * Fix StyleCI.
1 parent 5c27d3b commit fe08478

File tree

1 file changed

+61
-71
lines changed

1 file changed

+61
-71
lines changed

tests/Http/HttpClientTest.php

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,203 +4,193 @@
44

55
use Illuminate\Http\Client\Factory;
66
use Illuminate\Http\Client\PendingRequest;
7+
use Illuminate\Http\Client\Request;
78
use Illuminate\Support\Str;
89
use OutOfBoundsException;
910
use PHPUnit\Framework\TestCase;
1011

1112
class HttpClientTest extends TestCase
1213
{
14+
/**
15+
* @var \Illuminate\Http\Client\Factory
16+
*/
17+
protected $factory;
18+
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->factory = new Factory;
24+
}
25+
1326
public function testStubbedResponsesAreReturnedAfterFaking()
1427
{
15-
$factory = new Factory;
16-
$factory->fake();
28+
$this->factory->fake();
1729

18-
$response = $factory->post('http://laravel.com/test-missing-page');
30+
$response = $this->factory->post('http://laravel.com/test-missing-page');
1931

2032
$this->assertTrue($response->ok());
2133
}
2234

2335
public function testUrlsCanBeStubbedByPath()
2436
{
25-
$factory = new Factory;
26-
27-
$factory->fake([
37+
$this->factory->fake([
2838
'foo.com/*' => ['page' => 'foo'],
2939
'bar.com/*' => ['page' => 'bar'],
3040
'*' => ['page' => 'fallback'],
3141
]);
3242

33-
$fooResponse = $factory->post('http://foo.com/test');
34-
$barResponse = $factory->post('http://bar.com/test');
35-
$fallbackResponse = $factory->post('http://fallback.com/test');
43+
$fooResponse = $this->factory->post('http://foo.com/test');
44+
$barResponse = $this->factory->post('http://bar.com/test');
45+
$fallbackResponse = $this->factory->post('http://fallback.com/test');
3646

37-
$this->assertEquals('foo', $fooResponse['page']);
38-
$this->assertEquals('bar', $barResponse['page']);
39-
$this->assertEquals('fallback', $fallbackResponse['page']);
47+
$this->assertSame('foo', $fooResponse['page']);
48+
$this->assertSame('bar', $barResponse['page']);
49+
$this->assertSame('fallback', $fallbackResponse['page']);
4050

41-
$factory->assertSent(function ($request) {
51+
$this->factory->assertSent(function (Request $request) {
4252
return $request->url() === 'http://foo.com/test' &&
4353
$request->hasHeader('Content-Type', 'application/json');
4454
});
4555
}
4656

4757
public function testCanSendJsonData()
4858
{
49-
$factory = new Factory;
50-
51-
$factory->fake();
59+
$this->factory->fake();
5260

53-
$fooResponse = $factory->withHeaders([
61+
$this->factory->withHeaders([
5462
'X-Test-Header' => 'foo',
5563
])->post('http://foo.com/json', [
5664
'name' => 'Taylor',
5765
]);
5866

59-
$factory->assertSent(function ($request) {
67+
$this->factory->assertSent(function (Request $request) {
6068
return $request->url() === 'http://foo.com/json' &&
6169
$request->hasHeader('Content-Type', 'application/json') &&
6270
$request->hasHeader('X-Test-Header', 'foo') &&
63-
$request['name'] == 'Taylor';
71+
$request['name'] === 'Taylor';
6472
});
6573
}
6674

6775
public function testCanSendFormData()
6876
{
69-
$factory = new Factory;
77+
$this->factory->fake();
7078

71-
$factory->fake();
72-
73-
$fooResponse = $factory->asForm()->post('http://foo.com/form', [
79+
$this->factory->asForm()->post('http://foo.com/form', [
7480
'name' => 'Taylor',
7581
'title' => 'Laravel Developer',
7682
]);
7783

78-
$factory->assertSent(function ($request) {
84+
$this->factory->assertSent(function (Request $request) {
7985
return $request->url() === 'http://foo.com/form' &&
8086
$request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') &&
81-
$request['name'] == 'Taylor';
87+
$request['name'] === 'Taylor';
8288
});
8389
}
8490

8591
public function testCanSendMultipartData()
8692
{
87-
$factory = new Factory;
88-
89-
$factory->fake();
93+
$this->factory->fake();
9094

91-
$fooResponse = $factory->asMultipart()->post('http://foo.com/multipart', [
95+
$this->factory->asMultipart()->post('http://foo.com/multipart', [
9296
[
9397
'name' => 'foo',
9498
'contents' => 'data',
9599
'headers' => ['X-Test-Header' => 'foo'],
96100
],
97101
]);
98102

99-
$factory->assertSent(function ($request) {
103+
$this->factory->assertSent(function (Request $request) {
100104
return $request->url() === 'http://foo.com/multipart' &&
101105
Str::startsWith($request->header('Content-Type')[0], 'multipart') &&
102-
$request[0]['name'] == 'foo';
106+
$request[0]['name'] === 'foo';
103107
});
104108
}
105109

106110
public function testFilesCanBeAttached()
107111
{
108-
$factory = new Factory;
112+
$this->factory->fake();
109113

110-
$factory->fake();
114+
$this->factory->attach('foo', 'data', 'file.txt', ['X-Test-Header' => 'foo'])
115+
->post('http://foo.com/file');
111116

112-
$fooResponse = $factory
113-
->attach('foo', 'data', 'file.txt', ['X-Test-Header' => 'foo'])
114-
->post('http://foo.com/file');
115-
116-
$factory->assertSent(function ($request) {
117+
$this->factory->assertSent(function (Request $request) {
117118
return $request->url() === 'http://foo.com/file' &&
118119
Str::startsWith($request->header('Content-Type')[0], 'multipart') &&
119-
$request[0]['name'] == 'foo' &&
120+
$request[0]['name'] === 'foo' &&
120121
$request->hasFile('foo', 'data', 'file.txt');
121122
});
122123
}
123124

124125
public function testSequenceBuilder()
125126
{
126-
$factory = new Factory;
127-
128-
$factory->fake([
129-
'*' => $factory->sequence()
127+
$this->factory->fake([
128+
'*' => $this->factory->sequence()
130129
->push('Ok', 201)
131130
->push(['fact' => 'Cats are great!'])
132131
->pushFile(__DIR__.'/fixtures/test.txt')
133132
->pushStatus(403),
134133
]);
135134

136-
/** @var PendingRequest $factory */
137-
$response = $factory->get('https://example.com');
135+
/** @var PendingRequest */
136+
$response = $this->factory->get('https://example.com');
138137
$this->assertSame('Ok', $response->body());
139138
$this->assertSame(201, $response->status());
140139

141-
$response = $factory->get('https://example.com');
140+
$response = $this->factory->get('https://example.com');
142141
$this->assertSame(['fact' => 'Cats are great!'], $response->json());
143142
$this->assertSame(200, $response->status());
144143

145-
$response = $factory->get('https://example.com');
146-
$this->assertSame("This is a story about something that happened long ago when your grandfather was a child.\n", $response->body());
144+
$response = $this->factory->get('https://example.com');
145+
$this->assertSame('This is a story about something that happened long ago when your grandfather was a child.'.PHP_EOL, $response->body());
147146
$this->assertSame(200, $response->status());
148147

149-
$response = $factory->get('https://example.com');
148+
$response = $this->factory->get('https://example.com');
150149
$this->assertSame('', $response->body());
151150
$this->assertSame(403, $response->status());
152151

153152
$this->expectException(OutOfBoundsException::class);
154153

155154
// The sequence is empty, it should throw an exception.
156-
$factory->get('https://example.com');
155+
$this->factory->get('https://example.com');
157156
}
158157

159158
public function testSequenceBuilderCanKeepGoingWhenEmpty()
160159
{
161-
$factory = new Factory;
162-
163-
$factory->fake([
164-
'*' => $factory->sequence()
160+
$this->factory->fake([
161+
'*' => $this->factory->sequence()
165162
->dontFailWhenEmpty()
166163
->push('Ok'),
167164
]);
168165

169-
/** @var PendingRequest $factory */
170-
$response = $factory->get('https://laravel.com');
166+
$response = $this->factory->get('https://laravel.com');
171167
$this->assertSame('Ok', $response->body());
172168

173169
// The sequence is empty, but it should not fail.
174-
$factory->get('https://laravel.com');
170+
$this->factory->get('https://laravel.com');
175171
}
176172

177173
public function testAssertSequencesAreEmpty()
178174
{
179-
$factory = new Factory;
180-
181-
$factory->fake([
182-
'*' => $factory->sequence()
175+
$this->factory->fake([
176+
'*' => $this->factory->sequence()
183177
->push('1')
184178
->push('2'),
185179
]);
186180

187-
/** @var PendingRequest $factory */
188-
$factory->get('https://example.com');
189-
$factory->get('https://example.com');
181+
$this->factory->get('https://example.com');
182+
$this->factory->get('https://example.com');
190183

191-
$factory->assertSequencesAreEmpty();
184+
$this->factory->assertSequencesAreEmpty();
192185
}
193186

194187
public function testFakeSequence()
195188
{
196-
$factory = new Factory;
197-
198-
$factory->fakeSequence()
189+
$this->factory->fakeSequence()
199190
->pushStatus(201)
200191
->pushStatus(301);
201192

202-
/** @var PendingRequest $factory */
203-
$this->assertSame(201, $factory->get('https://example.com')->status());
204-
$this->assertSame(301, $factory->get('https://example.com')->status());
193+
$this->assertSame(201, $this->factory->get('https://example.com')->status());
194+
$this->assertSame(301, $this->factory->get('https://example.com')->status());
205195
}
206196
}

0 commit comments

Comments
 (0)