Skip to content

Commit

Permalink
Adds test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Manderson committed Sep 24, 2014
1 parent e85f772 commit 379d6a4
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ composer.phar
vendor/
composer.lock
build/
phpunit.xml

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require_once __DIR__.'/../vendor/autoload.php';
// Use standard HttpFoundation
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
// Create a silex application
$app = new Silex\Application();
Expand All @@ -43,9 +44,9 @@ $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __
->register(new Silex\Provider\MonologServiceProvider(), array('monolog.logfile' => __DIR__.'/../app/logs/development.log'));
// Add in phruts to organise your controllers
$app->register(new \Phruts\PhrutsServiceProvider(), array(
$app->register(new Phruts\Provider\PhrutsServiceProvider(), array(
// Register our modules and configs
\Phruts\Util\Globals::ACTION_KERNEL_CONFIG => array(
Phruts\Util\Globals::ACTION_KERNEL_CONFIG => array(
'config' => '../app/config/web-config.xml', // Supports multiple modules/configurations
)
));
Expand All @@ -58,7 +59,7 @@ $app->get('{path}', function($path) use ($app) {
// Add routes to be matched by Phruts
$app->get('{path}', function (Request $request) use ($app) {
return $app[\Phruts\Util\Globals::ACTION_KERNEL]->handle($request, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST, false);
return $app[\Phruts\Util\Globals::ACTION_KERNEL]->handle($request, HttpKernelInterface::SUB_REQUEST, false);
})
->assert('path', '.*')
->value('path', '/'); // Set the welcome path
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"require-dev": {
"phpunit/phpunit": "~4.1",
"satooshi/php-coveralls": "dev-master",
"mikey179/vfsStream" : "1.*"
"mikey179/vfsStream" : "1.*",
"symfony/browser-kit": ">=2.3,<2.4-dev",
"symfony/css-selector": ">=2.3,<2.4-dev"
},
"suggest": {
},
Expand Down
14 changes: 7 additions & 7 deletions src/Action/ActionKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Phruts\Action;

use Phruts\Util\PropertyMessageResources;
use Phruts\Util\RequestUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand All @@ -18,11 +19,6 @@ class ActionKernel implements HttpKernelInterface
*/
protected $application;

/**
* @var \Phruts\Action\RequestProcessor
*/
protected $processor;

/**
* @var \Psr\Log\LoggerInterface
*/
Expand Down Expand Up @@ -99,6 +95,7 @@ protected function handleException(\Exception $e, Request $request, $type)
protected function process(Request $request, Response $response)
{
$this->init();
RequestUtils::selectModule($request, $this->application);
$this->getRequestProcessor($this->getModuleConfig($request))->process($request, $response);
}

Expand All @@ -121,8 +118,11 @@ protected function init()

// Get the configured modules
foreach ($this->application[\Phruts\Util\Globals::ACTION_KERNEL_CONFIG] as $prefixParam => $config) {
if(strlen($prefixParam) > 7 && substr($prefixParam, 0, 7) == 'config/') continue;
$prefix = substr($prefixParam, 7);
// Only read in the config params
if(strlen($prefixParam) < 6 || substr($prefixParam, 0, 6) != 'config') continue;

// Strip the config element
$prefix = preg_replace('#config/?#', '', $prefixParam);

// Get the module config
$moduleConfig = $moduleConfigProvider->getModuleConfig($prefix, $config);
Expand Down
19 changes: 10 additions & 9 deletions src/Action/RequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,22 @@ protected function processPath(\Symfony\Component\HttpFoundation\Request $reques
if ($path == null) {
$path = $request->getPathInfo();
}
if (($path != null) && (strlen($path) > 0)) {
return ($path);
}

// For extension matching, strip the module prefix and extension
$prefix = $this->moduleConfig->getPrefix();
if (substr($path, 0, strlen($prefix)) != $prefix) {
$msg = $this->getInternal()->getMessage("processPath", $request->getRequestURI());
if (!empty($this->log)) {
$this->log->error($msg);
if (!empty($prefix)) {
if (!preg_match('#^/?' . $prefix . '/.+#', $path)) {
$msg = $this->getInternal()->getMessage("processPath", $request->getRequestURI());
if (!empty($this->log)) {
$this->log->error($msg);
}
throw new BadRequestHttpException($msg);
}
throw new BadRequestHttpException($msg);
// Strip module
$path = preg_replace('#^/?' . $prefix . '#', '', $path);
}

return ($path);
return $path;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Phruts;
namespace Phruts\Provider;

use Phigester\Digester;
use Phruts\Config\ConfigRuleSet;
Expand Down
3 changes: 3 additions & 0 deletions src/Util/RequestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static function getModuleName(\Symfony\Component\HttpFoundation\Request $

$path = $request->getPathInfo();

// Clear the first forward slash /module/path
if(substr($path, 0, 1) == '/') $path = substr($path, 1);

$prefixes = $application[\Phruts\Util\Globals::PREFIXES_KEY];
if (is_null($prefixes)) {
$prefix = '';
Expand Down
2 changes: 1 addition & 1 deletion tests/ActionTest/ActionKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace ActionTest;

use Phruts\Action\ActionKernel;
use Phruts\PhrutsServiceProvider;
use Phruts\Provider\PhrutsServiceProvider;
use Phruts\Util\ModuleProvider\FileCacheModuleProvider;
use Phruts\Util\RequestUtils;
use Silex\Application;
Expand Down
86 changes: 83 additions & 3 deletions tests/BehavioursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,43 @@
* Created by Cam MANDERSON <cameronmanderson@gmail.com>
*/

class BehavioursTest extends PHPUnit_Framework_TestCase
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
use Phruts\Util\ModuleProvider\FileCacheModuleProvider;

class BehavioursTest extends \Silex\WebTestCase
{

public function testConfigPaths()
{
$client = $this->createClient();

// Welcome path
$client->request('GET', '');
$this->assertTrue($client->getResponse()->isOk());

$client->request('GET', '/resourceA');
$this->assertTrue($client->getResponse()->isOk());

$client->request('GET', '/resourceB');
$this->assertTrue($client->getResponse()->isOk());

$client->request('GET', '/resourceC');
$this->assertTrue($client->getResponse()->isOk());

$client->request('GET', '/module/resourceA');
$this->assertTrue($client->getResponse()->isOk());

$client->request('GET', '/module/resourceD');
$this->assertTrue($client->getResponse()->isOk());

$client->request('GET', '/module/resourceE');
$this->assertTrue($client->getResponse()->isOk());
}

public function testGlobalForwards()
{

Expand Down Expand Up @@ -51,7 +86,7 @@ public function testDataSource()

}

public function testActionRoles()
public function testRoles()
{

}
Expand All @@ -71,8 +106,53 @@ public function testCustomActionConfig()
// Test that properties can be set from the action config
}

public function testTwigExtensions()
{

}


public function createApplication()
{
// Create a silex application
$app = new Silex\Application();

// Configure
$app['debug'] = true;
$app['exception_handler']->disable();
$app['session.test'] = true;

// Add in phruts to organise your controllers
$app->register(new Phruts\Provider\PhrutsServiceProvider(), array(
// Register our modules and configs
Phruts\Util\Globals::ACTION_KERNEL_CONFIG => array(
'config' => __DIR__ . '/Resources/module1-config.xml',
'config/module' => __DIR__ . '/Resources/module2-config.xml'
)
));

// Setup the mock file system for caching
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('cacheDir'));
$app[Phruts\Util\Globals::MODULE_CONFIG_PROVIDER] = $app->share(function () use ($app) {
$provider = new FileCacheModuleProvider($app);
$provider->setCachePath(vfsStream::url('cacheDir'));
return $provider;
});

// Add a relevant html
$app->get('{path}', function($path) use ($app) {
return new \Symfony\Component\HttpFoundation\Response(file_get_contents(__DIR__ . '/Resources/' . $path));
})
->assert('path', '.+\.html');

// Add routes to be matched by Phruts
$app->get('{path}', function (Request $request) use ($app) {
return $app[Phruts\Util\Globals::ACTION_KERNEL]->handle($request, HttpKernelInterface::SUB_REQUEST, false);
})
->assert('path', '.*')
->value('path', '/'); // Set the welcome path

return $app;
}
}

2 changes: 1 addition & 1 deletion tests/PhrutsServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function testInstantiate()
{
$application = new \Silex\Application();

$service = new \Phruts\PhrutsServiceProvider();
$service = new \Phruts\Provider\PhrutsServiceProvider();
$service->register($application);
$service->boot($application);
$this->assertNotEmpty($application[\Phruts\Util\Globals::ACTION_KERNEL]);
Expand Down
17 changes: 6 additions & 11 deletions tests/Resources/example-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
</data-sources>

<form-beans>
<form-bean name="bean1" type="\\Example\\FormBeanOne"/>
<form-bean name="bean2" type="\\Example\\FormBeanTwo"/>
<form-bean name="bean1" type="\Example\FormBeanOne"/>
<form-bean name="bean2" type="\Example\FormBeanTwo"/>
</form-beans>

<global-exceptions>
<exception type="\\Example\\SomeException"
<exception type="\Example\SomeException"
key="some.exception.key"
path="error.html.twig"/>
</global-exceptions>
Expand Down Expand Up @@ -51,11 +51,6 @@
<controller>
<set-property property="noCache" value="true"/>
</controller>
<!--<plug-in className="\\Example\\MyExamplePlugin">-->
<!--<set-property-->
<!--property="config"-->
<!--value="somewhere.yaml"/>-->
<!--</plug-in>-->

<message-resources parameter="\\Action\\MessageResources"/>
</phruts-config>

<message-resources parameter="\Action\MessageResources"/>
</phruts-config>
24 changes: 24 additions & 0 deletions tests/Resources/module1-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phruts-config>

<global-forwards>
<forward name="resource" path="/resource.html" />
<forward name="welcome" path="/welcome.html" />
</global-forwards>

<action-mappings>
<action path="/"
type="\Phruts\Actions\ForwardAction"
parameter="welcome"/>
<action path="/resourceA"
type="\Phruts\Actions\ForwardAction"
parameter="resource"/>
<action path="/resourceB"
type="\Phruts\Actions\ForwardAction"
parameter="resource"/>
<action path="/resourceC"
type="\Phruts\Actions\ForwardAction"
parameter="resource"/>
</action-mappings>

</phruts-config>
19 changes: 19 additions & 0 deletions tests/Resources/module2-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phruts-config>

<global-forwards>
<forward name="resource" path="/resource.html" />
</global-forwards>

<action-mappings>
<action path="/resourceA"
type="\Phruts\Actions\ForwardAction"
parameter="resource"/>
<action path="/resourceD"
type="\Phruts\Actions\ForwardAction"
parameter="resource"/>
<action path="/resourceE"
type="\Phruts\Actions\ForwardAction"
parameter="resource"/>
</action-mappings>
</phruts-config>
1 change: 1 addition & 0 deletions tests/Resources/resource.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Resource</h1>
1 change: 1 addition & 0 deletions tests/Resources/welcome.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome</h1>

0 comments on commit 379d6a4

Please sign in to comment.