PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

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