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

https://github.com/Hywan/atoum · PHP · 410 lines · 394 code · 16 blank · 0 comment · 3 complexity · 90935e6a740879d744aace3071ae5ec5 MD5 · raw file

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