Skip to content

Instantly share code, notes, and snippets.

@kapooostin
Forked from Djuki/LaravelHelper.php
Last active August 29, 2015 14:11
Show Gist options
  • Save kapooostin/5e3a8a9a83656a07d173 to your computer and use it in GitHub Desktop.
Save kapooostin/5e3a8a9a83656a07d173 to your computer and use it in GitHub Desktop.
<?php
namespace Codeception\Module;
use Codeception\Util\Framework;
use Codeception\TestCase;
use Codeception\Util\Connector\Universal;
use Codeception\Configuration;
use Symfony\Component\BrowserKit\Response;
use Laravel\Routing\Router;
use Laravel\Section;
use HMVC, URI;
class LaravelHelper extends Framework
{
protected $requiredFields = array(
'environment',
);
public function _initialize()
{
$this->setPaths();
}
// HOOK: before test
public function _before(\Codeception\TestCase $test)
{
$this->client = new LaravelConnector();
$this->client->setEnvironment($this->config['environment']);
$this->client->setIndex(Configuration::projectDir().'content/index.php');
}
public function setPaths()
{
$project_root = Configuration::projectDir().'platform/';
require_once $project_root.'paths.php';
set_path('app', $project_root.'application'.DS);
set_path('bundle', $project_root.'bundles'.DS);
set_path('storage', $project_root.'storage'.DS);
}
}
class LaravelConnector extends Universal {
/**
* Check is this first time request is made
* If this is first time we need to load Framework
* @var bool
*/
public static $is_first_call = true;
/**
* Laravel environment
* @var
*/
private $environment = '';
/**
* @param mixed $environment
*/
public function setEnvironment($environment)
{
$this->environment = $environment;
}
/**
* Request action is made each time Codeception clik on link,
* or change page with $I->amOnPage('/contact');
*
* @param object $request
* @return Response
*/
public function doRequest($request)
{
$_COOKIE = $request->getCookies();
$_SERVER = $request->getServer();
$_FILES = $request->getFiles();
$uri = strtolower(str_replace('http://localhost','',$request->getUri()));
$uri = str_replace('http://yoursite.com/', '', $uri);
if (strtoupper($request->getMethod()) == 'GET') {
$_GET = $request->getParameters();
} else {
$_POST = $request->getParameters();
}
$_REQUEST = $request->getParameters();
$_SERVER['REQUEST_METHOD'] = $request->getMethod();
$_SERVER['REQUEST_URI'] = $uri;
if (static::$is_first_call)
{
$this->loadEnvironment();
ob_start();
include_once $this->index;
ob_get_clean();
static::$is_first_call = false;
}
$this->resetSectionsGlobally();
$content = $this->callHmvc($_SERVER['REQUEST_METHOD'], $uri);
$this->resetSectionsGlobally();
$headers['Content-type'] = isset($headers['Content-type']) ? $headers['Content-type']: "text/html; charset=UTF-8";
$response = new Response($content, 200, $headers);
return $response;
}
/**
* Make Laravel HMVC call which returns Request content
*
* @param $method
* @param $uri
* @return string
*/
private function callHmvc($method, $uri)
{
URI::$uri = $uri;
$_SERVER['REQUEST_URI'] = $uri;
$route = Router::route($method, $uri);
$request = $route->call();
return $request->render();
}
/**
* We reset Section static properties because they can mess response content
*/
private function resetSectionsGlobally()
{
Section::$sections = array();
Section::$last = array();
}
/**
* Load environment args and constants used by Laravel
*/
private function loadEnvironment() {
if ( ! defined('DS'))
{
define('DS', DIRECTORY_SEPARATOR);
}
// Set more server variables if needed
$_SERVER['argv'] = array('--env='.$this->environment);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment