/library/Zend/Tool/Project/Context/Zf/TestApplicationActionMethod.php

https://bitbucket.org/hamidrezas/melobit · PHP · 229 lines · 91 code · 30 blank · 108 comment · 12 complexity · eac816802df420c4ab8562592509af96 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ActionMethod.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Context_Interface
  24. */
  25. require_once 'Zend/Tool/Project/Context/Interface.php';
  26. /**
  27. * @see Zend_Reflection_File
  28. */
  29. require_once 'Zend/Reflection/File.php';
  30. /**
  31. * This class is the front most class for utilizing Zend_Tool_Project
  32. *
  33. * A profile is a hierarchical set of resources that keep track of
  34. * items within a specific project.
  35. *
  36. * @category Zend
  37. * @package Zend_Tool
  38. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Tool_Project_Context_Zf_TestApplicationActionMethod implements Zend_Tool_Project_Context_Interface
  42. {
  43. /**
  44. * @var Zend_Tool_Project_Profile_Resource
  45. */
  46. protected $_resource = null;
  47. /**
  48. * @var Zend_Tool_Project_Profile_Resource
  49. */
  50. protected $_testApplicationControllerResource = null;
  51. /**
  52. * @var string
  53. */
  54. protected $_testApplicationControllerPath = '';
  55. /**
  56. * @var string
  57. */
  58. protected $_forActionName = null;
  59. /**
  60. * init()
  61. *
  62. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  63. */
  64. public function init()
  65. {
  66. $this->_forActionName = $this->_resource->getAttribute('forActionName');
  67. $this->_resource->setAppendable(false);
  68. $this->_testApplicationControllerResource = $this->_resource->getParentResource();
  69. if (!$this->_testApplicationControllerResource->getContext() instanceof Zend_Tool_Project_Context_Zf_TestApplicationControllerFile) {
  70. require_once 'Zend/Tool/Project/Context/Exception.php';
  71. throw new Zend_Tool_Project_Context_Exception('ActionMethod must be a sub resource of a TestApplicationControllerFile');
  72. }
  73. // make the ControllerFile node appendable so we can tack on the actionMethod.
  74. $this->_resource->getParentResource()->setAppendable(true);
  75. $this->_testApplicationControllerPath = $this->_testApplicationControllerResource->getContext()->getPath();
  76. return $this;
  77. }
  78. /**
  79. * getPersistentAttributes
  80. *
  81. * @return array
  82. */
  83. public function getPersistentAttributes()
  84. {
  85. return array(
  86. 'forActionName' => $this->getForActionName()
  87. );
  88. }
  89. /**
  90. * getName()
  91. *
  92. * @return string
  93. */
  94. public function getName()
  95. {
  96. return 'TestApplicationActionMethod';
  97. }
  98. /**
  99. * setResource()
  100. *
  101. * @param Zend_Tool_Project_Profile_Resource $resource
  102. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  103. */
  104. public function setResource(Zend_Tool_Project_Profile_Resource $resource)
  105. {
  106. $this->_resource = $resource;
  107. return $this;
  108. }
  109. /**
  110. * getActionName()
  111. *
  112. * @return string
  113. */
  114. public function getForActionName()
  115. {
  116. return $this->_forActionName;
  117. }
  118. /**
  119. * create()
  120. *
  121. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  122. */
  123. public function create()
  124. {
  125. $file = $this->_testApplicationControllerPath;
  126. if (!file_exists($file)) {
  127. require_once 'Zend/Tool/Project/Context/Exception.php';
  128. throw new Zend_Tool_Project_Context_Exception(
  129. 'Could not create action within test controller ' . $file
  130. . ' with action name ' . $this->_forActionName
  131. );
  132. }
  133. $actionParam = $this->getForActionName();
  134. $controllerParam = $this->_resource->getParentResource()->getForControllerName();
  135. //$moduleParam = null;//
  136. /* @var $controllerDirectoryResource Zend_Tool_Project_Profile_Resource */
  137. $controllerDirectoryResource = $this->_resource->getParentResource()->getParentResource();
  138. if ($controllerDirectoryResource->getParentResource()->getName() == 'TestApplicationModuleDirectory') {
  139. $moduleParam = $controllerDirectoryResource->getParentResource()->getForModuleName();
  140. } else {
  141. $moduleParam = 'default';
  142. }
  143. if ($actionParam == 'index' && $controllerParam == 'Index' && $moduleParam == 'default') {
  144. $assert = '$this->assertQueryContentContains("div#welcome h3", "This is your project\'s main page");';
  145. } else {
  146. $assert = <<<EOS
  147. \$this->assertQueryContentContains(
  148. 'div#view-content p',
  149. 'View script for controller <b>' . \$params['controller'] . '</b> and script/action name <b>' . \$params['action'] . '</b>'
  150. );
  151. EOS;
  152. }
  153. $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($file, true, true);
  154. $codeGenFile->getClass()->setMethod(array(
  155. 'name' => 'test' . ucfirst($actionParam) . 'Action',
  156. 'body' => <<<EOS
  157. \$params = array('action' => '$actionParam', 'controller' => '$controllerParam', 'module' => '$moduleParam');
  158. \$urlParams = \$this->urlizeOptions(\$params);
  159. \$url = \$this->url(\$urlParams);
  160. \$this->dispatch(\$url);
  161. // assertions
  162. \$this->assertModule(\$urlParams['module']);
  163. \$this->assertController(\$urlParams['controller']);
  164. \$this->assertAction(\$urlParams['action']);
  165. $assert
  166. EOS
  167. ));
  168. file_put_contents($file, $codeGenFile->generate());
  169. return $this;
  170. }
  171. /**
  172. * delete()
  173. *
  174. * @return Zend_Tool_Project_Context_Zf_ActionMethod
  175. */
  176. public function delete()
  177. {
  178. // @todo do this
  179. return $this;
  180. }
  181. /**
  182. * hasActionMethod()
  183. *
  184. * @param string $controllerPath
  185. * @param string $actionName
  186. * @return bool
  187. */
  188. /*
  189. public static function hasActionMethod($controllerPath, $actionName)
  190. {
  191. if (!file_exists($controllerPath)) {
  192. return false;
  193. }
  194. $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
  195. return $controllerCodeGenFile->getClass()->hasMethod('test' . $actionName . 'Action');
  196. }
  197. */
  198. }