Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 17, 2025. It is now read-only.
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,17 @@ $server->listen();
At this time, you can optionally provide a callback to `listen()`; this will be passed to the handler as the third argument (`$done`):

```php
$server->listen(function ($error = null) {
$server->listen(function ($request, $response, $error = null) {
if (! $error) {
return;
}
// do something with the error...
});
```

Typically, the `listen` callback will be an error handler, and can expect to receive the error as its argument.
Typically, the `listen` callback will be an error handler, and can expect to
receive the request, response, and error as its arguments (though the error may
be null).

API
---
Expand Down
43 changes: 41 additions & 2 deletions test/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public function setUp()
$this->callback = function ($req, $res, $done) {
// Intentionally empty
};
$this->request = $this->getMock('Psr\Http\Message\ServerRequestInterface');
$this->response = $this->getMock('Psr\Http\Message\ResponseInterface');
$this->request = $this
->getMockBuilder('Psr\Http\Message\ServerRequestInterface')
->getMock();
$this->response = $this
->getMockBuilder('Psr\Http\Message\ResponseInterface')
->getMock();
}

public function tearDown()
Expand Down Expand Up @@ -211,4 +215,39 @@ public function testHeaderOrderIsHonoredWhenEmitted($stack)
$header
);
}

public function testListenPassesCallableArgumentToCallback()
{
$phpunit = $this;
$invoked = false;
$request = $this->request;
$response = $this->response;

$this->response
->expects($this->once())
->method('getHeaders')
->will($this->returnValue([]));

$final = function ($req, $res, $err = null) use ($phpunit, $request, $response, &$invoked) {
$phpunit->assertSame($request, $req);
$phpunit->assertSame($response, $res);
$invoked = true;
};

$callback = function ($req, $res, callable $final = null) use ($phpunit) {
if (! $final) {
$phpunit->fail('No final callable passed!');
}

$final($req, $res);
};

$server = Server::createServerFromRequest(
$callback,
$this->request,
$this->response
);
$server->listen($final);
$this->assertTrue($invoked);
}
}