Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a very very rough prototype of how PSR7 request dispatching could be integrated into into the TestSuite. There is still a lot TODO, but a very basic GET request is somewhat functional right now.
- Loading branch information
Showing
4 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @since 3.3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\TestSuite; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Event\EventManager; | ||
use Cake\Http\ResponseTransformer; | ||
use Cake\Http\Server; | ||
use Cake\Http\ServerRequestFactory; | ||
use LogicException; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
|
||
/** | ||
* Dispatches a request capturing the response for integration | ||
* testing purposes into the Cake\Http stack. | ||
* | ||
* @internal | ||
*/ | ||
class MiddlewareDispatcher | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param \Cake\TestSuite\IntegrationTestCase $test The test case to run. | ||
*/ | ||
public function __construct($test) | ||
{ | ||
$this->_test = $test; | ||
} | ||
|
||
/** | ||
* Run a request and get the response. | ||
* | ||
* @param \Cake\Network\Request $request The request to execute. | ||
* @return \Cake\Network\Response The generated response. | ||
*/ | ||
public function execute($request) | ||
{ | ||
try { | ||
$namespace = Configure::read('App.namespace'); | ||
$reflect = new ReflectionClass($namespace . '\Application'); | ||
$app = $reflect->newInstance('./config'); | ||
} catch (ReflectionException $e) { | ||
throw new LogicException(sprintf( | ||
'Cannot load "%s" for use in integration testing.', | ||
$class | ||
)); | ||
} | ||
|
||
// Spy on the controller using the initialize hook instead | ||
// of the dispatcher hooks as those will be going away one day. | ||
EventManager::instance()->on( | ||
'Controller.initialize', | ||
[$this->_test, 'controllerSpy'] | ||
); | ||
|
||
$server = new Server($app); | ||
|
||
// TODO How to handle passing all headers. | ||
// The Request doesn't expose a way to read all headers values. | ||
// TODO How to pass session data? PSR7 requests don't handle sessions.. | ||
// TODO pass php://input stream, base, webroot | ||
$serverData = [ | ||
'REQUEST_URI' => $request->here, | ||
'REQUEST_METHOD' => $request->method(), | ||
]; | ||
$psrRequest = ServerRequestFactory::fromGlobals( | ||
array_merge($_SERVER, $serverData), | ||
$request->query, | ||
$request->data(), | ||
$request->cookies | ||
); | ||
$response = $server->run($psrRequest); | ||
return ResponseTransformer::toCake($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since 3.3.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace TestApp; | ||
|
||
use Cake\Http\BaseApplication; | ||
use Cake\Routing\Middleware\RoutingMiddleware; | ||
|
||
class Application extends BaseApplication | ||
{ | ||
/** | ||
* Bootstrap hook. | ||
* | ||
* Nerfed as this is for IntegrationTestCase testing. | ||
* | ||
* @return void | ||
*/ | ||
public function bootstrap() | ||
{ | ||
// Do nothing. | ||
} | ||
|
||
public function middleware($middleware) | ||
{ | ||
$middleware->push(new RoutingMiddleware()); | ||
return $middleware; | ||
} | ||
} |