File indexing completed on 2024-12-29 05:28:10

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$
0021  */
0022 
0023 /**
0024  * @see Zend_Tool_Project_Provider_Abstract
0025  */
0026 // require_once 'Zend/Tool/Project/Provider/Abstract.php';
0027 
0028 /**
0029  * @see Zend_Tool_Project_Provider_Exception
0030  */
0031 // require_once 'Zend/Tool/Project/Provider/Exception.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_Tool
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Tool_Project_Provider_Test extends Zend_Tool_Project_Provider_Abstract
0040 {
0041 
0042     protected $_specialties = array('Application', 'Library');
0043 
0044     /**
0045      * isTestingEnabled()
0046      *
0047      * @param Zend_Tool_Project_Profile $profile
0048      * @return bool
0049      */
0050     public static function isTestingEnabled(Zend_Tool_Project_Profile $profile)
0051     {
0052         $profileSearchParams = array('testsDirectory');
0053         $testsDirectory = $profile->search($profileSearchParams);
0054 
0055         return $testsDirectory->isEnabled();
0056     }
0057 
0058     public static function isPHPUnitAvailable()
0059     {
0060         if (class_exists('PHPUnit_Runner_Version', false)) {
0061             return true;
0062         }
0063         
0064         $included = @include 'PHPUnit/Runner/Version.php';
0065         
0066         if ($included === false) {
0067             return false;
0068         } else {
0069             return true;
0070         }
0071     }
0072     
0073     /**
0074      * createApplicationResource()
0075      *
0076      * @param Zend_Tool_Project_Profile $profile
0077      * @param string $controllerName
0078      * @param string $actionName
0079      * @param string $moduleName
0080      * @return Zend_Tool_Project_Profile_Resource
0081      */
0082     public static function createApplicationResource(Zend_Tool_Project_Profile $profile, $controllerName, $actionName, $moduleName = null)
0083     {
0084         if (!is_string($controllerName)) {
0085             throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_View::createApplicationResource() expects \"controllerName\" is the name of a controller resource to create.');
0086         }
0087 
0088         if (!is_string($actionName)) {
0089             throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_View::createApplicationResource() expects \"actionName\" is the name of a controller resource to create.');
0090         }
0091 
0092         $testsDirectoryResource = $profile->search('testsDirectory');
0093 
0094         // parentOfController could either be application/ or a particular module folder, which is why we use this name
0095         if (($testParentOfControllerDirectoryResource = $testsDirectoryResource->search('testApplicationDirectory')) === false) {
0096             $testParentOfControllerDirectoryResource = $testsDirectoryResource->createResource('testApplicationDirectory');
0097         }
0098 
0099         if ($moduleName) {
0100             if (($testAppModulesDirectoryResource = $testParentOfControllerDirectoryResource->search('testApplicationModulesDirectory')) === false) {
0101                 $testAppModulesDirectoryResource = $testParentOfControllerDirectoryResource->createResource('testApplicationModulesDirectory');
0102             }
0103             
0104             if (($testAppModuleDirectoryResource = $testAppModulesDirectoryResource->search(array('testApplicationModuleDirectory' => array('forModuleName' => $moduleName)))) === false) {
0105                 $testAppModuleDirectoryResource = $testAppModulesDirectoryResource->createResource('testApplicationModuleDirectory', array('forModuleName' => $moduleName));
0106             }
0107             
0108             $testParentOfControllerDirectoryResource = $testAppModuleDirectoryResource;
0109         }
0110 
0111         if (($testAppControllerDirectoryResource = $testParentOfControllerDirectoryResource->search('testApplicationControllerDirectory', 'testApplicationModuleDirectory')) === false) {
0112             $testAppControllerDirectoryResource = $testParentOfControllerDirectoryResource->createResource('testApplicationControllerDirectory');
0113         }
0114 
0115         if (($testAppControllerFileResource = $testAppControllerDirectoryResource->search(array('testApplicationControllerFile' => array('forControllerName' => $controllerName)))) === false) {
0116             $testAppControllerFileResource = $testAppControllerDirectoryResource->createResource('testApplicationControllerFile', array('forControllerName' => $controllerName));
0117         }
0118         
0119         return $testAppControllerFileResource->createResource('testApplicationActionMethod', array('forActionName' => $actionName));
0120     }
0121 
0122     /**
0123      * createLibraryResource()
0124      *
0125      * @param Zend_Tool_Project_Profile $profile
0126      * @param string $libraryClassName
0127      * @return Zend_Tool_Project_Profile_Resource
0128      */
0129     public static function createLibraryResource(Zend_Tool_Project_Profile $profile, $libraryClassName)
0130     {
0131         $testLibraryDirectoryResource = $profile->search(array('TestsDirectory', 'TestLibraryDirectory'));
0132 
0133 
0134         $fsParts = explode('_', $libraryClassName);
0135 
0136         $currentDirectoryResource = $testLibraryDirectoryResource;
0137 
0138         while ($nameOrNamespacePart = array_shift($fsParts)) {
0139 
0140             if (count($fsParts) > 0) {
0141 
0142                 if (($libraryDirectoryResource = $currentDirectoryResource->search(array('TestLibraryNamespaceDirectory' => array('namespaceName' => $nameOrNamespacePart)))) === false) {
0143                     $currentDirectoryResource = $currentDirectoryResource->createResource('TestLibraryNamespaceDirectory', array('namespaceName' => $nameOrNamespacePart));
0144                 } else {
0145                     $currentDirectoryResource = $libraryDirectoryResource;
0146                 }
0147 
0148             } else {
0149 
0150                 if (($libraryFileResource = $currentDirectoryResource->search(array('TestLibraryFile' => array('forClassName' => $libraryClassName)))) === false) {
0151                     $libraryFileResource = $currentDirectoryResource->createResource('TestLibraryFile', array('forClassName' => $libraryClassName));
0152                 }
0153 
0154             }
0155 
0156         }
0157 
0158         return $libraryFileResource;
0159     }
0160 
0161     public function enable()
0162     {
0163 
0164     }
0165 
0166     public function disable()
0167     {
0168 
0169     }
0170 
0171     /**
0172      * create()
0173      *
0174      * @param string $libraryClassName
0175      */
0176     public function create($libraryClassName)
0177     {
0178         $profile = $this->_loadProfile();
0179 
0180         if (!self::isTestingEnabled($profile)) {
0181             $this->_registry->getResponse()->appendContent('Testing is not enabled for this project.');
0182         }
0183 
0184         $testLibraryResource = self::createLibraryResource($profile, $libraryClassName);
0185 
0186         $response = $this->_registry->getResponse();
0187 
0188         if ($this->_registry->getRequest()->isPretend()) {
0189             $response->appendContent('Would create a library stub in location ' . $testLibraryResource->getContext()->getPath());
0190         } else {
0191             $response->appendContent('Creating a library stub in location ' . $testLibraryResource->getContext()->getPath());
0192             $testLibraryResource->create();
0193             $this->_storeProfile();
0194         }
0195 
0196     }
0197 
0198 }