/tests/units/classes/scripts/tagger/engine.php

https://github.com/Hywan/atoum · PHP · 256 lines · 243 code · 13 blank · 0 comment · 0 complexity · 06b5ef185774ccab81409af886f6c7b1 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\scripts\tagger;
  3. use mageekguy\atoum;
  4. require_once __DIR__ . '/../../../runner.php';
  5. class engine extends atoum\test
  6. {
  7. public function testClassConstants()
  8. {
  9. $this
  10. ->testedClass
  11. ->hasConstant('defaultVersionPattern')->isEqualTo('/\$Rev: ([^ %]+) \$/')
  12. ;
  13. }
  14. public function test__construct()
  15. {
  16. $this
  17. ->if($this->newTestedInstance)
  18. ->then
  19. ->variable($this->testedInstance->getSrcDirectory())->isNull()
  20. ->variable($this->testedInstance->getDestinationDirectory())->isNull()
  21. ->variable($this->testedInstance->getVersion())->isNull()
  22. ->string($this->testedInstance->getVersionPattern())->isEqualTo(\mageekguy\atoum\scripts\tagger\engine::defaultVersionPattern)
  23. ->object($this->testedInstance->getAdapter())->isInstanceOf(atoum\adapter::class)
  24. ->if($this->newTestedInstance($adapter = new atoum\adapter()))
  25. ->then
  26. ->variable($this->testedInstance->getSrcDirectory())->isNull()
  27. ->variable($this->testedInstance->getDestinationDirectory())->isNull()
  28. ->variable($this->testedInstance->getVersion())->isNull()
  29. ->string($this->testedInstance->getVersionPattern())->isEqualTo(\mageekguy\atoum\scripts\tagger\engine::defaultVersionPattern)
  30. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  31. ;
  32. }
  33. public function testSetAdapter()
  34. {
  35. $this
  36. ->if($this->newTestedInstance)
  37. ->then
  38. ->object($this->testedInstance->setAdapter($adapter = new atoum\adapter()))->isTestedInstance
  39. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  40. ;
  41. }
  42. public function testSetVersion()
  43. {
  44. $this
  45. ->if($this->newTestedInstance)
  46. ->then
  47. ->object($this->testedInstance->setVersion($version = uniqid()))->isTestedInstance
  48. ->string($this->testedInstance->getVersion())->isEqualTo($version)
  49. ->object($this->testedInstance->setVersion($version = rand(1, PHP_INT_MAX)))->isTestedInstance
  50. ->string($this->testedInstance->getVersion())->isEqualTo((string) $version)
  51. ;
  52. }
  53. public function testSetVersionPattern()
  54. {
  55. $this
  56. ->if($this->newTestedInstance)
  57. ->then
  58. ->object($this->testedInstance->setVersionPattern($pattern = uniqid()))->isTestedInstance
  59. ->string($this->testedInstance->getVersionPattern())->isEqualTo($pattern)
  60. ->object($this->testedInstance->setVersionPattern($pattern = rand(1, PHP_INT_MAX)))->isTestedInstance
  61. ->string($this->testedInstance->getVersionPattern())->isEqualTo((string) $pattern)
  62. ;
  63. }
  64. public function testSetSrcDirectory()
  65. {
  66. $this
  67. ->if($this->newTestedInstance)
  68. ->then
  69. ->object($this->testedInstance->setSrcDirectory($directory = uniqid()))->isTestedInstance
  70. ->string($this->testedInstance->getSrcDirectory())->isEqualTo($directory)
  71. ->string($this->testedInstance->getDestinationDirectory())->isEqualTo($directory)
  72. ->object($this->testedInstance->setSrcDirectory(($otherDirectory = uniqid()) . \DIRECTORY_SEPARATOR))->isTestedInstance
  73. ->string($this->testedInstance->getSrcDirectory())->isEqualTo($otherDirectory)
  74. ->string($this->testedInstance->getDestinationDirectory())->isEqualTo($directory)
  75. ->object($this->testedInstance->setSrcDirectory($otherDirectory = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isTestedInstance
  76. ->string($this->testedInstance->getSrcDirectory())->isEqualTo((string) $otherDirectory)
  77. ->string($this->testedInstance->getDestinationDirectory())->isEqualTo($directory)
  78. ;
  79. }
  80. public function testSetDestinationDirectory()
  81. {
  82. $this
  83. ->if($this->newTestedInstance)
  84. ->then
  85. ->object($this->testedInstance->setDestinationDirectory($directory = uniqid()))->isTestedInstance
  86. ->string($this->testedInstance->getDestinationDirectory())->isEqualTo($directory)
  87. ->object($this->testedInstance->setDestinationDirectory(($directory = uniqid()) . \DIRECTORY_SEPARATOR))->isTestedInstance
  88. ->string($this->testedInstance->getDestinationDirectory())->isEqualTo($directory)
  89. ->object($this->testedInstance->setDestinationDirectory($directory = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isTestedInstance
  90. ->string($this->testedInstance->getDestinationDirectory())->isEqualTo((string) $directory)
  91. ;
  92. }
  93. public function testSetSrcIteratorInjector()
  94. {
  95. $this
  96. ->if(
  97. $tagger = $this->newTestedInstance,
  98. $this->testedInstance->setSrcDirectory(__DIR__)
  99. )
  100. ->then
  101. ->exception(function () use ($tagger) {
  102. $tagger->setSrcIteratorInjector(function () {
  103. });
  104. })
  105. ->isInstanceOf(atoum\exceptions\logic::class)
  106. ->hasMessage('Src iterator injector must take one argument')
  107. ->object($this->testedInstance->setSrcIteratorInjector(function ($directory) {
  108. return new \recursiveDirectoryIterator($directory);
  109. }))->isTestedInstance
  110. ->object($this->testedInstance->getSrcIterator())->isInstanceOf(\recursiveDirectoryIterator::class)
  111. ->string($this->testedInstance->getSrcIterator()->getPath())->isEqualTo(__DIR__)
  112. ;
  113. }
  114. public function testGetSrcIterator()
  115. {
  116. $this
  117. ->if($tagger = $this->newTestedInstance)
  118. ->then
  119. ->exception(function () use ($tagger) {
  120. $tagger->getSrcIterator();
  121. })
  122. ->isInstanceOf(atoum\exceptions\logic::class)
  123. ->hasMessage('Unable to get files iterator, source directory is undefined')
  124. ->if($this->testedInstance->setSrcDirectory(__DIR__))
  125. ->then
  126. ->object($this->testedInstance->getSrcIterator())->isInstanceOf(\recursiveIteratorIterator::class)
  127. ->object($this->testedInstance->getSrcIterator()->getInnerIterator())->isInstanceOf(atoum\iterators\filters\recursives\dot::class)
  128. ;
  129. }
  130. public function testTagVersion()
  131. {
  132. $this
  133. ->if(
  134. $tagger = $this->newTestedInstance($adapter = new atoum\test\adapter()),
  135. $adapter->is_dir = true,
  136. $adapter->mkdir = function () {
  137. }
  138. )
  139. ->then
  140. ->exception(function () use ($tagger) {
  141. $tagger->tagVersion(uniqid());
  142. })
  143. ->isInstanceOf(atoum\exceptions\logic::class)
  144. ->hasMessage('Unable to tag, src directory is undefined')
  145. ->if($this->testedInstance->setSrcDirectory($srcDirectory = uniqid()))
  146. ->then
  147. ->exception(function () use ($tagger) {
  148. $tagger->tagVersion(uniqid());
  149. })
  150. ->isInstanceOf(atoum\exceptions\logic::class)
  151. ->hasMessage('Unable to tag, version is undefined')
  152. ->if(
  153. $tagger
  154. ->setVersion($version = uniqid())
  155. ->setSrcIteratorInjector(function ($directory) {
  156. })
  157. )
  158. ->then
  159. ->exception(function () use ($tagger) {
  160. $tagger->tagVersion(uniqid());
  161. })
  162. ->isInstanceOf(atoum\exceptions\logic::class)
  163. ->hasMessage('Unable to tag, src iterator injector does not return an iterator')
  164. ->if(
  165. $srcIterator = new \arrayIterator([
  166. $file1 = $srcDirectory . \DIRECTORY_SEPARATOR . ($basename1 = uniqid()),
  167. $file2 = $srcDirectory . \DIRECTORY_SEPARATOR . ($basename2 = uniqid()),
  168. $file3 = $srcDirectory . \DIRECTORY_SEPARATOR . ($basename3 = uniqid()),
  169. ]
  170. ),
  171. $this->testedInstance->setSrcIteratorInjector(function ($directory) use ($srcIterator) {
  172. return $srcIterator;
  173. }),
  174. $adapter->file_get_contents[1] = ($file1Part1 = uniqid()) . '\'$Rev: ' . rand(1, PHP_INT_MAX) . ' $\'' . ($file1Part2 = uniqid()),
  175. $adapter->file_get_contents[2] = $contentOfFile2 = uniqid(),
  176. $adapter->file_get_contents[3] = ($file3Part1 = uniqid()) . '"$Rev: ' . rand(1, PHP_INT_MAX) . ' $"' . ($file3Part2 = uniqid()),
  177. $adapter->file_put_contents = function () {
  178. }
  179. )
  180. ->then
  181. ->object($this->testedInstance->tagVersion())->isTestedInstance
  182. ->adapter($adapter)
  183. ->call('file_get_contents')->withArguments($file1)->once()
  184. ->call('file_put_contents')->withArguments($file1, $file1Part1 . '\'' . $version . '\'' . $file1Part2, \LOCK_EX)->once()
  185. ->call('file_get_contents')->withArguments($file2)->once()
  186. ->call('file_put_contents')->withArguments($file2, $contentOfFile2, \LOCK_EX)->once()
  187. ->call('file_get_contents')->withArguments($file3)->once()
  188. ->call('file_put_contents')->withArguments($file3, $file3Part1 . '"' . $version . '"' . $file3Part2, \LOCK_EX)->once()
  189. ->if($adapter->resetCalls()->file_get_contents[2] = false)
  190. ->then
  191. ->exception(function () use ($tagger) {
  192. $tagger->tagVersion(uniqid());
  193. })
  194. ->isInstanceOf(atoum\exceptions\runtime::class)
  195. ->hasMessage('Unable to tag, path \'' . $file2 . '\' is not readable')
  196. ->if(
  197. $adapter->resetCalls(),
  198. $adapter->file_get_contents[2] = $contentOfFile2,
  199. $adapter->file_put_contents[2] = false
  200. )
  201. ->then
  202. ->exception(function () use ($tagger) {
  203. $tagger->tagVersion(uniqid());
  204. })
  205. ->isInstanceOf(atoum\exceptions\runtime::class)
  206. ->hasMessage('Unable to tag, path \'' . $file2 . '\' is not writable')
  207. ->if(
  208. $adapter->resetCalls(),
  209. $this->testedInstance->setDestinationDirectory($destinationDirectory = uniqid())
  210. )
  211. ->when(function () use ($adapter) {
  212. unset($adapter->file_put_contents[2]);
  213. })
  214. ->then
  215. ->object($this->testedInstance->tagVersion())->isTestedInstance
  216. ->adapter($adapter)
  217. ->call('is_dir')->withArguments($destinationDirectory)->exactly(3)
  218. ->call('mkdir')->never()
  219. ->call('file_get_contents')->withArguments($file1)->once()
  220. ->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename1, $file1Part1 . '\'' . $version . '\'' . $file1Part2, \LOCK_EX)->once()
  221. ->call('file_get_contents')->withArguments($file2)->once()
  222. ->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename2, $contentOfFile2, \LOCK_EX)->once()
  223. ->call('file_get_contents')->withArguments($file3)->once()
  224. ->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename3, $file3Part1 . '"' . $version . '"' . $file3Part2, \LOCK_EX)->once()
  225. ->if(
  226. $adapter
  227. ->resetCalls()
  228. ->is_dir = false
  229. )
  230. ->then
  231. ->object($this->testedInstance->tagVersion())->isTestedInstance
  232. ->adapter($adapter)
  233. ->call('is_dir')->withArguments($destinationDirectory)->exactly(3)
  234. ->call('mkdir')->withArguments($destinationDirectory, 0777, true)->exactly(3)
  235. ->call('file_get_contents')->withArguments($file1)->once()
  236. ->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename1, $file1Part1 . '\'' . $version . '\'' . $file1Part2, \LOCK_EX)->once()
  237. ->call('file_get_contents')->withArguments($file2)->once()
  238. ->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename2, $contentOfFile2, \LOCK_EX)->once()
  239. ->call('file_get_contents')->withArguments($file3)->once()
  240. ->call('file_put_contents')->withArguments($destinationDirectory . \DIRECTORY_SEPARATOR . $basename3, $file3Part1 . '"' . $version . '"' . $file3Part2, \LOCK_EX)->once()
  241. ;
  242. }
  243. }