PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/scripts/builder/vcs/svn.php

http://github.com/mageekguy/atoum
PHP | 354 lines | 338 code | 16 blank | 0 comment | 1 complexity | 42686dbffbb850f5a14321f4221e55be MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\scripts\builder\vcs;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\mock,
  6. mageekguy\atoum\mock\stream,
  7. mageekguy\atoum\scripts\builder\vcs
  8. ;
  9. require_once __DIR__ . '/../../../../runner.php';
  10. class svn extends atoum\test
  11. {
  12. public function beforeTestMethod($testMethod)
  13. {
  14. if (extension_loaded('svn') === false)
  15. {
  16. define('SVN_REVISION_HEAD', -1);
  17. define('PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS', 1);
  18. define('SVN_AUTH_PARAM_DEFAULT_USERNAME', 2);
  19. define('SVN_AUTH_PARAM_DEFAULT_PASSWORD', 3);
  20. }
  21. }
  22. public function testClass()
  23. {
  24. $this->testedClass->isSubclassOf('mageekguy\atoum\scripts\builder\vcs');
  25. }
  26. public function test__construct()
  27. {
  28. $this
  29. ->if($this->newTestedInstance)
  30. ->then
  31. ->object($this->testedInstance->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  32. ->variable($this->testedInstance->getRepositoryUrl())->isNull()
  33. ->if($this->newTestedInstance($adapter = new atoum\adapter()))
  34. ->then
  35. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  36. ->variable($this->testedInstance->getRepositoryUrl())->isNull()
  37. ;
  38. }
  39. public function testSetAdapter()
  40. {
  41. $this
  42. ->if($adapter = new atoum\test\adapter())
  43. ->and($adapter->extension_loaded = true)
  44. ->and($this->newTestedInstance($adapter))
  45. ->then
  46. ->object($this->testedInstance->setAdapter($adapter = new atoum\adapter()))->isTestedInstance
  47. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  48. ;
  49. }
  50. public function testSetRepositoryUrl()
  51. {
  52. $this
  53. ->if($adapter = new atoum\test\adapter())
  54. ->and($adapter->extension_loaded = true)
  55. ->and($this->newTestedInstance($adapter))
  56. ->then
  57. ->object($this->testedInstance->setRepositoryUrl($url = uniqid()))->isTestedInstance
  58. ->string($this->testedInstance->getRepositoryUrl())->isEqualTo($url)
  59. ->object($this->testedInstance->setRepositoryUrl($url = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isTestedInstance
  60. ->string($this->testedInstance->getRepositoryUrl())->isEqualTo((string) $url)
  61. ;
  62. }
  63. public function testSetRevision()
  64. {
  65. $this
  66. ->if($adapter = new atoum\test\adapter())
  67. ->and($adapter->extension_loaded = true)
  68. ->and($this->newTestedInstance($adapter))
  69. ->then
  70. ->object($this->testedInstance->setRevision($revisionNumber = rand(1, PHP_INT_MAX)))->isTestedInstance
  71. ->integer($this->testedInstance->getRevision())->isEqualTo($revisionNumber)
  72. ->object($this->testedInstance->setRevision($revisionNumber = uniqid()))->isTestedInstance
  73. ->integer($this->testedInstance->getRevision())->isEqualTo((int) $revisionNumber)
  74. ;
  75. }
  76. public function testSetUsername()
  77. {
  78. $this
  79. ->if($adapter = new atoum\test\adapter())
  80. ->and($adapter->extension_loaded = true)
  81. ->and($this->newTestedInstance($adapter))
  82. ->then
  83. ->object($this->testedInstance->setUsername($username = uniqid()))->isTestedInstance
  84. ->string($this->testedInstance->getUsername())->isEqualTo($username)
  85. ->object($this->testedInstance->setUsername($username = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isTestedInstance
  86. ->string($this->testedInstance->getUsername())->isEqualTo((string) $username)
  87. ;
  88. }
  89. public function testSetPassword()
  90. {
  91. $this
  92. ->if(
  93. $adapter = new atoum\test\adapter(),
  94. $adapter->extension_loaded = true,
  95. $this->newTestedInstance($adapter)
  96. )
  97. ->then
  98. ->object($this->testedInstance->setPassword($password = uniqid()))->isTestedInstance
  99. ->string($this->testedInstance->getPassword())->isEqualTo($password)
  100. ->object($this->testedInstance->setPassword($password = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isTestedInstance
  101. ->string($this->testedInstance->getPassword())->isEqualTo((string) $password)
  102. ;
  103. }
  104. public function testGetWorkingDirectoryIterator()
  105. {
  106. $this
  107. ->if(
  108. $adapter = new atoum\test\adapter(),
  109. $adapter->extension_loaded = true,
  110. $this->newTestedInstance($adapter),
  111. $this->testedInstance->setWorkingDirectory(__DIR__)
  112. )
  113. ->then
  114. ->object($recursiveIteratorIterator = $this->testedInstance->getWorkingDirectoryIterator())->isInstanceOf('recursiveIteratorIterator')
  115. ->object($recursiveDirectoryIterator = $recursiveIteratorIterator->getInnerIterator())->isInstanceOf('recursiveDirectoryIterator')
  116. ->string($recursiveDirectoryIterator->current()->getPathInfo()->getPathname())->isEqualTo(__DIR__)
  117. ;
  118. }
  119. public function testGetNextRevisions()
  120. {
  121. $this
  122. ->if(
  123. $adapter = new atoum\test\adapter(),
  124. $adapter->extension_loaded = true,
  125. $adapter->svn_auth_set_parameter = function() {},
  126. $svn = $this->newTestedInstance($adapter)
  127. )
  128. ->then
  129. ->exception(function() use ($svn) {
  130. $svn->getNextRevisions();
  131. }
  132. )
  133. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  134. ->hasMessage('Unable to get logs, repository url is undefined')
  135. ->adapter($adapter)
  136. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->never()
  137. ->call('svn_log')->never()
  138. ->if(
  139. $this->testedInstance->setRepositoryUrl($repositoryUrl = uniqid()),
  140. $adapter->svn_auth_set_parameter = function() {},
  141. $adapter->svn_log = array(),
  142. $adapter->resetCalls()
  143. )
  144. ->then
  145. ->array($this->testedInstance->getNextRevisions())->isEmpty()
  146. ->adapter($adapter)
  147. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()
  148. ->call('svn_log')->withArguments($repositoryUrl, 1, SVN_REVISION_HEAD)->once()
  149. ->if(
  150. $this->testedInstance->setRevision($revision = rand(1, PHP_INT_MAX)),
  151. $adapter->resetCalls()
  152. )
  153. ->then
  154. ->array($this->testedInstance->getNextRevisions())->isEmpty()
  155. ->adapter($adapter)
  156. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()
  157. ->call('svn_log')->withArguments($repositoryUrl, $revision, SVN_REVISION_HEAD)->once()
  158. ->if(
  159. $adapter->resetCalls(),
  160. $adapter->svn_log = array(uniqid() => uniqid())
  161. )
  162. ->then
  163. ->array($this->testedInstance->getNextRevisions())->isEmpty()
  164. ->adapter($adapter)
  165. ->call('svn_log')->withArguments($repositoryUrl, $revision, SVN_REVISION_HEAD)->once()
  166. ->if(
  167. $adapter->resetCalls(),
  168. $adapter->svn_log = array(
  169. array('rev' => $revision1 = uniqid()),
  170. array('rev' => $revision2 = uniqid()),
  171. array('rev' => $revision3 = uniqid())
  172. )
  173. )
  174. ->then
  175. ->array($this->testedInstance->getNextRevisions())->isEqualTo(array($revision1, $revision2, $revision3))
  176. ->adapter($adapter)->call('svn_log')->withArguments($repositoryUrl, $revision, SVN_REVISION_HEAD)->once()
  177. ;
  178. }
  179. public function testSetExportDirectory()
  180. {
  181. $this
  182. ->if(
  183. $adapter = new atoum\test\adapter(),
  184. $adapter->extension_loaded = true,
  185. $svn = new \mock\mageekguy\atoum\scripts\builder\vcs\svn($adapter)
  186. )
  187. ->then
  188. ->object($svn->setWorkingDirectory($workingDirectory = uniqid()))->isIdenticalTo($svn)
  189. ->string($svn->getWorkingDirectory())->isEqualTo($workingDirectory)
  190. ->object($svn->setWorkingDirectory($workingDirectory = rand(1, PHP_INT_MAX)))->isIdenticalTo($svn)
  191. ->string($svn->getWorkingDirectory())->isIdenticalTo((string) $workingDirectory)
  192. ;
  193. }
  194. public function testExportRepository()
  195. {
  196. $this
  197. ->if(
  198. $adapter = new atoum\test\adapter(),
  199. $adapter->extension_loaded = true,
  200. $svn = new \mock\mageekguy\atoum\scripts\builder\vcs\svn($adapter),
  201. $svn->getMockController()->cleanWorkingDirectory = $svn
  202. )
  203. ->then
  204. ->exception(function() use ($svn) {
  205. $svn->exportRepository(uniqid());
  206. }
  207. )
  208. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  209. ->hasMessage('Unable to export repository, repository url is undefined')
  210. ->adapter($adapter)
  211. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->never()
  212. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->never()
  213. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->never()
  214. ->call('svn_checkout')->never()
  215. ->if(
  216. $svn
  217. ->setRepositoryUrl($repositoryUrl = uniqid())
  218. ->setWorkingDirectory($workingDirectory = __DIR__),
  219. $adapter->resetCalls(),
  220. $adapter->svn_checkout = false,
  221. $adapter->svn_auth_set_parameter = function() {}
  222. )
  223. ->then
  224. ->exception(function() use ($svn) {
  225. $svn->exportRepository();
  226. }
  227. )
  228. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  229. ->hasMessage('Unable to checkout repository \'' . $repositoryUrl . '\' in directory \'' . $workingDirectory . '\'')
  230. ->adapter($adapter)
  231. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()
  232. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->never()
  233. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->never()
  234. ->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()
  235. ->mock($svn)
  236. ->call('cleanWorkingDirectory')->once()
  237. ->if(
  238. $svn
  239. ->setUsername(uniqid())
  240. ->getMockController()->resetCalls(),
  241. $adapter->resetCalls()
  242. )
  243. ->then
  244. ->exception(function() use ($svn) {
  245. $svn->exportRepository();
  246. }
  247. )
  248. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  249. ->hasMessage('Unable to checkout repository \'' . $repositoryUrl . '\' in directory \'' . $workingDirectory . '\'')
  250. ->adapter($adapter)
  251. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()
  252. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->once()
  253. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->never()
  254. ->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()
  255. ->mock($svn)
  256. ->call('cleanWorkingDirectory')->once()
  257. ->if(
  258. $svn
  259. ->setPassword(uniqid())
  260. ->getMockController()->resetCalls(),
  261. $adapter->resetCalls()
  262. )
  263. ->then
  264. ->exception(function() use ($svn) {
  265. $svn->exportRepository();
  266. }
  267. )
  268. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  269. ->hasMessage('Unable to checkout repository \'' . $repositoryUrl . '\' in directory \'' . $workingDirectory . '\'')
  270. ->adapter($adapter)
  271. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()
  272. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->once()
  273. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->once()
  274. ->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()
  275. ->mock($svn)
  276. ->call('cleanWorkingDirectory')->once()
  277. ->if(
  278. $svn->getMockController()->resetCalls(),
  279. $adapter->svn_checkout = true,
  280. $adapter->resetCalls()
  281. )
  282. ->then
  283. ->object($svn->exportRepository())->isIdenticalTo($svn)
  284. ->adapter($adapter)
  285. ->call('svn_auth_set_parameter')->withArguments(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true)->once()
  286. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn->getUsername())->once()
  287. ->call('svn_auth_set_parameter')->withArguments(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn->getPassword())->once()
  288. ->call('svn_checkout')->withArguments($svn->getRepositoryUrl(), $workingDirectory, $svn->getRevision())->once()
  289. ->mock($svn)
  290. ->call('cleanWorkingDirectory')->once()
  291. ;
  292. }
  293. public function testCleanWorkingDirectory()
  294. {
  295. $this
  296. ->if($adapter = new atoum\test\adapter())
  297. ->and($adapter->extension_loaded = true)
  298. ->and($adapter->unlink = function() {})
  299. ->and($adapter->rmdir = function() {})
  300. ->and($workingDirectory = stream::get('workingDirectory'))
  301. ->and($workingDirectory->opendir = true)
  302. ->and($workingDirectory->readdir[1] = $aDirectory = stream::getSubStream($workingDirectory))
  303. ->and($aDirectory->opendir = true)
  304. ->and($aDirectory->readdir[1] = $firstFile = stream::getSubStream($aDirectory))
  305. ->and($firstFile->unlink = true)
  306. ->and($aDirectory->readdir[2] = $secondFile = stream::getSubStream($aDirectory))
  307. ->and($secondFile->unlink = true)
  308. ->and($aDirectory->readdir[3] = false)
  309. ->and($workingDirectory->readdir[2] = $emptyDirectory = stream::getSubStream($workingDirectory))
  310. ->and($emptyDirectory->opendir = true)
  311. ->and($emptyDirectory->readdir[1] = false)
  312. ->and($workingDirectory->readdir[3] = $anOtherDirectory = stream::getSubStream($workingDirectory))
  313. ->and($anOtherDirectory->opendir = true)
  314. ->and($anOtherDirectory->readdir[1] = $anOtherFirstFile = stream::getSubStream($anOtherDirectory))
  315. ->and($anOtherFirstFile->unlink = true)
  316. ->and($anOtherDirectory->readdir[2] = $anOtherSecondFile = stream::getSubStream($anOtherDirectory))
  317. ->and($anOtherSecondFile->unlink = true)
  318. ->and($anOtherDirectory->readdir[3] = false)
  319. ->and($workingDirectory->readdir[4] = $aFile = stream::getSubStream($workingDirectory))
  320. ->and($aFile->unlink = true)
  321. ->and($workingDirectory->readdir[5] = false)
  322. ->and($svn = new \mock\mageekguy\atoum\scripts\builder\vcs\svn($adapter, $svnController = new mock\controller()))
  323. ->and($svn->setWorkingDirectory('atoum://workingDirectory'))
  324. ->then
  325. ->object($svn->cleanWorkingDirectory())->isIdenticalTo($svn)
  326. ->adapter($adapter)
  327. ->call('unlink')->withArguments((string) $firstFile)->once()
  328. ->call('unlink')->withArguments((string) $secondFile)->once()
  329. ->call('rmdir')->withArguments((string) $aDirectory)->once()
  330. ->call('rmdir')->withArguments((string) $emptyDirectory)->once()
  331. ->call('unlink')->withArguments((string) $anOtherFirstFile)->once()
  332. ->call('unlink')->withArguments((string) $anOtherSecondFile)->once()
  333. ->call('rmdir')->withArguments((string) $anOtherDirectory)->once()
  334. ->call('unlink')->withArguments((string) $aFile)->once()
  335. ->call('rmdir')->withArguments((string) $workingDirectory)->never()
  336. ;
  337. }
  338. }