Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process GET parameters and decouple requiring #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
vendor/
composer.lock
bin/
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
"require-dev": {
"symfony/filesystem": "^2.8",
"symfony/yaml": "^2.8",
"phpspec/phpspec": "^2.5"
"phpspec/phpspec": "^2.5",
"phpunit/phpunit": "^4.0"
},
"config": {
"bin-dir": "bin"
},
"extra": {
"branch-alias": {
Expand Down
14 changes: 14 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Mink\Session;
Expand Down Expand Up @@ -120,6 +121,7 @@ private function addController($file, $path, $method = 'GET', $parameter = null,

/**
* @When /^I make a(?: "([^"]*)")? request to "([^"]*)"$/
* @When /^I make a(?: "([^"]*)")? request to "([^"]*)" with no parameters$/
*/
public function iMakeARequestTo($method, $path)
{
Expand Down Expand Up @@ -160,4 +162,16 @@ public function theFileIsConfiguredToBootstrapWithTheApplication($file)
{
$this->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));
}
}
}
7 changes: 4 additions & 3 deletions features/bootstrap/SessionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,7 @@ class SessionBuilder
private $documentRoot;

/**
* @var RouteCollection
* @var Controllers
*/
private $controllers;

Expand All @@ -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();
}
Expand All @@ -46,7 +47,7 @@ public function __construct($documentRoot)
*/
public function addController(Route $route)
{
$this->controllers->add(md5(serialize($route)), $route);
$this->controllers->add($route);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions features/passing_parameters.feature
Original file line number Diff line number Diff line change
@@ -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:
"""
<?php
echo "My name is: {$_GET['name']}";
"""
When I make a request to "/item" with no parameters
Then the page content should include the error "Undefined index: name"

Scenario: Error is generated when trying to render input with incorrect parameter passed
Given the "index.php" file contains:
"""
<?php
echo "My name is: {$_GET['name']}";
"""
When I make a request to "/item?nae=jon"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this route should be "/", and same above

Then the page content should include the error "Undefined index: name"

Scenario: Correctly rendering input when correct parameter passed
Given the "index.php" file contains:
"""
<?php
echo "My name is: {$_GET['name']}";
"""
When I make a request to "/?name=jon"
Then the page content should be "My name is: jon"
14 changes: 14 additions & 0 deletions spec/LegacyApp/ControllersSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace spec\carlosV2\LegacyDriver\LegacyApp;

use PhpSpec\ObjectBehavior;
use Symfony\Component\Routing\RouteCollection;

class ControllersSpec extends ObjectBehavior
{
function it_is_constructed_with_routes(RouteCollection $routeCollection)
{
$this->beConstructedWith($routeCollection);
}
}
48 changes: 48 additions & 0 deletions spec/LegacyApp/LegacyAppSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace spec\carlosV2\LegacyDriver\LegacyApp;

use carlosV2\LegacyDriver\LegacyApp\Controllers;
use carlosV2\LegacyDriver\LegacyApp\LegacyApp;
use carlosV2\LegacyDriver\LegacyApp\Script;
use PhpSpec\ObjectBehavior;
use PHPUnit_Framework_Assert;
use Symfony\Component\BrowserKit\Request;

/**
* @mixin LegacyApp
*/
class LegacyAppSpec extends ObjectBehavior
{
function let(Script $bootstrapScript, Controllers $controllers)
{
$bootstrapScript->__toString()->willReturn('script.php');
$documentRoot = './';
$environmentVariables = [];
$bootstrapScripts = [$bootstrapScript];

$bootstrapScript->load()->willReturn();

$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()->shouldBeCalledTimes(2);

$this->handle($request);
}

function it_sets_up_request_parameters_from_query_string_on_GET(Script $bootstrapScript, Controllers $controllers)
{
$request = new Request('http://localhost/?name=jon', 'GET');
$controllers->getFront($request)->willReturn($bootstrapScript);

$this->handle($request);

PHPUnit_Framework_Assert::assertEquals($_REQUEST['name'], 'jon');
}
}
13 changes: 13 additions & 0 deletions spec/LegacyApp/ScriptSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace spec\carlosV2\LegacyDriver\LegacyApp;

use PhpSpec\ObjectBehavior;

class ScriptSpec extends ObjectBehavior
{
public function let()
{
$this->beConstructedWith(['bootstrap.php']);
}
}
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ protected function doRequest($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',
Expand Down
52 changes: 52 additions & 0 deletions src/LegacyApp/Controllers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace carlosV2\LegacyDriver\LegacyApp;

use Symfony\Component\BrowserKit\Request;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class Controllers
{
/**
* @var RouteCollection
*/
private $routeCollection;

/**
* @param RouteCollection $routeCollection
*/
public function __construct(RouteCollection $routeCollection)
{
$this->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);
}
}
50 changes: 14 additions & 36 deletions src/LegacyApp/LegacyApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
) {
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];

Expand All @@ -184,15 +164,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);
}
}
Loading