Skip to content

Commit

Permalink
Merge pull request #57 from donatj/test/DelayedResponse
Browse files Browse the repository at this point in the history
Improve test coverage of DelayedResponse
  • Loading branch information
donatj authored Sep 7, 2023
2 parents 0153ba7 + 510618d commit 0e22d8d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/DelayedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@ class DelayedResponse implements InitializingResponseInterface, MultiResponseInt
protected $delay;
/** @var \donatj\MockWebServer\ResponseInterface */
protected $response;
/** @var callable */
protected $usleep;

/**
* @param int $delay Microseconds to delay the response
*/
public function __construct(
ResponseInterface $response,
int $delay
int $delay,
?callable $usleep = null
) {
$this->response = $response;
$this->delay = $delay;

$this->usleep = '\\usleep';
if( $usleep ) {
$this->usleep = $usleep;
}
}

public function getRef() : string {
return md5('delayed.' . $this->response->getRef());
}

public function initialize( RequestInfo $request ) : void {
usleep($this->delay);
($this->usleep)($this->delay);
}

public function getBody( RequestInfo $request ) : string {
Expand Down
70 changes: 70 additions & 0 deletions test/DelayedResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Test;

use donatj\MockWebServer\DelayedResponse;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\Responses\DefaultResponse;
use donatj\MockWebServer\ResponseStack;
use PHPUnit\Framework\TestCase;

class DelayedResponseTest extends TestCase {

public function testInitialize() : void {
$foundDelay = null;
$resp = new DelayedResponse(new DefaultResponse, 1234, function ( int $delay ) use ( &$foundDelay ) {
$foundDelay = $delay;
});

$requestInfo = $this->getMockBuilder(RequestInfo::class)
->disableOriginalConstructor()
->getMock();

$resp->initialize($requestInfo);

$this->assertSame(1234, $foundDelay);
}

public function testNext() : void {
$resp = new DelayedResponse(new DefaultResponse, 1234);
$this->assertFalse($resp->next());

$resp = new DelayedResponse(new DelayedResponse(new DefaultResponse, 1234), 1234);
$this->assertFalse($resp->next());

$resp = new DelayedResponse(new ResponseStack(
new Response('foo'),
new Response('bar'),
new Response('baz')
), 1234);

$req = $this->getMockBuilder(RequestInfo::class)
->disableOriginalConstructor()
->getMock();

$this->assertSame('foo', $resp->getBody($req));
$this->assertTrue($resp->next());
$this->assertSame('bar', $resp->getBody($req));
$this->assertTrue($resp->next());
$this->assertSame('baz', $resp->getBody($req));
$this->assertFalse($resp->next());
}

public function testGetRef() : void {
$resp1 = new DelayedResponse(new DefaultResponse, 1234);
$this->assertNotFalse(
preg_match('/^[a-f0-9]{32}$/', $resp1->getRef()),
'Ref must be a 32 character hex string'
);

$resp2 = new DelayedResponse(new Response('foo'), 1234);
$this->assertNotFalse(
preg_match('/^[a-f0-9]{32}$/', $resp2->getRef()),
'Ref is a 32 character hex string'
);

$this->assertNotSame($resp1->getRef(), $resp2->getRef(), 'Ref is unique per response');
}

}

0 comments on commit 0e22d8d

Please sign in to comment.