web/lib/Zend/Tool/Project/Context/Zf/TestApplicationActionMethod.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     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 /**
       
    24  * @see Zend_Tool_Project_Context_Interface
       
    25  */
       
    26 require_once 'Zend/Tool/Project/Context/Interface.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Reflection_File
       
    30  */
       
    31 require_once 'Zend/Reflection/File.php';
       
    32 
       
    33 /**
       
    34  * This class is the front most class for utilizing Zend_Tool_Project
       
    35  *
       
    36  * A profile is a hierarchical set of resources that keep track of
       
    37  * items within a specific project.
       
    38  *
       
    39  * @category   Zend
       
    40  * @package    Zend_Tool
       
    41  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    43  */
       
    44 class Zend_Tool_Project_Context_Zf_TestApplicationActionMethod implements Zend_Tool_Project_Context_Interface
       
    45 {
       
    46 
       
    47     /**
       
    48      * @var Zend_Tool_Project_Profile_Resource
       
    49      */
       
    50     protected $_resource = null;
       
    51 
       
    52     /**
       
    53      * @var Zend_Tool_Project_Profile_Resource
       
    54      */
       
    55     protected $_testApplicationControllerResource = null;
       
    56 
       
    57     /**
       
    58      * @var string
       
    59      */
       
    60     protected $_testApplicationControllerPath = '';
       
    61 
       
    62     /**
       
    63      * @var string
       
    64      */
       
    65     protected $_forActionName = null;
       
    66 
       
    67     /**
       
    68      * init()
       
    69      *
       
    70      * @return Zend_Tool_Project_Context_Zf_ActionMethod
       
    71      */
       
    72     public function init()
       
    73     {
       
    74         $this->_forActionName = $this->_resource->getAttribute('forActionName');
       
    75 
       
    76         $this->_resource->setAppendable(false);
       
    77         $this->_testApplicationControllerResource = $this->_resource->getParentResource();
       
    78         if (!$this->_testApplicationControllerResource->getContext() instanceof Zend_Tool_Project_Context_Zf_TestApplicationControllerFile) {
       
    79             require_once 'Zend/Tool/Project/Context/Exception.php';
       
    80             throw new Zend_Tool_Project_Context_Exception('ActionMethod must be a sub resource of a TestApplicationControllerFile');
       
    81         }
       
    82         // make the ControllerFile node appendable so we can tack on the actionMethod.
       
    83         $this->_resource->getParentResource()->setAppendable(true);
       
    84 
       
    85         $this->_testApplicationControllerPath = $this->_testApplicationControllerResource->getContext()->getPath();
       
    86 
       
    87         return $this;
       
    88     }
       
    89 
       
    90     /**
       
    91      * getPersistentAttributes
       
    92      *
       
    93      * @return array
       
    94      */
       
    95     public function getPersistentAttributes()
       
    96     {
       
    97         return array(
       
    98             'forActionName' => $this->getForActionName()
       
    99             );
       
   100     }
       
   101 
       
   102     /**
       
   103      * getName()
       
   104      *
       
   105      * @return string
       
   106      */
       
   107     public function getName()
       
   108     {
       
   109         return 'TestApplicationActionMethod';
       
   110     }
       
   111 
       
   112     /**
       
   113      * setResource()
       
   114      *
       
   115      * @param Zend_Tool_Project_Profile_Resource $resource
       
   116      * @return Zend_Tool_Project_Context_Zf_ActionMethod
       
   117      */
       
   118     public function setResource(Zend_Tool_Project_Profile_Resource $resource)
       
   119     {
       
   120         $this->_resource = $resource;
       
   121         return $this;
       
   122     }
       
   123 
       
   124     /**
       
   125      * getActionName()
       
   126      *
       
   127      * @return string
       
   128      */
       
   129     public function getForActionName()
       
   130     {
       
   131         return $this->_forActionName;
       
   132     }
       
   133 
       
   134     /**
       
   135      * create()
       
   136      *
       
   137      * @return Zend_Tool_Project_Context_Zf_ActionMethod
       
   138      */
       
   139     public function create()
       
   140     {
       
   141         $file = $this->_testApplicationControllerPath;
       
   142         
       
   143         if (!file_exists($file)) {
       
   144             require_once 'Zend/Tool/Project/Context/Exception.php';
       
   145             throw new Zend_Tool_Project_Context_Exception(
       
   146                 'Could not create action within test controller ' . $file
       
   147                 . ' with action name ' . $this->_forActionName
       
   148                 );
       
   149         }
       
   150         
       
   151         $actionParam = $this->getForActionName();
       
   152         $controllerParam = $this->_resource->getParentResource()->getForControllerName();
       
   153         //$moduleParam = null;//
       
   154         
       
   155         /* @var $controllerDirectoryResource Zend_Tool_Project_Profile_Resource */
       
   156         $controllerDirectoryResource = $this->_resource->getParentResource()->getParentResource();
       
   157         if ($controllerDirectoryResource->getParentResource()->getName() == 'TestApplicationModuleDirectory') {
       
   158             $moduleParam = $controllerDirectoryResource->getParentResource()->getForModuleName();
       
   159         } else {
       
   160             $moduleParam = 'default';
       
   161         }
       
   162         
       
   163         
       
   164 
       
   165         if ($actionParam == 'index' && $controllerParam == 'Index' && $moduleParam == 'default') {
       
   166             $assert = '$this->assertQueryContentContains("div#welcome h3", "This is your project\'s main page");';
       
   167         } else {
       
   168             $assert = <<<EOS
       
   169 \$this->assertQueryContentContains(
       
   170     'div#view-content p',
       
   171     'View script for controller <b>' . \$params['controller'] . '</b> and script/action name <b>' . \$params['action'] . '</b>'
       
   172     );
       
   173 EOS;
       
   174         }
       
   175         
       
   176         $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($file, true, true);
       
   177         $codeGenFile->getClass()->setMethod(array(
       
   178             'name' => 'test' . ucfirst($actionParam) . 'Action',
       
   179             'body' => <<<EOS
       
   180 \$params = array('action' => '$actionParam', 'controller' => '$controllerParam', 'module' => '$moduleParam');
       
   181 \$urlParams = \$this->urlizeOptions(\$params);
       
   182 \$url = \$this->url(\$urlParams);
       
   183 \$this->dispatch(\$url);
       
   184 
       
   185 // assertions
       
   186 \$this->assertModule(\$urlParams['module']);
       
   187 \$this->assertController(\$urlParams['controller']);
       
   188 \$this->assertAction(\$urlParams['action']);
       
   189 $assert
       
   190 
       
   191 EOS
       
   192             ));
       
   193 
       
   194         file_put_contents($file, $codeGenFile->generate());
       
   195         
       
   196         return $this;
       
   197     }
       
   198 
       
   199     /**
       
   200      * delete()
       
   201      *
       
   202      * @return Zend_Tool_Project_Context_Zf_ActionMethod
       
   203      */
       
   204     public function delete()
       
   205     {
       
   206         // @todo do this
       
   207         return $this;
       
   208     }
       
   209 
       
   210     /**
       
   211      * hasActionMethod()
       
   212      *
       
   213      * @param string $controllerPath
       
   214      * @param string $actionName
       
   215      * @return bool
       
   216      */
       
   217     /*
       
   218     public static function hasActionMethod($controllerPath, $actionName)
       
   219     {
       
   220         if (!file_exists($controllerPath)) {
       
   221             return false;
       
   222         }
       
   223 
       
   224         $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
       
   225         return $controllerCodeGenFile->getClass()->hasMethod('test' . $actionName . 'Action');
       
   226     }
       
   227     */
       
   228 
       
   229 }