/ZendFramework/library/Zend/Tool/Project/Provider/Test.php

https://bitbucket.org/Dal-Papa/is-340-publish-base · PHP · 198 lines · 96 code · 42 blank · 60 comment · 20 complexity · 10d61329677e600c2c703afa56604b86 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: Test.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Provider_Abstract
  24. */
  25. require_once 'Zend/Tool/Project/Provider/Abstract.php';
  26. /**
  27. * @see Zend_Tool_Project_Provider_Exception
  28. */
  29. require_once 'Zend/Tool/Project/Provider/Exception.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Tool_Project_Provider_Test extends Zend_Tool_Project_Provider_Abstract
  37. {
  38. protected $_specialties = array('Application', 'Library');
  39. /**
  40. * isTestingEnabled()
  41. *
  42. * @param Zend_Tool_Project_Profile $profile
  43. * @return bool
  44. */
  45. public static function isTestingEnabled(Zend_Tool_Project_Profile $profile)
  46. {
  47. $profileSearchParams = array('testsDirectory');
  48. $testsDirectory = $profile->search($profileSearchParams);
  49. return $testsDirectory->isEnabled();
  50. }
  51. public static function isPHPUnitAvailable()
  52. {
  53. if (class_exists('PHPUnit_Runner_Version', false)) {
  54. return true;
  55. }
  56. $included = @include 'PHPUnit/Runner/Version.php';
  57. if ($included === false) {
  58. return false;
  59. } else {
  60. return true;
  61. }
  62. }
  63. /**
  64. * createApplicationResource()
  65. *
  66. * @param Zend_Tool_Project_Profile $profile
  67. * @param string $controllerName
  68. * @param string $actionName
  69. * @param string $moduleName
  70. * @return Zend_Tool_Project_Profile_Resource
  71. */
  72. public static function createApplicationResource(Zend_Tool_Project_Profile $profile, $controllerName, $actionName, $moduleName = null)
  73. {
  74. if (!is_string($controllerName)) {
  75. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_View::createApplicationResource() expects \"controllerName\" is the name of a controller resource to create.');
  76. }
  77. if (!is_string($actionName)) {
  78. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_View::createApplicationResource() expects \"actionName\" is the name of a controller resource to create.');
  79. }
  80. $testsDirectoryResource = $profile->search('testsDirectory');
  81. // parentOfController could either be application/ or a particular module folder, which is why we use this name
  82. if (($testParentOfControllerDirectoryResource = $testsDirectoryResource->search('testApplicationDirectory')) === false) {
  83. $testParentOfControllerDirectoryResource = $testsDirectoryResource->createResource('testApplicationDirectory');
  84. }
  85. if ($moduleName) {
  86. if (($testAppModulesDirectoryResource = $testParentOfControllerDirectoryResource->search('testApplicationModulesDirectory')) === false) {
  87. $testAppModulesDirectoryResource = $testParentOfControllerDirectoryResource->createResource('testApplicationModulesDirectory');
  88. }
  89. if (($testAppModuleDirectoryResource = $testAppModulesDirectoryResource->search(array('testApplicationModuleDirectory' => array('forModuleName' => $moduleName)))) === false) {
  90. $testAppModuleDirectoryResource = $testAppModulesDirectoryResource->createResource('testApplicationModuleDirectory', array('forModuleName' => $moduleName));
  91. }
  92. $testParentOfControllerDirectoryResource = $testAppModuleDirectoryResource;
  93. }
  94. if (($testAppControllerDirectoryResource = $testParentOfControllerDirectoryResource->search('testApplicationControllerDirectory', 'testApplicationModuleDirectory')) === false) {
  95. $testAppControllerDirectoryResource = $testParentOfControllerDirectoryResource->createResource('testApplicationControllerDirectory');
  96. }
  97. if (($testAppControllerFileResource = $testAppControllerDirectoryResource->search(array('testApplicationControllerFile' => array('forControllerName' => $controllerName)))) === false) {
  98. $testAppControllerFileResource = $testAppControllerDirectoryResource->createResource('testApplicationControllerFile', array('forControllerName' => $controllerName));
  99. }
  100. return $testAppControllerFileResource->createResource('testApplicationActionMethod', array('forActionName' => $actionName));
  101. }
  102. /**
  103. * createLibraryResource()
  104. *
  105. * @param Zend_Tool_Project_Profile $profile
  106. * @param string $libraryClassName
  107. * @return Zend_Tool_Project_Profile_Resource
  108. */
  109. public static function createLibraryResource(Zend_Tool_Project_Profile $profile, $libraryClassName)
  110. {
  111. $testLibraryDirectoryResource = $profile->search(array('TestsDirectory', 'TestLibraryDirectory'));
  112. $fsParts = explode('_', $libraryClassName);
  113. $currentDirectoryResource = $testLibraryDirectoryResource;
  114. while ($nameOrNamespacePart = array_shift($fsParts)) {
  115. if (count($fsParts) > 0) {
  116. if (($libraryDirectoryResource = $currentDirectoryResource->search(array('TestLibraryNamespaceDirectory' => array('namespaceName' => $nameOrNamespacePart)))) === false) {
  117. $currentDirectoryResource = $currentDirectoryResource->createResource('TestLibraryNamespaceDirectory', array('namespaceName' => $nameOrNamespacePart));
  118. } else {
  119. $currentDirectoryResource = $libraryDirectoryResource;
  120. }
  121. } else {
  122. if (($libraryFileResource = $currentDirectoryResource->search(array('TestLibraryFile' => array('forClassName' => $libraryClassName)))) === false) {
  123. $libraryFileResource = $currentDirectoryResource->createResource('TestLibraryFile', array('forClassName' => $libraryClassName));
  124. }
  125. }
  126. }
  127. return $libraryFileResource;
  128. }
  129. public function enable()
  130. {
  131. }
  132. public function disable()
  133. {
  134. }
  135. /**
  136. * create()
  137. *
  138. * @param string $libraryClassName
  139. */
  140. public function create($libraryClassName)
  141. {
  142. $profile = $this->_loadProfile();
  143. if (!self::isTestingEnabled($profile)) {
  144. $this->_registry->getResponse()->appendContent('Testing is not enabled for this project.');
  145. }
  146. $testLibraryResource = self::createLibraryResource($profile, $libraryClassName);
  147. $response = $this->_registry->getResponse();
  148. if ($this->_registry->getRequest()->isPretend()) {
  149. $response->appendContent('Would create a library stub in location ' . $testLibraryResource->getContext()->getPath());
  150. } else {
  151. $response->appendContent('Creating a library stub in location ' . $testLibraryResource->getContext()->getPath());
  152. $testLibraryResource->create();
  153. $this->_storeProfile();
  154. }
  155. }
  156. }