Skip to content

Commit

Permalink
Adds more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Manderson committed Sep 15, 2014
1 parent 83a0e16 commit ddff142
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ final public function __construct()

}

public function __wakeup()
{

}
// public function __wakeup()
// {
//
// }

/**
* Return the controller action server instance to which we are attached.
Expand Down
2 changes: 1 addition & 1 deletion src/Action/ActionForward.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace \Phruts\Action;
namespace Phruts\Action;

class ActionForward extends \Phruts\Config\ForwardConfig
{
Expand Down
17 changes: 0 additions & 17 deletions src/Action/RequestDispatcherMatcher.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Action/RequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class RequestProcessor
*/
protected $actionKernel = null;

public function __wakeup()
{
}
// public function __wakeup()
// {
// }

final public function __construct()
{
Expand Down Expand Up @@ -808,7 +808,7 @@ protected function doForward($uri, \Symfony\Component\HttpFoundation\Request $re

// TODO: Update to match the request dispatcher
$app = $this->actionKernel->getApplication();
$rd = $app['request_dispatcher_matcher']->getRequestDispatcher($uri);
$rd = $app['request_dispatcher'];
if (is_null($rd)) {
$response->setStatusCode(500);
$response->setContent($this->getInternal()->getMessage(null, 'requestDispatcher', $uri));
Expand All @@ -831,7 +831,7 @@ protected function doInclude($uri, \Symfony\Component\HttpFoundation\Request $re
{
// TODO: Update to match the request dispatcher
$app = $this->actionKernel->getApplication();
$rd = $app['request_dispatcher_matcher']->getRequestDispatcher($uri);
$rd = $app['request_dispatcher'];
if (is_null($rd)) {
$response->setStatusCode(500);
$response->setContent($this->getInternal()->getMessage(null, 'requestDispatcher', $uri));
Expand Down
36 changes: 35 additions & 1 deletion src/Actions/ForwardAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,38 @@

namespace Phruts\Actions;

class ForwardAction extends \Phruts\Action {}
use Phruts\Config\ForwardConfig;

class ForwardAction extends \Phruts\Action {
public function execute(
\Phruts\Config\ActionConfig $mapping,
$form,
\Symfony\Component\HttpFoundation\Request $request,
\Symfony\Component\HttpFoundation\Response $response
) {

// Action Config defines the parameter for the forward configuration
$parameter = $mapping->getParameter();
if(empty($parameter)) {
throw new \Phruts\Exception\IllegalArgumentException('Need to specify a parameter for this ForwardAction');
}

// Original strategy, let's assume it is a path
if(!preg_match('/^[A-z]+$/', $parameter)) {
$forward = new ForwardConfig();
$forward->setPath($parameter);
$forward->setContextRelative(true);
return $forward;
} else {
// This implementation of forward action is slightly different that the original
// where the forward config must be defined and the key passed as parameter
// so that nextAction/action chaining is implemented
// TODO: Create an action chaining forward path
$forward = $mapping->findForwardConfig($parameter);
if(empty($forward)) {
throw new \Phruts\Exception('ForwardAction parameter should reference a forward config name');
}
return $forward;
}
}
}
57 changes: 50 additions & 7 deletions tests/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ public function testGetDataSource()
$datasource = $getDataSource->invokeArgs($this->action, array(new \Symfony\Component\HttpFoundation\Request(), 'key'));
$this->assertNotEmpty($datasource);
$this->assertTrue($datasource instanceof PDO);

$this->actionKernel->method('getDataSource')->will($this->throwException(new Exception));
$this->setExpectedException('\Exception');
$datasource = $getDataSource->invokeArgs($this->action, array(new \Symfony\Component\HttpFoundation\Request(), 'key'));
}

public function testGetResources()
{
$getDataSource = self::getMethod('getResources');

// Get from the request
$messages = new \Phruts\Util\PropertyMessageResources(__DIR__ .'/UtilTest/Example');
$this->request->attributes->set(\Phruts\Util\Globals::MESSAGES_KEY, $messages);
$result = $getDataSource->invokeArgs($this->action, array($this->request));
$this->assertNotEmpty($result);

// Get from the application
$request = \Symfony\Component\HttpFoundation\Request::create('http://localhost/test', 'GET', array(), array(), array(), array('PATH_INFO' => '/test'));

$application = new Silex\Application;
$application[\Phruts\Util\Globals::PREFIXES_KEY] = array();
$moduleConfig = new \Phruts\Config\ModuleConfig('');
$application[\Phruts\Util\Globals::MODULE_KEY] = $moduleConfig;
\Phruts\Util\RequestUtils::selectModule($request, $application);

$key = 'key';
$application[$key] = $messages;
$actionKernel = new \Phruts\Action\ActionKernel($application);
$this->action->setActionKernel($actionKernel);

$result = $getDataSource->invokeArgs($this->action, array($this->request, $key));
$this->assertNotEmpty($result);
}

public function testLocale()
Expand All @@ -71,11 +103,6 @@ public function testLocale()
$this->assertEquals('fr', $getLocale->invokeArgs($this->action, array($this->request)));
}

public function testGetResources()
{
// TODO: Test
}

public function testIsCancelled()
{
$isCancelled = self::getMethod('isCancelled');
Expand Down Expand Up @@ -134,6 +161,7 @@ public function testAddErrors()
$errors->add('key', new \Phruts\Action\ActionError('message'));

$saveErrors = self::getMethod('addErrors');
$this->assertEmpty($saveErrors->invokeArgs($this->action, array($this->request, null)));
$saveErrors->invokeArgs($this->action, array($this->request, $errors));

$this->assertNotEmpty($this->request->attributes->get(\Phruts\Util\Globals::ERROR_KEY));
Expand Down Expand Up @@ -265,22 +293,37 @@ public function testGetMessages()

public function testGenerateToken()
{
$generateLocale = self::getMethod('generateToken');
$generateToken = self::getMethod('generateToken');

$result = $generateToken->invokeArgs($this->action, array($this->request));
$this->assertNotEmpty($result);
}

public function testIsTokenValid()
{
$saveToken = self::getMethod('saveToken');
$isTokenValid = self::getMethod('isTokenValid');
$saveToken->invokeArgs($this->action, array($this->request));
$token = $this->request->getSession()->get(\Phruts\Util\Globals::TRANSACTION_TOKEN_KEY);
$this->request->query->set(\Phruts\Util\Globals::TOKEN_KEY, $token);
$this->assertTrue($isTokenValid->invokeArgs($this->action, array($this->request)));
}

public function testResetToken()
{
$saveToken = self::getMethod('saveToken');
$resetToken = self::getMethod('resetToken');
$saveToken->invokeArgs($this->action, array($this->request));
$this->assertNotEmpty($this->request->getSession()->get(\Phruts\Util\Globals::TRANSACTION_TOKEN_KEY));
$resetToken->invokeArgs($this->action, array($this->request));
$this->assertEmpty($this->request->getSession()->get(\Phruts\Util\Globals::TRANSACTION_TOKEN_KEY));
}

public function testSaveToken()
{
$isTokenValid = self::getMethod('isTokenValid');
$saveToken = self::getMethod('saveToken');
$saveToken->invokeArgs($this->action, array($this->request));
$this->assertNotEmpty($this->request->getSession()->get(\Phruts\Util\Globals::TRANSACTION_TOKEN_KEY));
}

public function testExecute()
Expand Down
91 changes: 86 additions & 5 deletions tests/ActionTest/RequestProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

use Phruts\Action\ActionMapping;
use Phruts\Action\RequestDispatcherMatcher;
use Phruts\Config\ModuleConfig;
use Phruts\Util\ClassLoader;
use Phruts\Config\ActionConfig;
use Phruts\Config\ForwardConfig;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RequestProcessorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Phruts\Action\RequestProcessor
*/
protected $requestProcessor;
protected $request;
protected $response;
Expand All @@ -33,10 +40,11 @@ public function setUp()
$storage = new \Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage();
$session = new \Symfony\Component\HttpFoundation\Session\Session($storage);
$this->request->setSession($session);
$this->request->initialize();
$this->response = new \Symfony\Component\HttpFoundation\Response();


$this->moduleConfig = new \Phruts\Config\ModuleConfig('default');
$this->moduleConfig = new \Phruts\Config\ModuleConfig('');
$actionConfig = new \Phruts\Config\ActionConfig();
$actionConfig->setPath('/default');
$actionConfig->setType('\Phruts\Actions\ForwardAction');
Expand Down Expand Up @@ -72,12 +80,28 @@ public function setUp()
$dispatcher->method('doForward')
->willReturn(null);

$requestMatcher = new RequestDispatcherMatcher($dispatcher);
$this->application['request_dispatcher_matcher'] = $requestMatcher;

// $requestMatcher = new RequestDispatcherMatcher($dispatcher);
$this->application['request_dispatcher'] = $dispatcher;
}


public function testProcessPath()
{
// Mock a request
$request = $this->getMock('\Symfony\Component\HttpFoundation\Request');
$request->initialize();
$request->expects($this->exactly(1))
->method('getPathInfo')
->willReturn('/test');

$method = self::getMethod('processPath');

$result = $method->invokeArgs($this->requestProcessor, array($request, $this->response));
$this->assertNotEmpty($result);
$this->assertNotEquals(400, $this->response->getStatusCode());
$this->assertEquals('/test', $result);
}

public function testProcessLocale()
{
$method = self::getMethod('processLocale');
Expand Down Expand Up @@ -231,6 +255,64 @@ public function testDoInclude()
$method->invokeArgs($this->requestProcessor, array($uri, $this->request, $this->response));
}

public function testProcess()
{
// Mock a request
$request = Request::create('http://localhost/test', 'GET', array(), array(), array(), array('PATH_INFO' => '/test'));

// Update the mock
$dispatcher = $this->getMock('\Phruts\Action\RequestDispatcher');
$dispatcher->expects($this->once())
->method('doForward')
->willReturn(null);
$this->application['request_dispatcher'] = $dispatcher;

// Setup a base action configuration to forward a success
$actionConfig = new ActionMapping();
$actionConfig->setPath('/test');
$actionConfig->setType('\Phruts\Actions\ForwardAction');
$actionConfig->setParameter('success');
$actionConfig->setModuleConfig($this->moduleConfig);
$this->moduleConfig->addActionConfig($actionConfig);
$forwardConfig = new ForwardConfig();
$forwardConfig->setName('success');
$forwardConfig->setPath('file.html');
$actionConfig->addForwardConfig($forwardConfig);

$this->requestProcessor->process($request, $this->response);
$this->assertEquals(200, $this->response->getStatusCode());
}

// public function testNextActionForward()
// {
// $request = $this->getMock('\Symfony\Component\HttpFoundation\Request');
// $request->initialize();
// $request->expects($this->exactly(1))
// ->method('getPathInfo')
// ->willReturn('/test');
//
// // Update the mock
// $dispatcher = $this->getMock('\Phruts\Action\RequestDispatcher');
// $dispatcher->expects($this->exactly(1))
// ->method('doInclude')
// ->willReturn(null);
// $this->application['request_dispatcher'] = $dispatcher;
//
// // Setup a base action configuration to forward a success
// $actionConfig = new ActionMapping();
// $actionConfig->setPath('/test');
// $actionConfig->setType('\ActionTest\MockAction');
// $actionConfig->setModuleConfig($this->moduleConfig);
// $this->moduleConfig->addActionConfig($actionConfig);
// $forwardConfig = new ForwardConfig();
// $forwardConfig->setName('success');
// $forwardConfig->setPath('file.html');
//
// $this->requestProcessor->process($request, $this->response);
// }



protected static function getMethod($name)
{
$class = new \ReflectionClass('\Phruts\Action\RequestProcessor');
Expand All @@ -240,4 +322,3 @@ protected static function getMethod($name)
}

}

Loading

0 comments on commit ddff142

Please sign in to comment.