File indexing completed on 2025-01-19 05:21:36
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Tool 0017 * @subpackage Framework 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id: ActionMethod.php 20096 2010-01-06 02:05:09Z bkarwin $ 0021 */ 0022 0023 /** 0024 * @see Zend_Tool_Project_Context_Interface 0025 */ 0026 // require_once 'Zend/Tool/Project/Context/Interface.php'; 0027 0028 /** 0029 * @see Zend_Reflection_File 0030 */ 0031 // require_once 'Zend/Reflection/File.php'; 0032 0033 /** 0034 * This class is the front most class for utilizing Zend_Tool_Project 0035 * 0036 * A profile is a hierarchical set of resources that keep track of 0037 * items within a specific project. 0038 * 0039 * @category Zend 0040 * @package Zend_Tool 0041 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0042 * @license http://framework.zend.com/license/new-bsd New BSD License 0043 */ 0044 class Zend_Tool_Project_Context_Zf_TestApplicationActionMethod implements Zend_Tool_Project_Context_Interface 0045 { 0046 0047 /** 0048 * @var Zend_Tool_Project_Profile_Resource 0049 */ 0050 protected $_resource = null; 0051 0052 /** 0053 * @var Zend_Tool_Project_Profile_Resource 0054 */ 0055 protected $_testApplicationControllerResource = null; 0056 0057 /** 0058 * @var string 0059 */ 0060 protected $_testApplicationControllerPath = ''; 0061 0062 /** 0063 * @var string 0064 */ 0065 protected $_forActionName = null; 0066 0067 /** 0068 * init() 0069 * 0070 * @return Zend_Tool_Project_Context_Zf_ActionMethod 0071 */ 0072 public function init() 0073 { 0074 $this->_forActionName = $this->_resource->getAttribute('forActionName'); 0075 0076 $this->_resource->setAppendable(false); 0077 $this->_testApplicationControllerResource = $this->_resource->getParentResource(); 0078 if (!$this->_testApplicationControllerResource->getContext() instanceof Zend_Tool_Project_Context_Zf_TestApplicationControllerFile) { 0079 // require_once 'Zend/Tool/Project/Context/Exception.php'; 0080 throw new Zend_Tool_Project_Context_Exception('ActionMethod must be a sub resource of a TestApplicationControllerFile'); 0081 } 0082 // make the ControllerFile node appendable so we can tack on the actionMethod. 0083 $this->_resource->getParentResource()->setAppendable(true); 0084 0085 $this->_testApplicationControllerPath = $this->_testApplicationControllerResource->getContext()->getPath(); 0086 0087 return $this; 0088 } 0089 0090 /** 0091 * getPersistentAttributes 0092 * 0093 * @return array 0094 */ 0095 public function getPersistentAttributes() 0096 { 0097 return array( 0098 'forActionName' => $this->getForActionName() 0099 ); 0100 } 0101 0102 /** 0103 * getName() 0104 * 0105 * @return string 0106 */ 0107 public function getName() 0108 { 0109 return 'TestApplicationActionMethod'; 0110 } 0111 0112 /** 0113 * setResource() 0114 * 0115 * @param Zend_Tool_Project_Profile_Resource $resource 0116 * @return Zend_Tool_Project_Context_Zf_ActionMethod 0117 */ 0118 public function setResource(Zend_Tool_Project_Profile_Resource $resource) 0119 { 0120 $this->_resource = $resource; 0121 return $this; 0122 } 0123 0124 /** 0125 * getActionName() 0126 * 0127 * @return string 0128 */ 0129 public function getForActionName() 0130 { 0131 return $this->_forActionName; 0132 } 0133 0134 /** 0135 * create() 0136 * 0137 * @return Zend_Tool_Project_Context_Zf_ActionMethod 0138 */ 0139 public function create() 0140 { 0141 $file = $this->_testApplicationControllerPath; 0142 0143 if (!file_exists($file)) { 0144 // require_once 'Zend/Tool/Project/Context/Exception.php'; 0145 throw new Zend_Tool_Project_Context_Exception( 0146 'Could not create action within test controller ' . $file 0147 . ' with action name ' . $this->_forActionName 0148 ); 0149 } 0150 0151 $actionParam = $this->getForActionName(); 0152 $controllerParam = $this->_resource->getParentResource()->getForControllerName(); 0153 //$moduleParam = null;// 0154 0155 /* @var $controllerDirectoryResource Zend_Tool_Project_Profile_Resource */ 0156 $controllerDirectoryResource = $this->_resource->getParentResource()->getParentResource(); 0157 if ($controllerDirectoryResource->getParentResource()->getName() == 'TestApplicationModuleDirectory') { 0158 $moduleParam = $controllerDirectoryResource->getParentResource()->getForModuleName(); 0159 } else { 0160 $moduleParam = 'default'; 0161 } 0162 0163 0164 0165 if ($actionParam == 'index' && $controllerParam == 'Index' && $moduleParam == 'default') { 0166 $assert = '$this->assertQueryContentContains("div#welcome h3", "This is your project\'s main page");'; 0167 } else { 0168 $assert = <<<EOS 0169 \$this->assertQueryContentContains( 0170 'div#view-content p', 0171 'View script for controller <b>' . \$params['controller'] . '</b> and script/action name <b>' . \$params['action'] . '</b>' 0172 ); 0173 EOS; 0174 } 0175 0176 $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($file, true, true); 0177 $codeGenFile->getClass()->setMethod(array( 0178 'name' => 'test' . ucfirst($actionParam) . 'Action', 0179 'body' => <<<EOS 0180 \$params = array('action' => '$actionParam', 'controller' => '$controllerParam', 'module' => '$moduleParam'); 0181 \$urlParams = \$this->urlizeOptions(\$params); 0182 \$url = \$this->url(\$urlParams); 0183 \$this->dispatch(\$url); 0184 0185 // assertions 0186 \$this->assertModule(\$urlParams['module']); 0187 \$this->assertController(\$urlParams['controller']); 0188 \$this->assertAction(\$urlParams['action']); 0189 $assert 0190 0191 EOS 0192 )); 0193 0194 file_put_contents($file, $codeGenFile->generate()); 0195 0196 return $this; 0197 } 0198 0199 /** 0200 * delete() 0201 * 0202 * @return Zend_Tool_Project_Context_Zf_ActionMethod 0203 */ 0204 public function delete() 0205 { 0206 // @todo do this 0207 return $this; 0208 } 0209 0210 /** 0211 * hasActionMethod() 0212 * 0213 * @param string $controllerPath 0214 * @param string $actionName 0215 * @return bool 0216 */ 0217 /* 0218 public static function hasActionMethod($controllerPath, $actionName) 0219 { 0220 if (!file_exists($controllerPath)) { 0221 return false; 0222 } 0223 0224 $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true); 0225 return $controllerCodeGenFile->getClass()->hasMethod('test' . $actionName . 'Action'); 0226 } 0227 */ 0228 0229 }