Added ability to generate new requests/responses on duplicate start()

pull/622/head
n0nag0n 1 week ago
parent 62bf542d12
commit 25250bf44e

@ -94,6 +94,9 @@ class Engine
/** If the framework has been initialized or not. */ /** If the framework has been initialized or not. */
protected bool $initialized = false; protected bool $initialized = false;
/** If the request has been handled or not. */
protected bool $requestHandled = false;
public function __construct() public function __construct()
{ {
$this->loader = new Loader(); $this->loader = new Loader();
@ -476,6 +479,18 @@ class Engine
{ {
$dispatched = false; $dispatched = false;
$self = $this; $self = $this;
// This behavior is specifically for test suites, and for async platforms like swoole, workerman, etc.
if ($this->requestHandled === false) {
// not doing much here, just setting the requestHandled flag to true
$this->requestHandled = true;
} else {
// deregister the request and response objects and re-register them with new instances
$this->unregister('request');
$this->unregister('response');
$this->register('request', Request::class);
$this->register('response', Response::class);
}
$request = $this->request(); $request = $this->request();
$response = $this->response(); $response = $this->response();
$router = $this->router(); $router = $this->router();

@ -211,8 +211,8 @@ class Request
$this->data->setData($data); $this->data->setData($data);
} }
} }
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data // Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
} else if (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) { } elseif (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
$body = $this->getBody(); $body = $this->getBody();
if ($body !== '') { if ($body !== '') {
$data = []; $data = [];

@ -174,6 +174,30 @@ class EngineTest extends TestCase
$engine->start(); $engine->start();
} }
public function testDoubleStart()
{
$engine = new Engine();
$engine->route('/someRoute', function () {
echo 'i ran';
}, true);
$engine->request()->url = '/someRoute';
$engine->start();
$request = $engine->request();
$response = $engine->response();
// This is pretending like this is embodied in a platform like swoole where
// another request comes in while still holding all the same state.
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/someRoute';
$engine->start();
$this->assertFalse($request === $engine->request());
$this->assertFalse($response === $engine->response());
$this->expectOutputString('i rani ran');
}
public function testStopWithCode() public function testStopWithCode()
{ {
$engine = new class extends Engine { $engine = new class extends Engine {

@ -378,7 +378,7 @@ class FlightTest extends TestCase
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{ {
$this->expectOutputString(<<<'html' $html = <<<'html'
<div>Hi</div> <div>Hi</div>
<div>Hi</div> <div>Hi</div>
@ -386,7 +386,12 @@ class FlightTest extends TestCase
<input type="number" /> <input type="number" />
html); html;
// if windows replace \n with \r\n
$html = str_replace("\n", PHP_EOL, $html);
$this->expectOutputString($html);
Flight::render('myComponent', ['prop' => 'Hi']); Flight::render('myComponent', ['prop' => 'Hi']);
Flight::render('myComponent'); Flight::render('myComponent');

@ -163,7 +163,7 @@ class RequestTest extends TestCase
'base' => '/vagrant/public', 'base' => '/vagrant/public',
'query' => new Collection(), 'query' => new Collection(),
'type' => '', 'type' => '',
'method' => 'GET' 'method' => 'GET'
]); ]);
$this->assertEquals('/flightphp', $request->url); $this->assertEquals('/flightphp', $request->url);
} }
@ -174,7 +174,7 @@ class RequestTest extends TestCase
'url' => '', 'url' => '',
'base' => '/vagrant/public', 'base' => '/vagrant/public',
'type' => '', 'type' => '',
'method' => 'GET' 'method' => 'GET'
]); ]);
$this->assertEquals('/', $request->url); $this->assertEquals('/', $request->url);
} }
@ -193,13 +193,13 @@ class RequestTest extends TestCase
'data' => new Collection(), 'data' => new Collection(),
'query' => new Collection(), 'query' => new Collection(),
'stream_path' => $stream_path, 'stream_path' => $stream_path,
'method' => 'POST' 'method' => 'POST'
]); ]);
$this->assertEquals([ 'foo' => 'bar' ], $request->data->getData()); $this->assertEquals([ 'foo' => 'bar' ], $request->data->getData());
$this->assertEquals('{"foo":"bar"}', $request->getBody()); $this->assertEquals('{"foo":"bar"}', $request->getBody());
} }
public function testInitWithFormBody() public function testInitWithFormBody()
{ {
// create dummy file to pull request body from // create dummy file to pull request body from
$tmpfile = tmpfile(); $tmpfile = tmpfile();
@ -213,12 +213,12 @@ class RequestTest extends TestCase
'data' => new Collection(), 'data' => new Collection(),
'query' => new Collection(), 'query' => new Collection(),
'stream_path' => $stream_path, 'stream_path' => $stream_path,
'method' => 'PATCH' 'method' => 'PATCH'
]); ]);
$this->assertEquals([ $this->assertEquals([
'foo' => 'bar', 'foo' => 'bar',
'baz' => 'qux' 'baz' => 'qux'
], $request->data->getData()); ], $request->data->getData());
$this->assertEquals('foo=bar&baz=qux', $request->getBody()); $this->assertEquals('foo=bar&baz=qux', $request->getBody());
} }

@ -175,7 +175,7 @@ class ViewTest extends TestCase
public function testKeepThePreviousStateOfOneViewComponentByDefault(): void public function testKeepThePreviousStateOfOneViewComponentByDefault(): void
{ {
$this->expectOutputString(<<<'html' $html = <<<'html'
<div>Hi</div> <div>Hi</div>
<div>Hi</div> <div>Hi</div>
@ -183,7 +183,12 @@ class ViewTest extends TestCase
<input type="number" /> <input type="number" />
html); html;
// if windows replace \n with \r\n
$html = str_replace("\n", PHP_EOL, $html);
$this->expectOutputString($html);
$this->view->render('myComponent', ['prop' => 'Hi']); $this->view->render('myComponent', ['prop' => 'Hi']);
$this->view->render('myComponent'); $this->view->render('myComponent');
@ -197,11 +202,16 @@ class ViewTest extends TestCase
$this->view->set('prop', 'bar'); $this->view->set('prop', 'bar');
$this->expectOutputString(<<<'html' $html = <<<'html'
<div>qux</div> <div>qux</div>
<div>bar</div> <div>bar</div>
html); html;
// if windows replace \n with \r\n
$html = str_replace("\n", PHP_EOL, $html);
$this->expectOutputString($html);
$this->view->render('myComponent', ['prop' => 'qux']); $this->view->render('myComponent', ['prop' => 'qux']);
$this->view->render('myComponent'); $this->view->render('myComponent');
@ -209,24 +219,29 @@ class ViewTest extends TestCase
public static function renderDataProvider(): array public static function renderDataProvider(): array
{ {
return [ $html1 = <<<'html'
[
<<<'html'
<div>Hi</div> <div>Hi</div>
<div></div> <div></div>
html, html;
['myComponent', ['prop' => 'Hi']], $html2 = <<<'html'
'/^Undefined variable:? \$?prop$/'
],
[
<<<'html'
<input type="number" /> <input type="number" />
<input type="text" /> <input type="text" />
html, html;
$html1 = str_replace("\n", PHP_EOL, $html1);
$html2 = str_replace("\n", PHP_EOL, $html2);
return [
[
$html1,
['myComponent', ['prop' => 'Hi']],
'/^Undefined variable:? \$?prop$/'
],
[
$html2,
['input', ['type' => 'number']], ['input', ['type' => 'number']],
'/^.*$/' '/^.*$/'
], ],

Loading…
Cancel
Save