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

https://bitbucket.org/Ebozavrik/test-application · PHP · 201 lines · 96 code · 42 blank · 63 comment · 20 complexity · b97eca54e0915889b3742bd382bded3a 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. *
  44. * @return bool
  45. */
  46. public static function isTestingEnabled (Zend_Tool_Project_Profile $profile)
  47. {
  48. $profileSearchParams = array( 'testsDirectory' );
  49. $testsDirectory = $profile->search($profileSearchParams);
  50. return $testsDirectory->isEnabled();
  51. }
  52. public static function isPHPUnitAvailable ()
  53. {
  54. if (class_exists('PHPUnit_Runner_Version', false)) {
  55. return true;
  56. }
  57. $included = @include 'PHPUnit/Runner/Version.php';
  58. if ($included === false) {
  59. return false;
  60. } else {
  61. return true;
  62. }
  63. }
  64. /**
  65. * createApplicationResource()
  66. *
  67. * @param Zend_Tool_Project_Profile $profile
  68. * @param string $controllerName
  69. * @param string $actionName
  70. * @param string $moduleName
  71. *
  72. * @return Zend_Tool_Project_Profile_Resource
  73. */
  74. public static function createApplicationResource (Zend_Tool_Project_Profile $profile, $controllerName, $actionName, $moduleName = null)
  75. {
  76. if (!is_string($controllerName)) {
  77. throw new Zend_Tool_Project_Provider_Exception( 'Zend_Tool_Project_Provider_View::createApplicationResource() expects \"controllerName\" is the name of a controller resource to create.' );
  78. }
  79. if (!is_string($actionName)) {
  80. throw new Zend_Tool_Project_Provider_Exception( 'Zend_Tool_Project_Provider_View::createApplicationResource() expects \"actionName\" is the name of a controller resource to create.' );
  81. }
  82. $testsDirectoryResource = $profile->search('testsDirectory');
  83. // parentOfController could either be application/ or a particular module folder, which is why we use this name
  84. if (( $testParentOfControllerDirectoryResource = $testsDirectoryResource->search('testApplicationDirectory') ) === false) {
  85. $testParentOfControllerDirectoryResource = $testsDirectoryResource->createResource('testApplicationDirectory');
  86. }
  87. if ($moduleName) {
  88. if (( $testAppModulesDirectoryResource = $testParentOfControllerDirectoryResource->search('testApplicationModulesDirectory') ) === false) {
  89. $testAppModulesDirectoryResource = $testParentOfControllerDirectoryResource->createResource('testApplicationModulesDirectory');
  90. }
  91. if (( $testAppModuleDirectoryResource = $testAppModulesDirectoryResource->search(array( 'testApplicationModuleDirectory' => array( 'forModuleName' => $moduleName ) )) ) === false) {
  92. $testAppModuleDirectoryResource = $testAppModulesDirectoryResource->createResource('testApplicationModuleDirectory', array( 'forModuleName' => $moduleName ));
  93. }
  94. $testParentOfControllerDirectoryResource = $testAppModuleDirectoryResource;
  95. }
  96. if (( $testAppControllerDirectoryResource = $testParentOfControllerDirectoryResource->search('testApplicationControllerDirectory', 'testApplicationModuleDirectory') ) === false) {
  97. $testAppControllerDirectoryResource = $testParentOfControllerDirectoryResource->createResource('testApplicationControllerDirectory');
  98. }
  99. if (( $testAppControllerFileResource = $testAppControllerDirectoryResource->search(array( 'testApplicationControllerFile' => array( 'forControllerName' => $controllerName ) )) ) === false) {
  100. $testAppControllerFileResource = $testAppControllerDirectoryResource->createResource('testApplicationControllerFile', array( 'forControllerName' => $controllerName ));
  101. }
  102. return $testAppControllerFileResource->createResource('testApplicationActionMethod', array( 'forActionName' => $actionName ));
  103. }
  104. /**
  105. * createLibraryResource()
  106. *
  107. * @param Zend_Tool_Project_Profile $profile
  108. * @param string $libraryClassName
  109. *
  110. * @return Zend_Tool_Project_Profile_Resource
  111. */
  112. public static function createLibraryResource (Zend_Tool_Project_Profile $profile, $libraryClassName)
  113. {
  114. $testLibraryDirectoryResource = $profile->search(array( 'TestsDirectory', 'TestLibraryDirectory' ));
  115. $fsParts = explode('_', $libraryClassName);
  116. $currentDirectoryResource = $testLibraryDirectoryResource;
  117. while ($nameOrNamespacePart = array_shift($fsParts)) {
  118. if (count($fsParts) > 0) {
  119. if (( $libraryDirectoryResource = $currentDirectoryResource->search(array( 'TestLibraryNamespaceDirectory' => array( 'namespaceName' => $nameOrNamespacePart ) )) ) === false) {
  120. $currentDirectoryResource = $currentDirectoryResource->createResource('TestLibraryNamespaceDirectory', array( 'namespaceName' => $nameOrNamespacePart ));
  121. } else {
  122. $currentDirectoryResource = $libraryDirectoryResource;
  123. }
  124. } else {
  125. if (( $libraryFileResource = $currentDirectoryResource->search(array( 'TestLibraryFile' => array( 'forClassName' => $libraryClassName ) )) ) === false) {
  126. $libraryFileResource = $currentDirectoryResource->createResource('TestLibraryFile', array( 'forClassName' => $libraryClassName ));
  127. }
  128. }
  129. }
  130. return $libraryFileResource;
  131. }
  132. public function enable ()
  133. {
  134. }
  135. public function disable ()
  136. {
  137. }
  138. /**
  139. * create()
  140. *
  141. * @param string $libraryClassName
  142. */
  143. public function create ($libraryClassName)
  144. {
  145. $profile = $this->_loadProfile();
  146. if (!self::isTestingEnabled($profile)) {
  147. $this->_registry->getResponse()->appendContent('Testing is not enabled for this project.');
  148. }
  149. $testLibraryResource = self::createLibraryResource($profile, $libraryClassName);
  150. $response = $this->_registry->getResponse();
  151. if ($this->_registry->getRequest()->isPretend()) {
  152. $response->appendContent('Would create a library stub in location ' . $testLibraryResource->getContext()->getPath());
  153. } else {
  154. $response->appendContent('Creating a library stub in location ' . $testLibraryResource->getContext()->getPath());
  155. $testLibraryResource->create();
  156. $this->_storeProfile();
  157. }
  158. }
  159. }