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); }
public function testGetInputForward() { // Input forward $this->actionMapping->setInput('aPath'); $forward = $this->actionMapping->getInputForward(); $this->assertNotEmpty($forward); $this->assertEquals('aPath', $forward->getPath()); // Controller based input forward $controllerConfig = new \Phruts\Config\ControllerConfig(); $controllerConfig->setInputForward(true); $this->actionMapping->setInput('path1'); $this->moduleConfig->setControllerConfig($controllerConfig); $forward = $this->actionMapping->getInputForward(); $this->assertNotEmpty($forward); $this->assertEquals('path1', $forward->getName()); }
/** * If this request was not cancelled, and the request's \Phruts\Config\ActionConfig * has not disabled validation, call the validate method of the specified * ActionForm, and forward back to the input form if there were any * errors. * * Return true if we should continue processing, or false if we have already * forwarded control back to the input form. * * @param \Symfony\Component\HttpFoundation\Request $request The kernel request we are * processing * @param \Symfony\Component\HttpFoundation\Response $response The kernel response we are * creating * @param \Phruts\Action\AbstractActionForm $form The ActionForm instance we are * populating * @param \Phruts\Action\ActionMapping $mapping The \Phruts\Config\ActionConfig we are using * @return boolean */ protected function processValidate(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response, \Phruts\Action\AbstractActionForm $form = null, \Phruts\Action\ActionMapping $mapping) { if (is_null($form)) { return true; } // Was this request cancelled? if (!is_null($request->attributes->get(\Phruts\Util\Globals::CANCEL_KEY))) { if (!empty($this->log)) { $this->log->debug(' Cancelled transaction, skipping validation'); } return true; } // Has validation been turned off for this mapping? if (!$mapping->getValidate()) { return true; } // Call the form bean's validation method if (!empty($this->log)) { $this->log->debug(' Validating input form properties'); } $errors = $form->validate($mapping, $request); if (is_null($errors) || $errors->isEmpty()) { if (!empty($this->log)) { $this->log->debug(' No errors detected, accepting input'); } return true; } // Has an input form been specified for this mapping? $input = $mapping->getInput(); if (is_null($input)) { if (!empty($this->log)) { $this->log->debug(' Validation failed but no input form available'); } $msg = $this->getInternal()->getMessage(null, 'noInput', $mapping->getPath()); throw new \Phruts\Exception($msg); } // Save our error messages and return to the input form if possible if (!empty($this->log)) { $this->log->debug(' Validation failed, returning to "' . $input . '"'); } $request->attributes->set(\Phruts\Util\Globals::ERROR_KEY, $errors); if ($this->moduleConfig->getControllerConfig()->getInputForward()) { $forward = $mapping->findForward($input); $this->processForwardConfig($request, $response, $forward); } else { // Delegate the processing of this request if (!empty($this->log)) { $this->log->debug(' Delegating via forward to "' . $input . '"'); } $this->doForward($input, $request, $response); } }
public function testInvalidActionForm() { // Mock a request $request = Request::create('http://localhost/test', 'GET', array(), array(), array(), array('PATH_INFO' => '/test')); $formConfig = new FormBeanConfig(); $formConfig->setName('form1'); $formConfig->setType('\\ActionTest\\MyInvalidForm'); $this->moduleConfig->addFormBeanConfig($formConfig); $actionMapping = new ActionMapping(); $actionMapping->setScope('request'); $actionMapping->setPath('/test'); $actionMapping->setType('\\Phruts\\Actions\\ForwardAction'); $actionMapping->setParameter('success'); $actionMapping->setName('form1'); $actionMapping->setInput('myinput.html.twig'); $forwardConfig = new ForwardConfig(); $forwardConfig->setName('success'); $forwardConfig->setPath('success.html.twig'); $actionMapping->addForwardConfig($forwardConfig); $actionMapping->setModuleConfig($this->moduleConfig); $this->moduleConfig->addActionConfig($actionMapping); $this->requestProcessor->process($request, $this->response); }
/** * Create (if necessary) and return a \Phruts\Action\AbstractActionForm instance appropriate * for this request. * * If no \Phruts\Action\AbstractActionForm instance is required, return null. * * @param \Symfony\Component\HttpFoundation\Request $request The actionKernel request we are * processing * @param \Phruts\Config\ActionConfig $mapping The action mapping for this request * @param \Phruts\Config\ModuleConfig $moduleConfig The configuration for this * module * @param \Phruts\Action\ActionKernel $actionKernel The action actionKernel * @return \Phruts\Action\AbstractActionForm Form instance associated with this * request */ public static function createActionForm(\Symfony\Component\HttpFoundation\Request $request, \Phruts\Config\ActionConfig $mapping, \Phruts\Config\ModuleConfig $moduleConfig, \Phruts\Action\ActionKernel $kernel) { // Is there a form bean associated with this mapping? $attribute = $mapping->getAttribute(); if (is_null($attribute)) { return null; } // Look up the form bean configuration information to use $name = $mapping->getName(); $config = $moduleConfig->findFormBeanConfig($name); if (is_null($config)) { return null; } $instance = null; $session = null; if ($mapping->getScope() == 'request') { $instance = $request->attributes->get($attribute); } else { \Phruts\Util\ClassLoader::loadClass($config->getType()); $session = $request->getSession(); if (!empty($session)) { $instance = $session->get($attribute); } } // Can we recycle the existing form bean instance (if there is one)? if (!is_null($instance)) { $configClass = $config->getType(); $instanceClass = get_class($instance); if (\Phruts\Util\ClassLoader::classIsAssignableFrom($configClass, $instanceClass)) { return $instance; } } // Create and return a new form bean instance try { /** @var \Phruts\Action\AbstractActionForm $instance */ $instance = \Phruts\Util\ClassLoader::newInstance($config->getType(), '\\Phruts\\Action\\AbstractActionForm'); $instance->setActionKernel($kernel); } catch (\Exception $e) { $msg = $kernel->getInternal()->getMessage(null, 'formBean', $config->getType()); throw new \Phruts\Exception($msg); } return $instance; }
public function testModuleConfig() { $config = new ModuleConfig('prefix2'); $this->assertEquals('prefix2', $config->getPrefix()); $config->setPrefix('prefix'); $this->assertEquals('prefix', $config->getPrefix()); $controllerConfig = new ControllerConfig(); $controllerConfig->setProcessorClass('\\Mock\\Proccessor'); $controllerConfig->setContentType('application/x-javascript'); $controllerConfig->setNocache('true'); $controllerConfig->setInputForward('true'); $controllerConfig->setLocale('true'); $expected = "\\Phruts\\Config\\ControllerConfig[processorClass='\\\\Mock\\\\Proccessor',contentType='application/x-javascript',nocache=true,inputForward=true,locale=true]"; $this->assertEquals($expected, (string) $controllerConfig); $config->setControllerConfig($controllerConfig); $this->assertNotEmpty($config->getControllerConfig()); $actionConfig1 = new ActionConfig(); $actionConfig1->setPath('action1'); $actionConfig1->setType('\\ForwardConfig'); $config->addActionConfig($actionConfig1); $actionConfig2 = new ActionConfig(); $actionConfig2->setPath('action2'); $actionConfig2->setType('\\ForwardConfig'); $config->addActionConfig($actionConfig2); $this->assertNotEmpty($config->findActionConfig('/action1')); $this->assertNotEmpty($config->findActionConfig('/action2')); $config->removeActionConfig($actionConfig2); $this->assertEmpty($config->findActionConfig('/action2')); $actionClass = '\\MyActionConfigClass'; $config->setActionClass($actionClass); $this->assertEquals('\\MyActionConfigClass', $config->getActionClass()); $formBeanConfig1 = new FormBeanConfig(); $formBeanConfig1->setName('myForm1'); $formBeanConfig1->setType('\\MyForm1'); $formBeanConfig2 = new FormBeanConfig(); $formBeanConfig2->setName('myForm2'); $formBeanConfig2->setType('\\MyForm2'); $config->addFormBeanConfig($formBeanConfig1); $config->addFormBeanConfig($formBeanConfig2); $this->assertEquals(2, count($config->findFormBeanConfigs())); $this->assertEquals($formBeanConfig1, $config->findFormBeanConfig('myForm1')); $this->assertEquals($formBeanConfig2, $config->findFormBeanConfig('myForm2')); $config->removeFormBeanConfig($formBeanConfig2); $this->assertEmpty($config->findFormBeanConfig('myForm2')); $forwardConfig1 = new ForwardConfig(); $forwardConfig1->setName('welcome'); $forwardConfig1->setPath('welcome.html.twig'); $forwardConfig2 = new ForwardConfig(); $forwardConfig2->setName('login'); $forwardConfig1->setPath('login.html.twig'); $config->addForwardConfig($forwardConfig1); $config->addForwardConfig($forwardConfig2); $this->assertEquals($forwardConfig1, $config->findForwardConfig('welcome')); $this->assertEquals($forwardConfig2, $config->findForwardConfig('login')); $config->removeForwardConfig($forwardConfig1); $this->assertEmpty($config->findForwardConfig('welcome')); $dataSourceConfig = new DataSourceConfig(); $dataSourceConfig->setKey('key1'); $config->addDataSourceConfig($dataSourceConfig); $dataSourceConfig2 = new DataSourceConfig(); $dataSourceConfig2->setKey('key2'); $config->addDataSourceConfig($dataSourceConfig2); $this->assertEquals(2, count($config->findDataSourceConfigs())); $this->assertEquals($dataSourceConfig, $config->findDataSourceConfig('key1')); $this->assertEquals($dataSourceConfig2, $config->findDataSourceConfig('key2')); $config->removeDataSourceConfig($dataSourceConfig); $this->assertEmpty($config->findDataSourceConfig('key1')); $messageConfig = new MessageResourcesConfig(); $messageConfig->setKey('key1'); $config->addMessageResourcesConfig($messageConfig); $this->assertEquals($messageConfig, $config->findMessageResourcesConfig('key1')); $this->assertEquals(1, count($config->findMessageResourcesConfigs())); $config->removeMessageResourcesConfig($messageConfig); $this->assertEmpty($config->findMessageResourcesConfig('key1')); $plugInConfig = new PlugInConfig(); $plugInConfig->setKey('test'); $plugInConfig->setClassName('\\stdClass'); $config->addPlugInConfig($plugInConfig); $this->assertEquals(1, count($config->findPlugInConfigs())); $exceptionConfig = new ExceptionConfig(); $exceptionConfig->setType('\\Exception'); $config->addExceptionConfig($exceptionConfig); $this->assertEquals(1, count($config->findExceptionConfigs())); $this->assertEquals($exceptionConfig, $config->findExceptionConfig('\\Exception')); $config->removeExceptionConfig($exceptionConfig); $this->assertEmpty($config->findExceptionConfig('\\Exception')); // Test exception $config->freeze(); $this->assertTrue($config->getConfigured()); $this->setExpectedException('\\Phruts\\Exception\\IllegalStateException'); $config->setPrefix('prefix2'); }
protected function initModulePlugIns(\Phruts\Config\ModuleConfig $config) { if (!empty($this->log)) { $this->log->debug('Initializing module "' . $config->getPrefix() . '" plug ins'); } $plugInConfigs = $config->findPlugInConfigs(); $plugIns = array(); foreach ($plugInConfigs as $plugInConfig) { /* @var $plugInConfig \Phruts\Config\PlugInConfig */ try { /* @var $plugIn \Phruts\Action\PlugInInterface */ $plugIn = \Phruts\Util\ClassLoader::newInstance($plugInConfig->getClassName(), '\\Phruts\\Action\\PlugInInterface'); \Phruts\Util\BeanUtils::populate($plugIn, $plugInConfig->getProperties()); $plugIn->init($this, $config); $plugIns[] = $plugIn; } catch (\Exception $e) { $msg = $this->getInternal()->getMessage(null, 'plugIn.init', $plugInConfig->getClassName()); if (!empty($this->log)) { $this->log->error($msg . ' - ' . $e->getMessage()); } throw new \Phruts\Exception($msg); } } $this->application[\Phruts\Util\Globals::PLUG_INS_KEY . $config->getPrefix()] = $plugIns; }