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

https://github.com/tharkun/atoum · PHP · 274 lines · 229 code · 45 blank · 0 comment · 0 complexity · afa5ed3641be998f66683a1a067446c9 MD5 · raw file

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