From d9aa80f22ea929cdcdf55e82ee0fd8cbdee4376d Mon Sep 17 00:00:00 2001 From: Jon Acker Date: Sun, 12 Mar 2017 21:50:31 +0000 Subject: [PATCH 1/6] Process GET parameters and refactor LegacyApp --- .gitignore | 3 +- composer.json | 5 ++- features/bootstrap/FeatureContext.php | 14 +++++++ features/bootstrap/SessionBuilder.php | 7 ++-- features/passing_parameters.feature | 34 +++++++++++++++++ spec/LegacyApp/ControllersSpec.php | 17 +++++++++ spec/LegacyApp/LegacyAppSpec.php | 43 +++++++++++++++++++++ spec/LegacyApp/ScriptSpec.php | 13 +++++++ src/Client.php | 2 +- src/LegacyApp/Controllers.php | 54 +++++++++++++++++++++++++++ src/LegacyApp/LegacyApp.php | 50 +++++++------------------ src/LegacyApp/LegacyAppBuilder.php | 15 ++++---- src/LegacyApp/Script.php | 32 ++++++++++++++++ 13 files changed, 239 insertions(+), 50 deletions(-) create mode 100644 features/passing_parameters.feature create mode 100644 spec/LegacyApp/ControllersSpec.php create mode 100644 spec/LegacyApp/LegacyAppSpec.php create mode 100644 spec/LegacyApp/ScriptSpec.php create mode 100644 src/LegacyApp/Controllers.php create mode 100644 src/LegacyApp/Script.php diff --git a/.gitignore b/.gitignore index 3a9875b..1c451b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -/vendor/ +vendor/ composer.lock +bin/ diff --git a/composer.json b/composer.json index d7ae5af..16096ea 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,10 @@ "require-dev": { "symfony/filesystem": "^2.8", "symfony/yaml": "^2.8", - "phpspec/phpspec": "^2.5" + "phpspec/phpspec": "^3.2" + }, + "config": { + "bin-dir": "bin" }, "extra": { "branch-alias": { diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 6fa1a55..babe06d 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -1,5 +1,6 @@ sessionBuilder->addBootstrapScript($this->directory . '/' . $file); } + + /** + * @Then the page content should include the error :errorMessage + */ + public function thePageContentShouldIncludeTheError($errorMessage) + { + $pageContent = $this->session->getPage()->getContent(); + + if (strpos($pageContent, $errorMessage) === false) { + throw new RuntimeException(sprintf("Expected page contain error with: %s, page content was:\n%s", $errorMessage, $pageContent)); + } + } } diff --git a/features/bootstrap/SessionBuilder.php b/features/bootstrap/SessionBuilder.php index f85b0cb..3edd4cd 100644 --- a/features/bootstrap/SessionBuilder.php +++ b/features/bootstrap/SessionBuilder.php @@ -3,6 +3,7 @@ use Behat\Mink\Driver\BrowserKitDriver; use Behat\Mink\Session; use carlosV2\LegacyDriver\Client; +use carlosV2\LegacyDriver\LegacyApp\Controllers; use carlosV2\LegacyDriver\LegacyApp\LegacyAppBuilder; use carlosV2\LegacyDriver\Serializer; use Symfony\Component\Routing\Route; @@ -16,7 +17,7 @@ class SessionBuilder private $documentRoot; /** - * @var RouteCollection + * @var Controllers */ private $controllers; @@ -36,7 +37,7 @@ class SessionBuilder public function __construct($documentRoot) { $this->documentRoot = $documentRoot; - $this->controllers = new RouteCollection(); + $this->controllers = new Controllers(new RouteCollection()); $this->environment = array(); $this->bootstrapScripts = array(); } @@ -46,7 +47,7 @@ public function __construct($documentRoot) */ public function addController(Route $route) { - $this->controllers->add(md5(serialize($route)), $route); + $this->controllers->add($route); } /** diff --git a/features/passing_parameters.feature b/features/passing_parameters.feature new file mode 100644 index 0000000..56afa1b --- /dev/null +++ b/features/passing_parameters.feature @@ -0,0 +1,34 @@ +Feature: Passing parameters + In order to emulate ... + As a developer + I need to be able to pass GET and POST parameters to the application + + Background: + Given the file "index.php" is configured as the unique frontend controller + + Scenario: Error is generated when trying to render input when no parameters passed + Given the "index.php" file contains: + """ + beConstructedWith($routeCollection); + } +} diff --git a/spec/LegacyApp/LegacyAppSpec.php b/spec/LegacyApp/LegacyAppSpec.php new file mode 100644 index 0000000..1478cab --- /dev/null +++ b/spec/LegacyApp/LegacyAppSpec.php @@ -0,0 +1,43 @@ +__toString()->willReturn('script.php'); + $documentRoot = './'; + $environmentVariables = []; + $bootstrapScripts = []; + $this->beConstructedWith($documentRoot, $controllers, $environmentVariables, $bootstrapScripts); + } + + function it_loads_all_bootstrap_scripts(Script $bootstrapScript, Controllers $controllers) + { + $request = new Request('http://localhost/', 'GET'); + $controllers->getFront($request)->willReturn($bootstrapScript); + + $bootstrapScript->load()->shouldBeCalled(); + + $this->handle($request); + + } + + function xit_sets_up_get_parameters_from_query_string() + { + $request = new Request('http://localhost/', 'GET'); + $this->handle($request); + } +} diff --git a/spec/LegacyApp/ScriptSpec.php b/spec/LegacyApp/ScriptSpec.php new file mode 100644 index 0000000..d6324d9 --- /dev/null +++ b/spec/LegacyApp/ScriptSpec.php @@ -0,0 +1,13 @@ +beConstructedWith(['bootstrap.php']); + } +} diff --git a/src/Client.php b/src/Client.php index afda8b9..3d5abdf 100644 --- a/src/Client.php +++ b/src/Client.php @@ -41,7 +41,7 @@ public function __construct(LegacyAppBuilder $legacyAppBuilder, Serializer $seri /** * {@inheritdoc} */ - protected function doRequest($request) + protected function doRequest(Request $request) { $process = new Process($this->composeCommand($request)); $process->setInput($request->getContent()); diff --git a/src/LegacyApp/Controllers.php b/src/LegacyApp/Controllers.php new file mode 100644 index 0000000..4773f24 --- /dev/null +++ b/src/LegacyApp/Controllers.php @@ -0,0 +1,54 @@ +routeCollection = $routeCollection; + } + + /** + * @param Request $request + * + * @return Script + */ + public function getFront(Request $request) + { + $parts = parse_url($request->getUri()); + + $matcher = new UrlMatcher( + $this->routeCollection, + new RequestContext( + '/', + strtoupper($request->getMethod()) + ) + ); + + $parameters = $matcher->match($parts['path']); + + return new Script($parameters['file']); + } + + public function add(Route $route) + { + $this->routeCollection->add(md5(serialize($route)), $route); + } +} diff --git a/src/LegacyApp/LegacyApp.php b/src/LegacyApp/LegacyApp.php index 680566c..fb37cb7 100644 --- a/src/LegacyApp/LegacyApp.php +++ b/src/LegacyApp/LegacyApp.php @@ -3,8 +3,6 @@ namespace carlosV2\LegacyDriver\LegacyApp; use Symfony\Component\BrowserKit\Request; -use Symfony\Component\Routing\Matcher\UrlMatcher; -use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; final class LegacyApp @@ -30,14 +28,14 @@ final class LegacyApp private $bootstrapScripts; /** - * @param string $documentRoot - * @param RouteCollection $controllers - * @param string[] $environmentVariables - * @param string[] $bootstrapScripts + * @param string $documentRoot + * @param Controllers $controllers + * @param string[] $environmentVariables + * @param string[] $bootstrapScripts */ public function __construct( $documentRoot, - RouteCollection $controllers, + Controllers $controllers, array $environmentVariables, array $bootstrapScripts ) { @@ -52,32 +50,11 @@ public function __construct( */ public function handle(Request $request) { - $controller = $this->getFrontendControllerScript($request); - - chdir(dirname($controller)); $this->setVariables($request); - $this->bootstrapScripts[] = $controller; - $this->bootstrapApp(); - } + $this->bootstrapScripts[] = $this->controllers->getFront($request); - /** - * @param Request $request - * - * @return string - */ - private function getFrontendControllerScript(Request $request) - { - $parts = parse_url($request->getUri()); - $matcher = new UrlMatcher( - $this->controllers, - new RequestContext( - '/', - strtoupper($request->getMethod()) - ) - ); - $parameters = $matcher->match($parts['path']); - return $parameters['file']; + $this->bootstrapApp(); } /** @@ -135,7 +112,10 @@ private function setEnvironmentVariables() private function setGetVariables(Request $request) { if (strtoupper($request->getMethod()) === 'GET') { - $_GET = $request->getParameters(); + $parts = parse_url($request->getUri()); + if (isset($parts['query'])) { + parse_str($parts['query'], $_GET); + } } $_REQUEST = array_merge($_REQUEST, $_GET); @@ -170,7 +150,7 @@ private function setServerVariables(Request $request) { $_SERVER = $request->getServer(); $_SERVER['DOCUMENT_ROOT'] = $this->documentRoot . '/'; - $_SERVER['SCRIPT_FILENAME'] = $this->getFrontendControllerScript($request); + $_SERVER['SCRIPT_FILENAME'] = $this->controllers->getFront($request); $_SERVER['SCRIPT_NAME'] = str_replace($this->documentRoot, '', $_SERVER['SCRIPT_FILENAME']); $parts = parse_url($request->getUri()); @@ -183,15 +163,13 @@ private function setServerVariables(Request $request) $_SERVER['QUERY_STRING'] = $parts['query']; } - if ($_SERVER['HTTP_HOST'] === 'https') { + if ($_SERVER['REQUEST_SCHEME'] === 'https') { $_SERVER['HTTPS'] = 'on'; } } private function bootstrapApp() { - foreach ($this->bootstrapScripts as $bootstrapScript) { - require_once $bootstrapScript; - } + array_map(function(Script $script) { $script->load(); }, $this->bootstrapScripts); } } diff --git a/src/LegacyApp/LegacyAppBuilder.php b/src/LegacyApp/LegacyAppBuilder.php index d3195ed..3b684da 100644 --- a/src/LegacyApp/LegacyAppBuilder.php +++ b/src/LegacyApp/LegacyAppBuilder.php @@ -2,8 +2,6 @@ namespace carlosV2\LegacyDriver\LegacyApp; -use Symfony\Component\Routing\RouteCollection; - final class LegacyAppBuilder { /** @@ -12,7 +10,7 @@ final class LegacyAppBuilder private $documentRoot; /** - * @var RouteCollection + * @var Controllers */ private $controllers; @@ -27,15 +25,14 @@ final class LegacyAppBuilder private $bootstrapScripts; /** - * @param string $documentRoot - * @param RouteCollection $controllers + * @param string $documentRoot + * @param Controllers $controllers */ - public function __construct($documentRoot, RouteCollection $controllers) + public function __construct($documentRoot, Controllers $controllers) { $this->documentRoot = $documentRoot; $this->controllers = $controllers; $this->environmentVariables = array(); - $this->bootstrapScripts = array(); } /** @@ -51,7 +48,9 @@ public function addEnvironmentVariables(array $environmentVariables) */ public function addBootstrapScripts(array $bootstrapScripts) { - $this->bootstrapScripts = $bootstrapScripts; + $this->bootstrapScripts = array_map(function($script) { + return new Script($script); + }, $bootstrapScripts); } /** diff --git a/src/LegacyApp/Script.php b/src/LegacyApp/Script.php new file mode 100644 index 0000000..9615ef1 --- /dev/null +++ b/src/LegacyApp/Script.php @@ -0,0 +1,32 @@ +script = $script; + } + + /** + */ + public function load() + { + chdir(dirname($this->script)); + require_once $this->script; + } + + public function __toString() + { + return $this->script; + } +} From 69bb11ce9d840602464289de84e0b6432dfbed2f Mon Sep 17 00:00:00 2001 From: Jon Acker Date: Sun, 12 Mar 2017 22:05:36 +0000 Subject: [PATCH 2/6] Remove unused --- spec/LegacyApp/ControllersSpec.php | 3 --- src/LegacyApp/Controllers.php | 2 -- 2 files changed, 5 deletions(-) diff --git a/spec/LegacyApp/ControllersSpec.php b/spec/LegacyApp/ControllersSpec.php index 6612bf2..ba50868 100644 --- a/spec/LegacyApp/ControllersSpec.php +++ b/spec/LegacyApp/ControllersSpec.php @@ -2,16 +2,13 @@ namespace spec\carlosV2\LegacyDriver\LegacyApp; -use carlosV2\LegacyDriver\LegacyApp\Controllers; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; use Symfony\Component\Routing\RouteCollection; class ControllersSpec extends ObjectBehavior { function it_is_constructed_with_routes(RouteCollection $routeCollection) { - $this->beConstructedWith($routeCollection); } } diff --git a/src/LegacyApp/Controllers.php b/src/LegacyApp/Controllers.php index 4773f24..e56b4e8 100644 --- a/src/LegacyApp/Controllers.php +++ b/src/LegacyApp/Controllers.php @@ -15,8 +15,6 @@ class Controllers */ private $routeCollection; - private $controllers = []; - /** * @param RouteCollection $routeCollection */ From e571fe54d9632341da05e4ed5e8b17c9657b857d Mon Sep 17 00:00:00 2001 From: Jon Acker Date: Sun, 12 Mar 2017 22:23:42 +0000 Subject: [PATCH 3/6] Spec adding parameters from GET --- composer.json | 3 ++- spec/LegacyApp/LegacyAppSpec.php | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 16096ea..0be337a 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "require-dev": { "symfony/filesystem": "^2.8", "symfony/yaml": "^2.8", - "phpspec/phpspec": "^3.2" + "phpspec/phpspec": "^3.2", + "phpunit/phpunit": "5" }, "config": { "bin-dir": "bin" diff --git a/spec/LegacyApp/LegacyAppSpec.php b/spec/LegacyApp/LegacyAppSpec.php index 1478cab..7d275fa 100644 --- a/spec/LegacyApp/LegacyAppSpec.php +++ b/spec/LegacyApp/LegacyAppSpec.php @@ -6,9 +6,8 @@ use carlosV2\LegacyDriver\LegacyApp\LegacyApp; use carlosV2\LegacyDriver\LegacyApp\Script; use PhpSpec\ObjectBehavior; +use PHPUnit_Framework_Assert; use Symfony\Component\BrowserKit\Request; -use Symfony\Component\Routing\Route; -use Symfony\Component\Routing\RouteCollection; /** * @mixin LegacyApp @@ -20,7 +19,10 @@ function let(Script $bootstrapScript, Controllers $controllers) $bootstrapScript->__toString()->willReturn('script.php'); $documentRoot = './'; $environmentVariables = []; - $bootstrapScripts = []; + $bootstrapScripts = [$bootstrapScript]; + + $bootstrapScript->load()->willReturn(); + $this->beConstructedWith($documentRoot, $controllers, $environmentVariables, $bootstrapScripts); } @@ -29,15 +31,18 @@ function it_loads_all_bootstrap_scripts(Script $bootstrapScript, Controllers $co $request = new Request('http://localhost/', 'GET'); $controllers->getFront($request)->willReturn($bootstrapScript); - $bootstrapScript->load()->shouldBeCalled(); + $bootstrapScript->load()->shouldBeCalledTimes(2); $this->handle($request); - } - function xit_sets_up_get_parameters_from_query_string() + function it_sets_up_request_parameters_from_query_string_on_GET(Script $bootstrapScript, Controllers $controllers) { - $request = new Request('http://localhost/', 'GET'); + $request = new Request('http://localhost/?name=jon', 'GET'); + $controllers->getFront($request)->willReturn($bootstrapScript); + $this->handle($request); + + PHPUnit_Framework_Assert::assertEquals($_REQUEST['name'], 'jon'); } } From 76b8b5e2953ab5f8ab88654161790d0d94a3c1ca Mon Sep 17 00:00:00 2001 From: Jon Acker Date: Sun, 12 Mar 2017 22:29:55 +0000 Subject: [PATCH 4/6] bin/phpspec --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4bcf7ea..6d1e0fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,5 +8,5 @@ before_script: - composer install --prefer-source script: - - ./vendor/bin/phpspec run --format=pretty - - ./vendor/bin/behat -fprogress --strict + - ./bin/phpspec run --format=pretty + - ./bin/behat -fprogress --strict From 47133421191816afc7df23a4521fe11f4d87d4c2 Mon Sep 17 00:00:00 2001 From: Jon Acker Date: Sun, 12 Mar 2017 22:45:48 +0000 Subject: [PATCH 5/6] Fix type hinting --- src/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 3d5abdf..520711e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -41,7 +41,7 @@ public function __construct(LegacyAppBuilder $legacyAppBuilder, Serializer $seri /** * {@inheritdoc} */ - protected function doRequest(Request $request) + protected function doRequest($request) { $process = new Process($this->composeCommand($request)); $process->setInput($request->getContent()); @@ -57,11 +57,11 @@ protected function doRequest(Request $request) /** * Compose the command to run with the same PHP binary that was used to run Behat * - * @param Request $request + * @param object $request * * @return string */ - private function composeCommand(Request $request) + private function composeCommand($request) { return sprintf( '%s %s %s %s %s', From f083f7b33879015e346046398f7e59e9e6f9dc37 Mon Sep 17 00:00:00 2001 From: Jon Acker Date: Sun, 12 Mar 2017 22:49:58 +0000 Subject: [PATCH 6/6] Downgrade phpspec and phpunit versions --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0be337a..8bd1cd6 100644 --- a/composer.json +++ b/composer.json @@ -33,8 +33,8 @@ "require-dev": { "symfony/filesystem": "^2.8", "symfony/yaml": "^2.8", - "phpspec/phpspec": "^3.2", - "phpunit/phpunit": "5" + "phpspec/phpspec": "^2.5", + "phpunit/phpunit": "^4.0" }, "config": { "bin-dir": "bin"