PageRenderTime 74ms CodeModel.GetById 32ms RepoModel.GetById 3ms app.codeStats 1ms

/tests/Zend/Tool/Project/ProfileTest.php

https://github.com/EvanDotPro/zf1-mirror
PHP | 296 lines | 171 code | 64 blank | 61 comment | 4 complexity | 1a205a324ac78f9daef41ab99502772f 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 UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Tool/Project/Profile.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Tool
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. *
  30. * @group Zend_Tool
  31. * @group Zend_Tool_Framework
  32. * @group Zend_Tool_Framework_Action
  33. */
  34. class Zend_Tool_Project_ProfileTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected $_projectDirectory = null;
  37. protected $_projectProfileFile = null;
  38. /**
  39. * @var Zend_Tool_Project_Profile
  40. */
  41. protected $_standardProfileFromData = null;
  42. public function setup()
  43. {
  44. $this->_projectDirectory = dirname(__FILE__) . '/_files/project1/';
  45. $this->_projectProfileFile = dirname(__FILE__) . '/_files/.zfproject.xml.orig';
  46. $this->_removeProjectFiles();
  47. Zend_Tool_Project_Context_Repository::resetInstance();
  48. $contextRegistry = Zend_Tool_Project_Context_Repository::getInstance();
  49. $contextRegistry->addContextsFromDirectory(dirname(__FILE__) . '/../../../../library/Zend/Tool/Project/Context/Zf/', 'Zend_Tool_Project_Context_Zf_');
  50. $this->_standardProfileFromData = new Zend_Tool_Project_Profile();
  51. $this->_standardProfileFromData->setAttribute('profileData', file_get_contents($this->_projectProfileFile));
  52. $this->_standardProfileFromData->setAttribute('projectDirectory', $this->_projectDirectory);
  53. }
  54. public function teardown()
  55. {
  56. $this->_removeProjectFiles();
  57. }
  58. public function testAttibuteGettersAndSettersWork()
  59. {
  60. $profile = new Zend_Tool_Project_Profile(array('foo' => 'bar'));
  61. $profile->setAttributes(array('baz' => 'BAZ'));
  62. $profile->setAttribute('boof', 'foob');
  63. $this->assertEquals('foob', $profile->getAttribute('boof'));
  64. $this->assertContains('bar', $profile->getAttributes());
  65. $this->assertContains('BAZ', $profile->getAttributes());
  66. }
  67. public function testProfileLoadsFromExistingFileGivenProjectDirectory()
  68. {
  69. copy($this->_projectProfileFile, $this->_projectDirectory . '/.zfproject.xml');
  70. $profile = new Zend_Tool_Project_Profile();
  71. $profile->setAttribute('projectDirectory', $this->_projectDirectory);
  72. $profile->loadFromFile();
  73. // first item in here should be 'projectDirectory'
  74. $projectDirectoryResource = $profile->current();
  75. $this->assertEquals(1, count($profile));
  76. $this->assertEquals('Zend_Tool_Project_Profile_Resource', get_class($projectDirectoryResource));
  77. $this->assertEquals('Zend_Tool_Project_Context_System_ProjectDirectory', get_class($projectDirectoryResource->getContext()));
  78. }
  79. public function testProfileLoadsFromExistingFileGivenProfileFile()
  80. {
  81. $profile = new Zend_Tool_Project_Profile(array(
  82. 'projectProfileFile' => $this->_projectProfileFile,
  83. 'projectDirectory' => $this->_projectDirectory
  84. ));
  85. $profile->loadFromFile();
  86. $projectDirectoryResource = $profile->current();
  87. $this->assertEquals('Zend_Tool_Project_Profile_Resource', get_class($projectDirectoryResource));
  88. $this->assertEquals('Zend_Tool_Project_Context_System_ProjectDirectory', get_class($projectDirectoryResource->getContext()));
  89. }
  90. public function testProfileFromVariousSourcesIsLoadableFromFile()
  91. {
  92. $profile = new Zend_Tool_Project_Profile();
  93. // no options, should return false
  94. $this->assertFalse($profile->isLoadableFromFile());
  95. // invalid file path, should be false
  96. $profile->setAttribute('projectProfileFile', $this->_projectProfileFile . '.invalid-file');
  97. $this->assertFalse($profile->isLoadableFromFile());
  98. // valid file path, shoudl be true
  99. $profile->setAttribute('projectProfileFile', $this->_projectProfileFile);
  100. $this->assertTrue($profile->isLoadableFromFile());
  101. // just project directory
  102. $profile = new Zend_Tool_Project_Profile();
  103. // shoudl be false with non existent directory
  104. $profile->setAttribute('projectDirectory', $this->_projectDirectory . 'non-existent/dir/');
  105. $this->assertFalse($profile->isLoadableFromFile());
  106. // should return true for proper directory
  107. copy($this->_projectProfileFile, $this->_projectDirectory . '/.zfproject.xml');
  108. $profile->setAttribute('projectDirectory', $this->_projectDirectory);
  109. $this->assertTrue($profile->isLoadableFromFile());
  110. }
  111. public function testLoadFromDataIsSameAsLoadFromFile()
  112. {
  113. $profile = new Zend_Tool_Project_Profile(array('projectProfileFile' => $this->_projectProfileFile));
  114. $profile->setAttribute('projectDirectory', $this->_projectDirectory);
  115. $profile->loadFromFile();
  116. $profile2 = new Zend_Tool_Project_Profile();
  117. $profile2->setAttribute('profileData', file_get_contents($this->_projectProfileFile));
  118. $profile2->setAttribute('projectDirectory', $this->_projectDirectory);
  119. $profile2->loadFromData();
  120. $this->assertEquals($profile->__toString(), $profile2->__toString());
  121. }
  122. public function testProfileCanReturnStorageData()
  123. {
  124. $this->_standardProfileFromData->loadFromData();
  125. $expectedValue = '<?xml version="1.0"?><projectProfile> <projectDirectory> <projectProfileFile filesystemName=".zfproject.xml"/> <applicationDirectory classNamePrefix="Application_"> <configsDirectory> <applicationConfigFile type="ini"/> </configsDirectory> <controllersDirectory> <controllerFile controllerName="index"/> <controllerFile controllerName="error"/> </controllersDirectory> <layoutsDirectory enabled="false"/> <modelsDirectory/> <modulesDirectory enabled="false"/> <servicesDirectory enabled="false"/> <viewsDirectory> <viewScriptsDirectory> <viewControllerScriptsDirectory forControllerName="index"> <viewScriptFile scriptName="index"/> </viewControllerScriptsDirectory> </viewScriptsDirectory> <viewHelpersDirectory/> <viewFiltersDirectory enabled="false"/> </viewsDirectory> <bootstrapFile filesystemName="Bootstrap.php"/> </applicationDirectory> <dataDirectory enabled="false"> <cacheDirectory enabled="false"/> <searchIndexesDirectory enabled="false"/> <localesDirectory enabled="false"/> <logsDirectory enabled="false"/> <sessionsDirectory enabled="false"/> <uploadsDirectory enabled="false"/> </dataDirectory> <libraryDirectory> <zfStandardLibraryDirectory/> </libraryDirectory> <publicDirectory> <publicStylesheetsDirectory enabled="false"/> <publicScriptsDirectory enabled="false"/> <publicImagesDirectory enabled="false"/> <publicIndexFile filesystemName="index.php"/> <htaccessFile filesystemName=".htaccess"/> </publicDirectory> <projectProvidersDirectory enabled="false"/> </projectDirectory></projectProfile>';
  126. $this->assertEquals($expectedValue, str_replace(array("\r\n", "\n"), '', $this->_standardProfileFromData->storeToData()));
  127. }
  128. public function testProfileCanSaveStorageDataToFile()
  129. {
  130. $this->_standardProfileFromData->loadFromData();
  131. $this->_standardProfileFromData->setAttribute('projectProfileFile', $this->_projectDirectory . 'my-xml-file.xml');
  132. $this->_standardProfileFromData->storeToFile();
  133. $this->assertTrue(file_exists($this->_projectDirectory . 'my-xml-file.xml'));
  134. }
  135. public function testProfileCanFindResource()
  136. {
  137. $profile = new Zend_Tool_Project_Profile(array(
  138. 'projectProfileFile' => $this->_projectProfileFile,
  139. 'projectDirectory' => $this->_projectDirectory
  140. ));
  141. $profile->loadFromFile();
  142. $modelsDirectoryResource = $profile->search('modelsDirectory');
  143. $this->assertEquals('Zend_Tool_Project_Profile_Resource', get_class($modelsDirectoryResource));
  144. $this->assertEquals('Zend_Tool_Project_Context_Zf_ModelsDirectory', get_class($modelsDirectoryResource->getContext()));
  145. $publicIndexFile = $profile->search(array('publicDirectory', 'publicIndexFile'));
  146. $this->assertEquals('Zend_Tool_Project_Profile_Resource', get_class($publicIndexFile));
  147. $this->assertEquals('Zend_Tool_Project_Context_Zf_PublicIndexFile', get_class($publicIndexFile->getContext()));
  148. }
  149. public function testProfileCanRecursivelyCreateParentFirst()
  150. {
  151. $this->_standardProfileFromData->loadFromData();
  152. foreach ($this->_standardProfileFromData->getIterator() as $resource) {
  153. $resource->getContext()->create();
  154. }
  155. $this->assertTrue(file_exists($this->_projectDirectory . 'public/index.php'));
  156. }
  157. public function testProfileCanDelete()
  158. {
  159. $this->_standardProfileFromData->loadFromData();
  160. foreach ($this->_standardProfileFromData->getIterator() as $resource) {
  161. $resource->getContext()->create();
  162. }
  163. $this->assertTrue(file_exists($this->_projectDirectory . 'public/index.php'));
  164. $publicIndexFile = $this->_standardProfileFromData->search('publicIndexFile');
  165. $publicIndexFile->getContext()->delete();
  166. $this->assertFalse(file_exists($this->_projectDirectory . 'public/index.php'));
  167. $appConfigFile = $this->_standardProfileFromData->search('applicationConfigFile');
  168. $appConfigFile->getContext()->delete();
  169. $configsDirectory = $this->_standardProfileFromData->search('configsDirectory');
  170. $configsDirectory->getContext()->delete();
  171. $this->assertFalse(file_exists($this->_projectDirectory . 'application/configs'));
  172. }
  173. /**
  174. *
  175. * @expectedException Zend_Tool_Project_Exception
  176. */
  177. public function testProfileThrowsExceptionOnLoadFromData()
  178. {
  179. $profile = new Zend_Tool_Project_Profile();
  180. // missing data from attributes should throw exception here
  181. $profile->loadFromData();
  182. }
  183. /**
  184. *
  185. * @expectedException Zend_Tool_Project_Exception
  186. */
  187. public function testProfileThrowsExceptionOnLoadFromFile()
  188. {
  189. $profile = new Zend_Tool_Project_Profile();
  190. // missing file path or project path
  191. $profile->loadFromFile();
  192. }
  193. /**
  194. *
  195. * @expectedException Zend_Tool_Project_Exception
  196. */
  197. public function testProfileThrowsExceptionOnStoreToFile()
  198. {
  199. $profile = new Zend_Tool_Project_Profile();
  200. // missing file path or project path
  201. $profile->storeToFile();
  202. }
  203. /**
  204. *
  205. * @expectedException Zend_Tool_Project_Exception
  206. */
  207. public function testProfileThrowsExceptionOnLoadFromFileWithBadPathForProfileFile()
  208. {
  209. $profile = new Zend_Tool_Project_Profile();
  210. $profile->setAttribute('projectProfileFile', '/path/should/not/exist');
  211. // missing file path or project path
  212. $profile->loadFromFile();
  213. }
  214. protected function _removeProjectFiles()
  215. {
  216. $rdi = new RecursiveDirectoryIterator($this->_projectDirectory);
  217. foreach (new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST) as $dirIteratorItem) {
  218. $basename = $dirIteratorItem->getBasename();
  219. if (stristr($dirIteratorItem->getPathname(), '.svn')
  220. || '.' === $basename
  221. || '..' === $basename)
  222. {
  223. continue;
  224. }
  225. if ($dirIteratorItem->isDir()) {
  226. rmdir($dirIteratorItem->getPathname());
  227. } elseif ($dirIteratorItem->isFile()) {
  228. unlink($dirIteratorItem->getPathname());
  229. }
  230. }
  231. }
  232. }