Skip to content

Commit

Permalink
Add ability to assert against callables
Browse files Browse the repository at this point in the history
  • Loading branch information
anubisthejackle committed Dec 7, 2020
1 parent 99735fc commit af6ba0a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Illuminate/Http/Client/Factory.php
Expand Up @@ -273,7 +273,16 @@ public function assertSentInOrder($requestSequence)
$this->assertSentCount(count($requestSequence));

foreach ($requestSequence as $orderPosition => $url) {
PHPUnit::assertEquals($url, $this->recorded[$orderPosition][0]->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] ) );

}
}

Expand Down
100 changes: 99 additions & 1 deletion tests/Http/HttpClientTest.php
Expand Up @@ -611,7 +611,7 @@ public function testSinkWhenStubbedByPath()
$this->assertSame(json_encode(['page' => 'foo']), stream_get_contents($resource));
}

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

Expand Down Expand Up @@ -664,4 +664,102 @@ public function testWrongNumberOfRequestsThrowAssertionFailed()

$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);
}
}

0 comments on commit af6ba0a

Please sign in to comment.