Skip to content

Commit

Permalink
fix error with queue order (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
verdet23 committed Aug 15, 2022
1 parent 3721113 commit 33b5779
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Handler/MockHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function reset(): void

private function getSuitableResponse(RequestInterface $request): mixed
{
foreach (array_reverse($this->queue) as $key => $fixture) {
foreach ($this->queue as $key => $fixture) {
$queueRequest = $fixture[0];
if (
!$this->isSuitableMethod($queueRequest, $request)
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Exception/GuzzleMockExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Verdet\GuzzleMock\Exception\GuzzleMockException;

class GuzzleMockExceptionTest extends TestCase
Expand All @@ -20,7 +19,7 @@ public function testSuitableResponseNotFound(): void
'Accept-Encoding' => 'gzip',
'Content-Type' => 'application/json',
],
json_encode(['foo' => 'bar', 'query' => 8472]) ?: throw new RuntimeException()
json_encode(['foo' => 'bar', 'query' => 8472], JSON_THROW_ON_ERROR)
);

$exception = GuzzleMockException::suitableResponseNotFound($request);
Expand Down
48 changes: 47 additions & 1 deletion tests/Unit/Handler/MockHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testCanGetLastRequestAndOptions(): void

public function testSinkFilename(): void
{
$filename = \sys_get_temp_dir() . '/mock_test_' . \uniqid();
$filename = \sys_get_temp_dir() . '/mock_test_' . \uniqid('', true);
$response = new Response(200, [], 'TEST CONTENT');
$request = new Request('GET', '/');

Expand Down Expand Up @@ -341,4 +341,50 @@ public function testFindSuitableByContentType(): void

$this->assertSame($responseJson, $result);
}

public function testObtainCoupleResult(): void
{
$requestXML = new Request(
'GET',
'https://example.com/page',
['Accept-Charset' => 'utf-8', 'Accept' => 'application/xml']
);
$responseXML = new Response(
200,
['Accept' => 'application/xml'],
'<?xml version="1.0" encoding="UTF-8"?>'
);

$requestJson = new Request('GET', 'https://example.com/page', ['Accept' => 'application/json']);
$responseJson = new Response(
200,
['Content-Type' => 'application/json'],
'{}'
);

$requestHTML = new Request('GET', 'https://example.com/page', ['Accept' => 'text/html']);
$responseHTML = new Response(
200,
['Content-Type' => 'text/htnl'],
'<html></html>'
);

$mock = new MockHandler(
[
[$requestJson, $responseJson],
[$requestXML, $responseXML],
[$requestHTML, $responseHTML]
]
);


$result = $mock($requestHTML, [])->wait();
$this->assertSame($responseHTML, $result);

$result = $mock($requestXML, [])->wait();
$this->assertSame($responseXML, $result);

$result = $mock($requestJson, [])->wait();
$this->assertSame($responseJson, $result);
}
}

0 comments on commit 33b5779

Please sign in to comment.