PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/units/classes/scripts/phar/generator.php

http://github.com/mageekguy/atoum
PHP | 370 lines | 354 code | 16 blank | 0 comment | 3 complexity | 933588e2b7d1f485e48818d22777f14d MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\scripts\phar;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\mock,
  6. mageekguy\atoum\mock\stream,
  7. mageekguy\atoum\iterators,
  8. mageekguy\atoum\scripts\phar
  9. ;
  10. require_once __DIR__ . '/../../../runner.php';
  11. class generator extends atoum\test
  12. {
  13. public function testClass()
  14. {
  15. $this->testedClass->extends('mageekguy\atoum\script');
  16. }
  17. public function testClassConstants()
  18. {
  19. $this->string(phar\generator::phar)->isEqualTo('atoum.phar');
  20. }
  21. public function test__construct()
  22. {
  23. $this
  24. ->if($adapter = new atoum\test\adapter())
  25. ->and($adapter->php_sapi_name = function() { return uniqid(); })
  26. ->then
  27. ->exception(function() use (& $name, $adapter) {
  28. $generator = new phar\generator($name = uniqid(), $adapter);
  29. }
  30. )
  31. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  32. ->hasMessage('\'' . $name . '\' must be used in CLI only')
  33. ->if($adapter->php_sapi_name = function() { return 'cli'; })
  34. ->and($generator = new phar\generator($name = uniqid(), $adapter))
  35. ->then
  36. ->object($generator->getAdapter())->isIdenticalTo($adapter)
  37. ->object($generator->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  38. ->object($generator->getOutputWriter())->isInstanceOf('mageekguy\atoum\writer')
  39. ->object($generator->getErrorWriter())->isInstanceOf('mageekguy\atoum\writer')
  40. ->string($generator->getName())->isEqualTo($name)
  41. ->variable($generator->getOriginDirectory())->isNull()
  42. ->variable($generator->getDestinationDirectory())->isNull()
  43. ->object($generator->getArgumentsParser())->isInstanceOf('mageekguy\atoum\script\arguments\parser')
  44. ;
  45. }
  46. public function testSetOriginDirectory()
  47. {
  48. $this
  49. ->if($adapter = new atoum\test\adapter())
  50. ->and($adapter->php_sapi_name = function() { return 'cli'; })
  51. ->and($adapter->realpath = function($path) { return $path; })
  52. ->and($generator = new phar\generator(uniqid(), $adapter))
  53. ->then
  54. ->exception(function() use ($generator) {
  55. $generator->setOriginDirectory('');
  56. }
  57. )
  58. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  59. ->hasMessage('Empty origin directory is invalid')
  60. ->if($adapter->is_dir = function() { return false; })
  61. ->then
  62. ->exception(function() use ($generator, & $directory) {
  63. $generator->setOriginDirectory($directory = uniqid());
  64. }
  65. )
  66. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  67. ->hasMessage('Path \'' . $directory . '\' of origin directory is invalid')
  68. ->if($adapter->is_dir = function() { return true; })
  69. ->then
  70. ->object($generator->setOriginDirectory('/'))->isIdenticalTo($generator)
  71. ->string($generator->getOriginDirectory())->isEqualTo('/')
  72. ->object($generator->setOriginDirectory(($directory = uniqid()) . DIRECTORY_SEPARATOR))->isIdenticalTo($generator)
  73. ->string($generator->getOriginDirectory())->isEqualTo($directory)
  74. ->if($generator->setDestinationDirectory(uniqid()))
  75. ->then
  76. ->exception(function() use ($generator) {
  77. $generator->setOriginDirectory($generator->getDestinationDirectory());
  78. }
  79. )
  80. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  81. ->hasMessage('Origin directory must be different from destination directory')
  82. ->if($realDirectory = $generator->getDestinationDirectory() . DIRECTORY_SEPARATOR . uniqid())
  83. ->and($adapter->realpath = function($path) use ($realDirectory) { return $realDirectory; })
  84. ->then
  85. ->object($generator->setOriginDirectory('/'))->isIdenticalTo($generator)
  86. ->string($generator->getOriginDirectory())->isEqualTo($realDirectory)
  87. ;
  88. }
  89. public function testSetDestinationDirectory()
  90. {
  91. $this
  92. ->if($adapter = new atoum\test\adapter())
  93. ->and($adapter->php_sapi_name = function() { return 'cli'; })
  94. ->and($adapter->realpath = function($path) { return $path; })
  95. ->and($generator = new phar\generator(uniqid(), $adapter))
  96. ->then
  97. ->exception(function() use ($generator) {
  98. $generator->setDestinationDirectory('');
  99. }
  100. )
  101. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  102. ->hasMessage('Empty destination directory is invalid')
  103. ->if ($adapter->is_dir = function() { return false; })
  104. ->then
  105. ->exception(function() use ($generator, & $directory) {
  106. $generator->setDestinationDirectory($directory = uniqid());
  107. }
  108. )
  109. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  110. ->hasMessage('Path \'' . $directory . '\' of destination directory is invalid')
  111. ->if($adapter->is_dir = function() { return true; })
  112. ->then
  113. ->object($generator->setDestinationDirectory('/'))->isIdenticalTo($generator)
  114. ->string($generator->getDestinationDirectory())->isEqualTo('/')
  115. ->object($generator->setDestinationDirectory($directory = uniqid()))->isIdenticalTo($generator)
  116. ->string($generator->getDestinationDirectory())->isEqualTo($directory)
  117. ->object($generator->setDestinationDirectory(($directory = uniqid()) . DIRECTORY_SEPARATOR))->isIdenticalTo($generator)
  118. ->string($generator->getDestinationDirectory())->isEqualTo($directory)
  119. ->if ($generator->setOriginDirectory(uniqid()))
  120. ->then
  121. ->exception(function() use ($generator) {
  122. $generator->setDestinationDirectory($generator->getOriginDirectory());
  123. }
  124. )
  125. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  126. ->hasMessage('Destination directory must be different from origin directory')
  127. ;
  128. }
  129. public function testSetStubFile()
  130. {
  131. $this
  132. ->if($adapter = new atoum\test\adapter())
  133. ->and($adapter->php_sapi_name = function() { return 'cli'; })
  134. ->and($adapter->realpath = function($path) { return $path; })
  135. ->and($generator = new phar\generator(uniqid(), $adapter))
  136. ->then
  137. ->exception(function() use ($generator) {
  138. $generator->setStubFile('');
  139. }
  140. )
  141. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  142. ->hasMessage('Stub file is invalid')
  143. ->if($adapter->is_file = function() { return false; })
  144. ->then
  145. ->exception(function() use ($generator) {
  146. $generator->setStubFile(uniqid());
  147. }
  148. )
  149. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  150. ->hasMessage('Stub file is not a valid file')
  151. ->if($adapter->is_file = function() { return true; })
  152. ->then
  153. ->object($generator->setStubFile($stubFile = uniqid()))->isIdenticalTo($generator)
  154. ->string($generator->getStubFile())->isEqualTo($stubFile)
  155. ;
  156. }
  157. public function testRun()
  158. {
  159. $this
  160. ->if
  161. ->extension('phar')->isLoaded()
  162. ->and($originDirectory = stream::get())
  163. ->and($originDirectory->opendir = true)
  164. ->and($adapter = new atoum\test\adapter())
  165. ->and($adapter->php_sapi_name = function() { return 'cli'; })
  166. ->and($adapter->realpath = function($path) { return $path; })
  167. ->and($adapter->is_dir = function() { return true; })
  168. ->and($adapter->is_file = function() { return true; })
  169. ->and($adapter->unlink = function() {})
  170. ->and($generator = new phar\generator(uniqid(), $adapter))
  171. ->then
  172. ->exception(function () use ($generator) {
  173. $generator->run();
  174. }
  175. )
  176. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  177. ->hasMessage('Origin directory must be defined')
  178. ->if($generator->setOriginDirectory((string) $originDirectory))
  179. ->then
  180. ->exception(function () use ($generator) {
  181. $generator->run();
  182. }
  183. )
  184. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  185. ->hasMessage('Destination directory must be defined')
  186. ->if($generator->setDestinationDirectory(uniqid()))
  187. ->then
  188. ->exception(function () use ($generator) {
  189. $generator->run();
  190. }
  191. )
  192. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  193. ->hasMessage('Stub file must be defined')
  194. ->if($generator->setStubFile($stubFile = uniqid()))
  195. ->and($adapter->is_readable = function() { return false; })
  196. ->then
  197. ->exception(function () use ($generator) {
  198. $generator->run();
  199. }
  200. )
  201. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  202. ->hasMessage('Origin directory \'' . $generator->getOriginDirectory() . '\' is not readable')
  203. ->if($adapter->is_readable = function($path) use ($originDirectory) { return ($path === (string) $originDirectory); })
  204. ->and($adapter->is_writable = function() { return false; })
  205. ->then
  206. ->exception(function () use ($generator) {
  207. $generator->run();
  208. }
  209. )
  210. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  211. ->hasMessage('Destination directory \'' . $generator->getDestinationDirectory() . '\' is not writable')
  212. ->if($adapter->is_writable = function() { return true; })
  213. ->then
  214. ->exception(function () use ($generator) {
  215. $generator->run();
  216. }
  217. )
  218. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  219. ->hasMessage('Stub file \'' . $generator->getStubFile() . '\' is not readable')
  220. ->if($adapter->is_readable = function($path) use ($originDirectory, $stubFile) { return ($path === (string) $originDirectory || $path === $stubFile); })
  221. ->and($generator->setPharFactory(function($name) use (& $phar) {
  222. $pharController = new mock\controller();
  223. $pharController->__construct = function() {};
  224. $pharController->setStub = function() {};
  225. $pharController->setMetadata = function() {};
  226. $pharController->buildFromIterator = function() {};
  227. $pharController->setSignatureAlgorithm = function() {};
  228. $pharController->offsetGet = function() {};
  229. $pharController->offsetSet = function() {};
  230. return ($phar = new \mock\phar($name));
  231. }
  232. )
  233. )
  234. ->and($adapter->file_get_contents = function($file) { return false; })
  235. ->then
  236. ->exception(function() use ($generator) {
  237. $generator->run();
  238. }
  239. )
  240. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  241. ->hasMessage('ABOUT file is missing in \'' . $generator->getOriginDirectory() . '\'')
  242. ->if($adapter->file_get_contents = function($file) use ($generator, & $description) {
  243. switch ($file)
  244. {
  245. case $generator->getOriginDirectory() . DIRECTORY_SEPARATOR . 'ABOUT':
  246. return ($description = uniqid());
  247. default:
  248. return false;
  249. }
  250. }
  251. )
  252. ->then
  253. ->exception(function() use ($generator) {
  254. $generator->run();
  255. }
  256. )
  257. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  258. ->hasMessage('COPYING file is missing in \'' . $generator->getOriginDirectory() . '\'')
  259. ->if($adapter->file_get_contents = function($file) use ($generator, & $description, & $licence, & $stub) {
  260. switch ($file)
  261. {
  262. case $generator->getOriginDirectory() . DIRECTORY_SEPARATOR . 'ABOUT':
  263. return ($description = uniqid());
  264. case $generator->getOriginDirectory() . DIRECTORY_SEPARATOR . 'COPYING':
  265. return ($licence = uniqid());
  266. case $generator->getStubFile():
  267. return ($stub = uniqid());
  268. default:
  269. return uniqid();
  270. }
  271. }
  272. )
  273. ->then
  274. ->object($generator->run())->isIdenticalTo($generator)
  275. ->mock($phar)
  276. ->call('__construct')->withArguments($generator->getDestinationDirectory() . DIRECTORY_SEPARATOR . atoum\scripts\phar\generator::phar, null, null, null)->once()
  277. ->call('setMetadata')
  278. ->withArguments(array(
  279. 'version' => atoum\version,
  280. 'author' => atoum\author,
  281. 'support' => atoum\mail,
  282. 'repository' => atoum\repository,
  283. 'description' => $description,
  284. 'licence' => $licence
  285. )
  286. )
  287. ->once()
  288. ->call('setStub')->withArguments($stub, null)->once()
  289. ->call('buildFromIterator')
  290. ->withArguments(new iterators\recursives\atoum\source($generator->getOriginDirectory(), '1'), null)
  291. ->once()
  292. ->call('setSignatureAlgorithm')
  293. ->withArguments(\phar::SHA1, null)
  294. ->once()
  295. ->if($superglobals = new atoum\superglobals())
  296. ->and($superglobals->_SERVER = array('argv' => array(uniqid(), '--help')))
  297. ->and($generator->setArgumentsParser(new atoum\script\arguments\parser($superglobals)))
  298. ->and($stdout = new \mock\mageekguy\atoum\writers\std\out())
  299. ->and($stdout->getMockController()->write = function() {})
  300. ->and($stderr = new \mock\mageekguy\atoum\writers\std\err())
  301. ->and($stderr->getMockController()->write = function() {})
  302. ->and($generator->setHelpWriter($stdout))
  303. ->and($generator->setErrorWriter($stderr))
  304. ->then
  305. ->object($generator->run())->isIdenticalTo($generator)
  306. ->mock($stdout)
  307. ->call('write')->withArguments(sprintf($generator->getLocale()->_('Usage: %s [options]'), $generator->getName()))->once()
  308. ->call('write')->withArguments($generator->getLocale()->_('Available options are:'))->once()
  309. ->call('write')->withArguments(' -h, --help: ' . $generator->getLocale()->_('Display this help'))->once()
  310. ->call('write')->withArguments(' -d <directory>, --directory <directory>: ' . $generator->getLocale()->_('Destination directory <dir>'))->once()
  311. ->if($generator->setPharFactory(function($name) use (& $phar) {
  312. $pharController = new mock\controller();
  313. $pharController->__construct = function() {};
  314. $pharController->setStub = function() {};
  315. $pharController->setMetadata = function() {};
  316. $pharController->buildFromIterator = function() {};
  317. $pharController->setSignatureAlgorithm = function() {};
  318. $pharController->offsetGet = function() {};
  319. $pharController->offsetSet = function() {};
  320. return ($phar = new \mock\phar($name));
  321. }
  322. )
  323. )
  324. ->then
  325. ->object($generator->run(array('-d', $directory = uniqid())))->isIdenticalTo($generator)
  326. ->string($generator->getDestinationDirectory())->isEqualTo($directory)
  327. ->mock($phar)
  328. ->call('__construct')
  329. ->withArguments($generator->getDestinationDirectory() . DIRECTORY_SEPARATOR . atoum\scripts\phar\generator::phar, null, null, null)
  330. ->once()
  331. ->call('setMetadata')
  332. ->withArguments(
  333. array(
  334. 'version' => atoum\version,
  335. 'author' => atoum\author,
  336. 'support' => atoum\mail,
  337. 'repository' => atoum\repository,
  338. 'description' => $description,
  339. 'licence' => $licence
  340. )
  341. )
  342. ->once()
  343. ->call('setStub')->withArguments($stub, null)->once()
  344. ->call('buildFromIterator')
  345. ->withArguments(new iterators\recursives\atoum\source($generator->getOriginDirectory(), '1'), null)
  346. ->once()
  347. ->call('setSignatureAlgorithm')
  348. ->withArguments(\phar::SHA1, null)
  349. ->once()
  350. ->adapter($adapter)
  351. ->call('unlink')->withArguments($directory . DIRECTORY_SEPARATOR . phar\generator::phar)->once()
  352. ;
  353. }
  354. }