PageRenderTime 59ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/scripts/git/pusher.php

https://github.com/agallou/atoum
PHP | 273 lines | 249 code | 24 blank | 0 comment | 0 complexity | 1305d48f2420cc479881318315351c2b MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\scripts\git;
  3. require __DIR__ . '/../../../runner.php';
  4. use
  5. atoum,
  6. atoum\scripts,
  7. atoum\cli\commands,
  8. atoum\scripts\git\pusher as testedClass
  9. ;
  10. class pusher extends atoum
  11. {
  12. public function testClass()
  13. {
  14. $this->testedClass->extends('atoum\script\configurable');
  15. }
  16. public function testClassConstants()
  17. {
  18. $this
  19. ->string(testedClass::defaultRemote)->isEqualTo('origin')
  20. ->string(testedClass::defaultTagFile)->isEqualTo('.tag')
  21. ->string(testedClass::versionPattern)->isEqualTo('$Rev: %s $')
  22. ;
  23. }
  24. public function test__construct()
  25. {
  26. $this
  27. ->if($pusher = new testedClass(__FILE__))
  28. ->then
  29. ->string($pusher->getRemote())->isEqualTo(testedClass::defaultRemote)
  30. ->string($pusher->getTagFile())->isEqualTo(__DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultTagFile)
  31. ->object($pusher->getTaggerEngine())->isEqualTo(new scripts\tagger\engine())
  32. ->string($pusher->getWorkingDirectory())->isEqualTo(getcwd())
  33. ->object($pusher->getGit())->isEqualTo(new commands\git())
  34. ;
  35. }
  36. public function testSetRemote()
  37. {
  38. $this
  39. ->if($pusher = new testedClass(__FILE__))
  40. ->then
  41. ->object($pusher->setRemote($remote = uniqid()))->isIdenticalTo($pusher)
  42. ->string($pusher->getRemote())->isEqualTo($remote)
  43. ->object($pusher->setRemote())->isIdenticalTo($pusher)
  44. ->string($pusher->getRemote())->isEqualTo(testedClass::defaultRemote)
  45. ;
  46. }
  47. public function testSetTagFile()
  48. {
  49. $this
  50. ->if($pusher = new testedClass(__FILE__))
  51. ->then
  52. ->object($pusher->setTagFile($tagFile = uniqid()))->isIdenticalTo($pusher)
  53. ->string($pusher->getTagFile())->isEqualTo($tagFile)
  54. ->object($pusher->setTagFile())->isIdenticalTo($pusher)
  55. ->string($pusher->getTagFile())->isEqualTo(__DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultTagFile)
  56. ;
  57. }
  58. public function testSetTaggerEngine()
  59. {
  60. $this
  61. ->if($pusher = new testedClass(__FILE__))
  62. ->then
  63. ->object($pusher->setTaggerEngine($taggerEngine = new scripts\tagger\engine()))->isIdenticalTo($pusher)
  64. ->object($pusher->getTaggerEngine())->isIdenticalTo($taggerEngine)
  65. ->object($pusher->setTaggerEngine())->isIdenticalTo($pusher)
  66. ->object($pusher->getTaggerEngine())
  67. ->isNotIdenticalTo($taggerEngine)
  68. ->isEqualTo(new scripts\tagger\engine())
  69. ;
  70. }
  71. public function testSetWorkingDirectory()
  72. {
  73. $this
  74. ->if($pusher = new testedClass(__FILE__))
  75. ->then
  76. ->object($pusher->setWorkingDirectory($workingDirectory = uniqid()))->isIdenticalTo($pusher)
  77. ->string($pusher->getWorkingDirectory())->isEqualTo($workingDirectory)
  78. ->object($pusher->setWorkingDirectory())->isIdenticalTo($pusher)
  79. ->string($pusher->getWorkingDirectory())->isEqualTo(getcwd())
  80. ;
  81. }
  82. public function testSetGit()
  83. {
  84. $this
  85. ->if($pusher = new testedClass(__FILE__))
  86. ->then
  87. ->object($pusher->setGit($git = new commands\git()))->isIdenticalTo($pusher)
  88. ->object($pusher->getGit())->isIdenticalTo($git)
  89. ->object($pusher->setGit())->isIdenticalTo($pusher)
  90. ->object($pusher->getGit())
  91. ->isNotIdenticalTo($git)
  92. ->isEqualTo(new commands\git())
  93. ;
  94. }
  95. public function testRun()
  96. {
  97. $this
  98. ->given(
  99. $pusher = new testedClass(__FILE__),
  100. $pusher->setTaggerEngine($taggerEngine = new \mock\mageekguy\atoum\scripts\tagger\engine()),
  101. $pusher->setGit($git = new \mock\mageekguy\atoum\cli\commands\git()),
  102. $pusher->setErrorWriter($errorWriter = new \mock\mageekguy\atoum\writers\std\err()),
  103. $pusher->setInfoWriter($infoWriter = new \mock\mageekguy\atoum\writers\std\out()),
  104. $this->calling($infoWriter)->write = $infoWriter
  105. )
  106. ->assert('Pusher should write error if tag file is not readable')
  107. ->if(
  108. $this->calling($errorWriter)->write = $errorWriter,
  109. $this->function->file_get_contents = false
  110. )
  111. ->then
  112. ->object($pusher->run())->isIdenticalTo($pusher)
  113. ->mock($errorWriter)->call('write')->withArguments('Unable to read \'' . $pusher->getTagFile() . '\'')->once()
  114. ->assert('Pusher should write error if tag file is not writable')
  115. ->if(
  116. $this->calling($errorWriter)->write = $errorWriter,
  117. $this->function->file_put_contents = false,
  118. $this->function->file_get_contents = '0.0.0'
  119. )
  120. ->then
  121. ->object($pusher->run())->isIdenticalTo($pusher)
  122. ->mock($errorWriter)->call('write')->withArguments('Unable to write in \'' . $pusher->getTagFile() . '\'')->once()
  123. ->assert('Pusher should tag code and commit it if tag file is writable')
  124. ->if(
  125. $this->function->file_put_contents = function($path, $data) { return strlen($data); },
  126. $this->calling($taggerEngine)->tagVersion->doesNothing(),
  127. $this->calling($git)->addAllAndCommit = $git,
  128. $this->calling($git)->checkoutAllFiles = $git,
  129. $this->calling($git)->createTag = $git,
  130. $this->calling($git)->push = $git,
  131. $this->calling($git)->forcePush = $git,
  132. $this->calling($git)->pushTag = $git,
  133. $this->calling($git)->resetHardTo = $git,
  134. $this->calling($git)->deleteLocalTag = $git
  135. )
  136. ->then
  137. ->object($pusher->run())->isIdenticalTo($pusher)
  138. ->function('file_put_contents')->wasCalledWithArguments($pusher->getTagFile(), '0.0.1')->once()
  139. ->mock($taggerEngine)
  140. ->call('tagVersion')
  141. ->before($this->mock($git)
  142. ->call('addAllAndCommit')->withArguments('Set version to 0.0.1.')
  143. ->before($this->mock($git)
  144. ->call('createTag')->withArguments('0.0.1')
  145. ->before($this->mock($git)
  146. ->call('push')->withArguments($pusher->getRemote())
  147. ->once()
  148. )
  149. ->before($this->mock($git)
  150. ->call('pushTag')->withArguments('0.0.1', $pusher->getRemote())
  151. ->once()
  152. )
  153. ->once()
  154. )
  155. ->once())
  156. ->after($this->mock($taggerEngine)
  157. ->call('setSrcDirectory')->withArguments($pusher->getWorkingDirectory())
  158. ->once()
  159. )
  160. ->after($this->mock($taggerEngine)
  161. ->call('setVersion')->withArguments('$Rev:' . ' 0.0.1 $') // Don't remove concatenation operator to avoid tagger replace the string.
  162. ->once()
  163. )
  164. ->once()
  165. ->call('tagVersion')
  166. ->before($this->mock($git)
  167. ->call('addAllAndCommit')->withArguments('Set version to DEVELOPMENT-0.0.1.')->once())
  168. ->after($this->mock($taggerEngine)->call('setSrcDirectory')->withArguments($pusher->getWorkingDirectory())->once())
  169. ->after($this->mock($taggerEngine)->call('setVersion')->withArguments('$Rev:' . ' DEVELOPMENT-0.0.1 $')->once()) // Don't remove concatenation operator to avoid tagger replace the string.
  170. ->once()
  171. ->if($pusher->tagPatchVersion())
  172. ->then
  173. ->object($pusher->run())->isIdenticalTo($pusher)
  174. ->function('file_put_contents')->wasCalledWithArguments($pusher->getTagFile(), '0.0.1')->twice()
  175. ->if($pusher->tagMinorVersion())
  176. ->then
  177. ->object($pusher->run())->isIdenticalTo($pusher)
  178. ->function('file_put_contents')->wasCalledWithArguments($pusher->getTagFile(), '0.1.0')->once()
  179. ->if($pusher->tagMajorVersion())
  180. ->then
  181. ->object($pusher->run())->isIdenticalTo($pusher)
  182. ->function('file_put_contents')->wasCalledWithArguments($pusher->getTagFile(), '1.0.0')->once()
  183. ->assert('Pusher should write error if pushing tag failed and should try to reset repository')
  184. ->if(
  185. $pusher->tagPatchVersion(),
  186. $this->calling($git)->pushTag->throw = $exception = new \exception(uniqid())
  187. )
  188. ->then
  189. ->object($pusher->run())->isIdenticalTo($pusher)
  190. ->mock($errorWriter)->call('write')->withArguments($exception->getMessage())->once()
  191. ->mock($git)
  192. ->call('resetHardTo')->withArguments('HEAD~2')->once()
  193. ->call('deleteLocalTag')->withArguments('0.0.1')->once()
  194. ->assert('Pusher should write error if pushing commit failed and should try to reset repository')
  195. ->if(
  196. $this->calling($git)->push->throw = $exception = new \exception(uniqid())
  197. )
  198. ->then
  199. ->object($pusher->run())->isIdenticalTo($pusher)
  200. ->mock($errorWriter)->call('write')->withArguments($exception->getMessage())->once()
  201. ->mock($git)
  202. ->call('resetHardTo')->withArguments('HEAD~2')->once()
  203. ->call('deleteLocalTag')->withArguments('0.0.1')->once()
  204. ->assert('Pusher should write error if pushing commit for DEVELOPMENT version failed and should try to reset repository')
  205. ->if(
  206. $this->calling($git)->push = $git,
  207. $this->calling($git)->addAllAndCommit[2]->throw = $exception = new \exception(uniqid())
  208. )
  209. ->then
  210. ->object($pusher->run())->isIdenticalTo($pusher)
  211. ->mock($errorWriter)->call('write')->withArguments($exception->getMessage())->once()
  212. ->mock($git)
  213. ->call('resetHardTo')->withArguments('HEAD~1')->once()
  214. ->assert('Pusher should write error if pushing commit for DEVELOPMENT version failed and should try to reset repository')
  215. ->if(
  216. $this->calling($git)->createTag->throw = $exception = new \exception(uniqid())
  217. )
  218. ->then
  219. ->object($pusher->run())->isIdenticalTo($pusher)
  220. ->mock($errorWriter)->call('write')->withArguments($exception->getMessage())->once()
  221. ->mock($git)
  222. ->call('resetHardTo')->withArguments('HEAD~1')->once()
  223. ->assert('Pusher should write error if commit failed for STABLE version and should try to reset repository')
  224. ->if(
  225. $this->calling($git)->createTag = $git,
  226. $this->calling($git)->addAllAndCommit[1]->throw = $exception = new \exception(uniqid())
  227. )
  228. ->then
  229. ->object($pusher->run())->isIdenticalTo($pusher)
  230. ->mock($errorWriter)
  231. ->call('write')->withArguments($exception->getMessage())
  232. ->after($this->mock($git)->call('addAllAndCommit'))
  233. ->once()
  234. ->mock($git)
  235. ->call('checkoutAllFiles')
  236. ->after($this->mock($git)->call('addAllAndCommit'))
  237. ->once()
  238. ->assert('Pusher should write error if reset failed')
  239. ->if(
  240. $this->calling($git)->checkoutAllFiles->throw = $checkoutAllFilesException = new \exception(uniqid())
  241. )
  242. ->then
  243. ->object($pusher->run())->isIdenticalTo($pusher)
  244. ->mock($errorWriter)
  245. ->call('write')->withArguments($checkoutAllFilesException->getMessage())
  246. ->once()
  247. ;
  248. }
  249. }