Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Create assertSentInOrder to test against the order of Http Requests using Strings or Closures #35525

Merged
merged 6 commits into from Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Http/Client/Factory.php
Expand Up @@ -262,6 +262,30 @@ public function assertSentCount($count)
PHPUnit::assertCount($count, $this->recorded);
}

/**
* Assert that requests were sent in the order specified.
*
* @param array $requestSequence
* @return void
*/
public function assertSentInOrder($requestSequence)
{
$this->assertSentCount(count($requestSequence));

foreach ($requestSequence as $orderPosition => $url) {

$callback = $url;
if(!is_callable($url)){
$callback = function($request) use ($url) {
return $request->url() == $url;
};
}

PHPUnit::assertTrue( $callback( $this->recorded[ $orderPosition ][0], $this->recorded[ $orderPosition ][1] ) );

}
}

/**
* Assert that every created response sequence is empty.
*
Expand Down
152 changes: 152 additions & 0 deletions tests/Http/HttpClientTest.php
Expand Up @@ -610,4 +610,156 @@ public function testSinkWhenStubbedByPath()

$this->assertSame(json_encode(['page' => 'foo']), stream_get_contents($resource));
}

public function testCanAssertAgainstOrderOfHttpRequestsWithUrlStrings()
{
$this->factory->fake();

$exampleUrls = [
'http://example.com/1',
'http://example.com/2',
'http://example.com/3',
];

foreach ($exampleUrls as $url) {
$this->factory->get($url);
}

$this->factory->assertSentInOrder($exampleUrls);
}

public function testAssertionsSentOutOfOrderThrowAssertionFailed()
{
$this->factory->fake();

$exampleUrls = [
'http://example.com/1',
'http://example.com/2',
'http://example.com/3',
];

$this->factory->get($exampleUrls[0]);
$this->factory->get($exampleUrls[2]);
$this->factory->get($exampleUrls[1]);

$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);

$this->factory->assertSentInOrder($exampleUrls);
}

public function testWrongNumberOfRequestsThrowAssertionFailed()
{
$this->factory->fake();

$exampleUrls = [
'http://example.com/1',
'http://example.com/2',
'http://example.com/3',
];

$this->factory->get($exampleUrls[0]);
$this->factory->get($exampleUrls[1]);

$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);

$this->factory->assertSentInOrder($exampleUrls);
}

public function testCanAssertAgainstOrderOfHttpRequestsWithCallables() {
$this->factory->fake();

$exampleUrls = [
function($request) {
return $request->url() == 'http://example.com/1';
},
function($request) {
return $request->url() == 'http://example.com/2';
},
function($request) {
return $request->url() == 'http://example.com/3';
},
];

$this->factory->get('http://example.com/1');
$this->factory->get('http://example.com/2');
$this->factory->get('http://example.com/3');

$this->factory->assertSentInOrder($exampleUrls);
}

public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeaders() {
$this->factory->fake();

$executionOrder = [
function (Request $request) {
return $request->url() === 'http://foo.com/json' &&
$request->hasHeader('Content-Type', 'application/json') &&
$request->hasHeader('X-Test-Header', 'foo') &&
$request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) &&
$request['name'] === 'Taylor';
},
function (Request $request) {
return $request->url() === 'http://bar.com/json' &&
$request->hasHeader('Content-Type', 'application/json') &&
$request->hasHeader('X-Test-Header', 'bar') &&
$request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) &&
$request['name'] === 'Taylor';
}
];

$this->factory->withHeaders([
'X-Test-Header' => 'foo',
'X-Test-ArrayHeader' => ['bar', 'baz'],
])->post('http://foo.com/json', [
'name' => 'Taylor',
]);

$this->factory->withHeaders([
'X-Test-Header' => 'bar',
'X-Test-ArrayHeader' => ['bar', 'baz'],
])->post('http://bar.com/json', [
'name' => 'Taylor',
]);

$this->factory->assertSentInOrder($executionOrder);
}

public function testCanAssertAgainstOrderOfHttpRequestsWithCallablesAndHeadersFailsCorrectly() {
$this->factory->fake();

$executionOrder = [
function (Request $request) {
return $request->url() === 'http://bar.com/json' &&
$request->hasHeader('Content-Type', 'application/json') &&
$request->hasHeader('X-Test-Header', 'bar') &&
$request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) &&
$request['name'] === 'Taylor';
},
function (Request $request) {
return $request->url() === 'http://foo.com/json' &&
$request->hasHeader('Content-Type', 'application/json') &&
$request->hasHeader('X-Test-Header', 'foo') &&
$request->hasHeader('X-Test-ArrayHeader', ['bar', 'baz']) &&
$request['name'] === 'Taylor';
},
];

$this->factory->withHeaders([
'X-Test-Header' => 'foo',
'X-Test-ArrayHeader' => ['bar', 'baz'],
])->post('http://foo.com/json', [
'name' => 'Taylor',
]);

$this->factory->withHeaders([
'X-Test-Header' => 'bar',
'X-Test-ArrayHeader' => ['bar', 'baz'],
])->post('http://bar.com/json', [
'name' => 'Taylor',
]);

$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);

$this->factory->assertSentInOrder($executionOrder);
}
}