PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

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