From ddff1427aacc1cb6075a4ec6dbf6b4c1b5b8826a Mon Sep 17 00:00:00 2001 From: Cameron Manderson Date: Mon, 15 Sep 2014 14:21:44 +1000 Subject: [PATCH] Adds more test coverage --- src/Action.php | 8 +- src/Action/ActionForward.php | 2 +- src/Action/RequestDispatcherMatcher.php | 17 ----- src/Action/RequestProcessor.php | 10 +-- src/Actions/ForwardAction.php | 36 ++++++++- tests/ActionTest.php | 57 ++++++++++++-- tests/ActionTest/RequestProcessorTest.php | 91 +++++++++++++++++++++-- tests/ActionsTest/ForwardActionTest.php | 78 +++++++++++++++++++ tests/{ => UtilTest}/ClassLoaderTest.php | 6 +- 9 files changed, 261 insertions(+), 44 deletions(-) delete mode 100644 src/Action/RequestDispatcherMatcher.php create mode 100644 tests/ActionsTest/ForwardActionTest.php rename tests/{ => UtilTest}/ClassLoaderTest.php (97%) diff --git a/src/Action.php b/src/Action.php index d5484e6..d6cbbd6 100644 --- a/src/Action.php +++ b/src/Action.php @@ -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. diff --git a/src/Action/ActionForward.php b/src/Action/ActionForward.php index 00dc5b4..bb69c8e 100644 --- a/src/Action/ActionForward.php +++ b/src/Action/ActionForward.php @@ -1,5 +1,5 @@ dispatcher = $dispatcher; - } - - public function getRequestDispatcher($uri) - { - return $this->dispatcher; - } -} - \ No newline at end of file diff --git a/src/Action/RequestProcessor.php b/src/Action/RequestProcessor.php index 424dfa0..9bf5173 100644 --- a/src/Action/RequestProcessor.php +++ b/src/Action/RequestProcessor.php @@ -44,9 +44,9 @@ class RequestProcessor */ protected $actionKernel = null; - public function __wakeup() - { - } +// public function __wakeup() +// { +// } final public function __construct() { @@ -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)); @@ -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)); diff --git a/src/Actions/ForwardAction.php b/src/Actions/ForwardAction.php index 7bd693f..7c7ef06 100644 --- a/src/Actions/ForwardAction.php +++ b/src/Actions/ForwardAction.php @@ -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; + } + } +} diff --git a/tests/ActionTest.php b/tests/ActionTest.php index 8a5cbfe..7390df0 100644 --- a/tests/ActionTest.php +++ b/tests/ActionTest.php @@ -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() @@ -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'); @@ -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)); @@ -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() diff --git a/tests/ActionTest/RequestProcessorTest.php b/tests/ActionTest/RequestProcessorTest.php index 2f4f7e1..95b4725 100644 --- a/tests/ActionTest/RequestProcessorTest.php +++ b/tests/ActionTest/RequestProcessorTest.php @@ -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; @@ -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'); @@ -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'); @@ -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'); @@ -240,4 +322,3 @@ protected static function getMethod($name) } } - \ No newline at end of file diff --git a/tests/ActionsTest/ForwardActionTest.php b/tests/ActionsTest/ForwardActionTest.php new file mode 100644 index 0000000..1ffadf6 --- /dev/null +++ b/tests/ActionsTest/ForwardActionTest.php @@ -0,0 +1,78 @@ +assertTrue($action instanceof \Phruts\Action); + } + + public function testForward() + { + $actionConfig = new ActionMapping(); + $actionConfig->setPath('/test'); + $actionConfig->setParameter('success'); + $moduleConfig = new ModuleConfig(''); + $moduleConfig->addActionConfig($actionConfig); + $actionConfig->setModuleConfig($moduleConfig); + + $forwardConfigOrig = new ForwardConfig(); + $forwardConfigOrig->setName('success'); + $actionConfig->addForwardConfig($forwardConfigOrig); + + $action = new ForwardAction(); + $request = new Request(); + $response = new Response(); + + $forwardConfig = $action->execute($actionConfig, null, $request, $response); + + $this->assertNotEmpty($forwardConfig); + $this->assertEquals($forwardConfigOrig->getName(), $forwardConfig->getName()); + } + + public function testCreateForward() + { + $actionConfig = new ActionMapping(); + $actionConfig->setPath('/test'); + $actionConfig->setParameter('myfile.html'); + $moduleConfig = new ModuleConfig(''); + $moduleConfig->addActionConfig($actionConfig); + $actionConfig->setModuleConfig($moduleConfig); + + $action = new ForwardAction(); + $request = new Request(); + $response = new Response(); + + $forwardConfig = $action->execute($actionConfig, null, $request, $response); + + $this->assertNotEmpty($forwardConfig); + $this->assertEquals($actionConfig->getParameter(), $forwardConfig->getPath()); + } + + public function testEmptyParameter() + { + $actionConfig = new ActionMapping(); + $actionConfig->setPath('/test'); + // NO PARAM + $moduleConfig = new ModuleConfig(''); + $moduleConfig->addActionConfig($actionConfig); + $actionConfig->setModuleConfig($moduleConfig); + + $action = new ForwardAction(); + $request = new Request(); + $response = new Response(); + + $this->setExpectedException('\Phruts\Exception'); + $action->execute($actionConfig, null, $request, $response); + } +} + \ No newline at end of file diff --git a/tests/ClassLoaderTest.php b/tests/UtilTest/ClassLoaderTest.php similarity index 97% rename from tests/ClassLoaderTest.php rename to tests/UtilTest/ClassLoaderTest.php index b83c8ae..65fb579 100644 --- a/tests/ClassLoaderTest.php +++ b/tests/UtilTest/ClassLoaderTest.php @@ -1,7 +1,5 @@ - */ +namespace UtilTest; class ClassLoaderTest extends \PHPUnit_Framework_TestCase { @@ -42,7 +40,7 @@ public function testClassIsAssignableFrom() { $this->assertTrue(\Phruts\Util\ClassLoader::classIsAssignableFrom('\Phruts\Actions\ActionDispatcher', '\Phruts\Action')); $this->assertTrue(\Phruts\Util\ClassLoader::classIsAssignableFrom('\Phruts\Action', '\Phruts\Action')); - $this->assertTrue(\Phruts\Util\ClassLoader::classIsAssignableFrom('B', 'A')); + $this->assertTrue(\Phruts\Util\ClassLoader::classIsAssignableFrom('\UtilTest\B', '\UtilTest\A')); } public function testNewInstance()