PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/script.php

http://github.com/mageekguy/atoum
PHP | 633 lines | 591 code | 42 blank | 0 comment | 0 complexity | 8d830ee817577d5f018e84e146cc39ec MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\writer,
  6. mageekguy\atoum\writers,
  7. mageekguy\atoum\script\prompt,
  8. mock\mageekguy\atoum as mock
  9. ;
  10. require_once __DIR__ . '/../runner.php';
  11. class script extends atoum\test
  12. {
  13. public function testClassConstants()
  14. {
  15. $this->string(atoum\script::padding)->isEqualTo(' ');
  16. }
  17. public function test__construct()
  18. {
  19. $this
  20. ->given(
  21. $labelColorizer = new atoum\cli\colorizer('0;32'),
  22. $labelColorizer->setPattern('/(^[^:]+: )/'),
  23. $argumentColorizer = new atoum\cli\colorizer('0;32'),
  24. $argumentColorizer->setPattern('/((?:^| )[-+]+[-a-z]+)/'),
  25. $valueColorizer = new atoum\cli\colorizer('0;34'),
  26. $valueColorizer->setPattern('/(<[^>]+>(?:\.\.\.)?)/'),
  27. $defaultOutputWriter = new writers\std\out(),
  28. $defaultInfoWriter = new writers\std\out(),
  29. $defaultInfoWriter
  30. ->addDecorator(new writer\decorators\rtrim())
  31. ->addDecorator(new writer\decorators\eol())
  32. ->addDecorator(new atoum\cli\clear()),
  33. $defaultWarningWriter = new writers\std\err(),
  34. $defaultWarningWriter
  35. ->addDecorator(new writer\decorators\trim())
  36. ->addDecorator(new writer\decorators\prompt('Warning: '))
  37. ->addDecorator(new writer\decorators\eol())
  38. ->addDecorator(new atoum\cli\clear()),
  39. $defaultErrorWriter = new writers\std\err(),
  40. $defaultErrorWriter
  41. ->addDecorator(new writer\decorators\trim())
  42. ->addDecorator(new writer\decorators\prompt('Error: '))
  43. ->addDecorator(new writer\decorators\eol())
  44. ->addDecorator(new atoum\cli\clear()),
  45. $defaultHelpWriter = new writers\std\out(),
  46. $defaultHelpWriter
  47. ->addDecorator($labelColorizer)
  48. ->addDecorator($valueColorizer)
  49. ->addDecorator($argumentColorizer)
  50. ->addDecorator(new writer\decorators\rtrim())
  51. ->addDecorator(new writer\decorators\eol())
  52. ->addDecorator(new atoum\cli\clear())
  53. )
  54. ->if($script = new mock\script($name = uniqid()))
  55. ->then
  56. ->string($script->getName())->isEqualTo($name)
  57. ->object($script->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  58. ->object($script->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  59. ->object($script->getArgumentsParser())->isInstanceOf('mageekguy\atoum\script\arguments\parser')
  60. ->object($script->getOutputWriter())->isEqualTo($defaultOutputWriter)
  61. ->object($script->getInfoWriter())->isEqualTo($defaultInfoWriter)
  62. ->object($script->getErrorWriter())->isEqualTo($defaultErrorWriter)
  63. ->object($script->getWarningWriter())->isEqualTo($defaultWarningWriter)
  64. ->object($script->getHelpWriter())->isEqualTo($defaultHelpWriter)
  65. ->array($script->getHelp())->isEmpty()
  66. ->object($script->getCli())->isEqualTo(new atoum\cli())
  67. ->integer($script->getVerbosityLevel())->isZero()
  68. ->and($script = new mock\script($name = uniqid(), $adapter = new atoum\adapter()))
  69. ->then
  70. ->string($script->getName())->isEqualTo($name)
  71. ->object($script->getAdapter())->isIdenticalTo($adapter)
  72. ->object($script->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  73. ->object($script->getArgumentsParser())->isInstanceOf('mageekguy\atoum\script\arguments\parser')
  74. ->object($script->getOutputWriter())->isEqualTo($defaultOutputWriter)
  75. ->object($script->getInfoWriter())->isEqualTo($defaultInfoWriter)
  76. ->object($script->getErrorWriter())->isEqualTo($defaultErrorWriter)
  77. ->object($script->getWarningWriter())->isEqualTo($defaultWarningWriter)
  78. ->object($script->getHelpWriter())->isEqualTo($defaultHelpWriter)
  79. ->array($script->getHelp())->isEmpty()
  80. ->object($script->getCli())->isEqualTo(new atoum\cli())
  81. ->integer($script->getVerbosityLevel())->isZero()
  82. ->if($adapter = new atoum\test\adapter())
  83. ->and($adapter->php_sapi_name = uniqid())
  84. ->then
  85. ->exception(function() use ($adapter, & $name) {
  86. new mock\script($name = uniqid(), $adapter);
  87. }
  88. )
  89. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  90. ->hasMessage('\'' . $name . '\' must be used in CLI only')
  91. ;
  92. }
  93. public function testSetAdapter()
  94. {
  95. $this
  96. ->if($script = new mock\script($name = uniqid()))
  97. ->then
  98. ->object($script->setAdapter($adapter = new atoum\adapter()))->isIdenticalTo($script)
  99. ->object($script->getAdapter())->isIdenticalTo($adapter)
  100. ;
  101. }
  102. public function testSetLocale()
  103. {
  104. $this
  105. ->if($script = new mock\script($name = uniqid()))
  106. ->then
  107. ->object($script->setLocale($locale = new atoum\locale()))->isIdenticalTo($script)
  108. ->object($script->getLocale())->isIdenticalTo($locale)
  109. ;
  110. }
  111. public function testSetCli()
  112. {
  113. $this
  114. ->if($script = new mock\script(uniqid()))
  115. ->then
  116. ->object($script->setCli($cli = new atoum\cli()))->isIdenticalTo($script)
  117. ->object($script->getCli())->isIdenticalTo($cli)
  118. ->object($script->setCli())->isIdenticalTo($script)
  119. ->object($script->getCli())
  120. ->isNotIdenticalTo($cli)
  121. ->isEqualTo(new atoum\cli())
  122. ;
  123. }
  124. public function testSetArgumentParser()
  125. {
  126. $this
  127. ->if($script = new mock\script($name = uniqid()))
  128. ->then
  129. ->object($script->setArgumentsParser($argumentsParser = new atoum\script\arguments\parser()))->isIdenticalTo($script)
  130. ->object($script->getArgumentsParser())->isIdenticalTo($argumentsParser)
  131. ;
  132. }
  133. public function testSetOutputWriter()
  134. {
  135. $this
  136. ->if($script = new mock\script($name = uniqid()))
  137. ->then
  138. ->object($script->setOutputWriter($outputWriter = new writers\std\out()))->isIdenticalTo($script)
  139. ->object($script->getOutputWriter())->isIdenticalTo($outputWriter)
  140. ->given(
  141. $defaultOutputWriter = new writers\std\out()
  142. )
  143. ->then
  144. ->object($script->setOutputWriter())->isIdenticalTo($script)
  145. ->object($script->getOutputWriter())
  146. ->isNotIdenticalTo($outputWriter)
  147. ->isEqualTo($defaultOutputWriter)
  148. ->object($script->getOutputWriter()->getCli())->isIdenticalTo($script->getCli())
  149. ;
  150. }
  151. public function testSetInfoWriter()
  152. {
  153. $this
  154. ->if($script = new mock\script($name = uniqid()))
  155. ->then
  156. ->object($script->setInfoWriter($infoWriter = new writers\std\out()))->isIdenticalTo($script)
  157. ->object($script->getInfoWriter())->isIdenticalTo($infoWriter)
  158. ->given(
  159. $defaultInfoWriter = new writers\std\out(),
  160. $defaultInfoWriter
  161. ->addDecorator(new writer\decorators\rtrim())
  162. ->addDecorator(new writer\decorators\eol())
  163. ->addDecorator(new atoum\cli\clear())
  164. )
  165. ->object($script->setInfoWriter())->isIdenticalTo($script)
  166. ->object($script->getInfoWriter())
  167. ->isNotIdenticalTo($infoWriter)
  168. ->isEqualTo($defaultInfoWriter)
  169. ->object($script->getInfoWriter()->getCli())->isIdenticalTo($script->getCli())
  170. ;
  171. }
  172. public function testSetWarningWriter()
  173. {
  174. $this
  175. ->if($script = new mock\script($name = uniqid()))
  176. ->then
  177. ->object($script->setWarningWriter($warningWriter = new writers\std\out()))->isIdenticalTo($script)
  178. ->object($script->getWarningWriter())->isIdenticalTo($warningWriter)
  179. ->given(
  180. $defaultWarningWriter = new writers\std\err(),
  181. $defaultWarningWriter
  182. ->addDecorator(new writer\decorators\trim())
  183. ->addDecorator(new writer\decorators\prompt($script->getLocale()->_('Warning: ')))
  184. ->addDecorator(new writer\decorators\eol())
  185. ->addDecorator(new atoum\cli\clear())
  186. )
  187. ->then
  188. ->object($script->setWarningWriter())->isIdenticalTo($script)
  189. ->object($script->getWarningWriter())
  190. ->isNotIdenticalTo($warningWriter)
  191. ->isEqualTo($defaultWarningWriter)
  192. ->object($script->getWarningWriter()->getCli())->isIdenticalTo($script->getCli())
  193. ;
  194. }
  195. public function testSetErrorWriter()
  196. {
  197. $this
  198. ->if($script = new mock\script($name = uniqid()))
  199. ->then
  200. ->object($script->setErrorWriter($errorWriter = new writers\std\out()))->isIdenticalTo($script)
  201. ->object($script->getErrorWriter())->isIdenticalTo($errorWriter)
  202. ->given(
  203. $defaultErrorWriter = new writers\std\err(),
  204. $defaultErrorWriter
  205. ->addDecorator(new writer\decorators\trim())
  206. ->addDecorator(new writer\decorators\prompt($script->getLocale()->_('Error: ')))
  207. ->addDecorator(new writer\decorators\eol())
  208. ->addDecorator(new atoum\cli\clear())
  209. )
  210. ->then
  211. ->object($script->setErrorWriter())->isIdenticalTo($script)
  212. ->object($script->getErrorWriter())
  213. ->isNotIdenticalTo($errorWriter)
  214. ->isEqualTo($defaultErrorWriter)
  215. ->object($script->getErrorWriter()->getCli())->isIdenticalTo($script->getCli())
  216. ;
  217. }
  218. public function testSetHelpWriter()
  219. {
  220. $this
  221. ->if($script = new mock\script($name = uniqid()))
  222. ->then
  223. ->object($script->setHelpWriter($helpWriter = new writers\std\out()))->isIdenticalTo($script)
  224. ->object($script->getHelpWriter())->isIdenticalTo($helpWriter)
  225. ->given(
  226. $labelColorizer = new atoum\cli\colorizer('0;32'),
  227. $labelColorizer->setPattern('/(^[^:]+: )/'),
  228. $argumentColorizer = new atoum\cli\colorizer('0;32'),
  229. $argumentColorizer->setPattern('/((?:^| )[-+]+[-a-z]+)/'),
  230. $valueColorizer = new atoum\cli\colorizer('0;34'),
  231. $valueColorizer->setPattern('/(<[^>]+>(?:\.\.\.)?)/'),
  232. $defaultHelpWriter = new writers\std\out(),
  233. $defaultHelpWriter
  234. ->addDecorator($labelColorizer)
  235. ->addDecorator($valueColorizer)
  236. ->addDecorator($argumentColorizer)
  237. ->addDecorator(new writer\decorators\rtrim())
  238. ->addDecorator(new writer\decorators\eol())
  239. ->addDecorator(new atoum\cli\clear())
  240. )
  241. ->then
  242. ->object($script->setHelpWriter())->isIdenticalTo($script)
  243. ->object($script->getHelpWriter())->isEqualTo($defaultHelpWriter)
  244. ;
  245. }
  246. public function testSetPrompt()
  247. {
  248. $this
  249. ->if($script = new mock\script(uniqid()))
  250. ->then
  251. ->object($script->setPrompt($prompt = new prompt()))->isIdenticalTo($script)
  252. ->object($script->getPrompt())->isIdenticalTo($prompt)
  253. ->object($prompt->getOutputWriter())->isIdenticalTo($script->getOutputWriter())
  254. ->given(
  255. $defaultPrompt = new prompt(),
  256. $defaultPrompt->setOutputWriter($script->getOutputWriter())
  257. )
  258. ->then
  259. ->object($script->setPrompt())->isIdenticalTo($script)
  260. ->object($script->getPrompt())
  261. ->isNotIdenticalTo($prompt)
  262. ->isEqualTo($defaultPrompt)
  263. ;
  264. }
  265. public function testAddArgumentHandler()
  266. {
  267. $this
  268. ->if($argumentsParser = new mock\script\arguments\parser())
  269. ->and($this->calling($argumentsParser)->addHandler = function() {})
  270. ->and($script = new mock\script($name = uniqid()))
  271. ->and($script->setArgumentsParser($argumentsParser))
  272. ->then
  273. ->object($script->addArgumentHandler($handlerA = function() {}, $argumentsA = array('-a')))->isIdenticalTo($script)
  274. ->mock($argumentsParser)->call('addHandler')->withArguments($handlerA, $argumentsA)->once()
  275. ->array($script->getHelp())->isEmpty()
  276. ->object($script->addArgumentHandler($handlerB = function() {}, $argumentsB = array('-b', '--b'), $valuesB = '<argumentB>'))->isIdenticalTo($script)
  277. ->mock($argumentsParser)->call('addHandler')->withArguments($handlerB, $argumentsB)->once()
  278. ->array($script->getHelp())->isEmpty()
  279. ->object($script->addArgumentHandler($handlerC = function() {}, $argumentsC = array('-c', '--c'), $valuesC = '<argumentC>', $helpC = 'help of C argument'))->isIdenticalTo($script)
  280. ->mock($argumentsParser)->call('addHandler')->withArguments($handlerC, $argumentsC)->once()
  281. ->array($script->getHelp())->isEqualTo(array(array($argumentsC, $valuesC, $helpC)))
  282. ;
  283. }
  284. public function testSetDefaultArgumentHandler()
  285. {
  286. $this
  287. ->if($argumentsParser = new mock\script\arguments\parser())
  288. ->and($this->calling($argumentsParser)->addHandler = function() {})
  289. ->and($script = new mock\script($name = uniqid()))
  290. ->and($script->setArgumentsParser($argumentsParser))
  291. ->then
  292. ->object($script->setDefaultArgumentHandler($defaultHandler = function($script, $argument) {}))->isIdenticalTo($script)
  293. ->mock($argumentsParser)->call('setDefaultHandler')->withArguments($defaultHandler)->once()
  294. ->array($script->getHelp())->isEmpty()
  295. ;
  296. }
  297. public function testIncreaseVerbosityLevel()
  298. {
  299. $this
  300. ->if($script = new mock\script(uniqid()))
  301. ->then
  302. ->object($script->increaseVerbosityLevel())->isIdenticalTo($script)
  303. ->integer($script->getVerbosityLevel())->isEqualTo(1)
  304. ->object($script->increaseVerbosityLevel())->isIdenticalTo($script)
  305. ->integer($script->getVerbosityLevel())->isEqualTo(2)
  306. ->object($script->increaseVerbosityLevel())->isIdenticalTo($script)
  307. ->integer($script->getVerbosityLevel())->isEqualTo(3)
  308. ;
  309. }
  310. public function testDecreaseVerbosityLevel()
  311. {
  312. $this
  313. ->if($script = new mock\script(uniqid()))
  314. ->then
  315. ->object($script->DecreaseVerbosityLevel())->isIdenticalTo($script)
  316. ->integer($script->getVerbosityLevel())->isZero()
  317. ->if($script->increaseVerbosityLevel())
  318. ->then
  319. ->object($script->DecreaseVerbosityLevel())->isIdenticalTo($script)
  320. ->integer($script->getVerbosityLevel())->isZero()
  321. ->object($script->DecreaseVerbosityLevel())->isIdenticalTo($script)
  322. ->integer($script->getVerbosityLevel())->isZero()
  323. ;
  324. }
  325. public function testResetVerbosityLevel()
  326. {
  327. $this
  328. ->if($script = new mock\script(uniqid()))
  329. ->then
  330. ->object($script->resetVerbosityLevel())->isIdenticalTo($script)
  331. ->integer($script->getVerbosityLevel())->isZero()
  332. ->if($script->increaseVerbosityLevel())
  333. ->and($script->increaseVerbosityLevel())
  334. ->then
  335. ->object($script->resetVerbosityLevel())->isIdenticalTo($script)
  336. ->integer($script->getVerbosityLevel())->isZero()
  337. ;
  338. }
  339. public function testHelp()
  340. {
  341. $this
  342. ->if($argumentsParser = new mock\script\arguments\parser())
  343. ->and($this->calling($argumentsParser)->addHandler = function() {})
  344. ->and($locale = new mock\locale())
  345. ->and($this->calling($locale)->_ = function($string) { return $string; })
  346. ->and($helpWriter = new mock\writers\std\out())
  347. ->and($this->calling($helpWriter)->write = function() {})
  348. ->and($script = new mock\script($name = uniqid()))
  349. ->and($script->setArgumentsParser($argumentsParser))
  350. ->and($script->setLocale($locale))
  351. ->and($script->setHelpWriter($helpWriter))
  352. ->then
  353. ->object($script->help())->isIdenticalTo($script)
  354. ->mock($helpWriter)->call('write')->never()
  355. ->if($script->addArgumentHandler(function() {}, array('-c', '--c'), $valuesC = '<argumentC>', $helpC = 'help of C argument'))
  356. ->then
  357. ->object($script->help())->isIdenticalTo($script)
  358. ->mock($locale)->call('_')->withArguments('Usage: %s [options]')->once()
  359. ->mock($helpWriter)
  360. ->call('write')
  361. ->withArguments('Usage: ' . $script->getName() . ' [options]')->once()
  362. ->withArguments('Available options are:')->once()
  363. ->withArguments(' -c <argumentC>, --c <argumentC>: help of C argument')->once()
  364. ;
  365. }
  366. public function testRun()
  367. {
  368. $this
  369. ->if($script = new mock\script(uniqid(), $adapter = new atoum\test\adapter()))
  370. ->and($argumentsParser = new mock\script\arguments\parser())
  371. ->and($this->calling($argumentsParser)->addHandler = function() {})
  372. ->and($script->setArgumentsParser($argumentsParser))
  373. ->then
  374. ->object($script->run())->isIdenticalTo($script)
  375. ->mock($argumentsParser)->call('parse')->withArguments($script, array())->once()
  376. ->adapter($adapter)->call('ini_set')->withArguments('log_errors_max_len', 0)->once()
  377. ->adapter($adapter)->call('ini_set')->withArguments('log_errors', 'Off')->once()
  378. ->adapter($adapter)->call('ini_set')->withArguments('display_errors', 'stderr')->once()
  379. ;
  380. }
  381. public function testPrompt()
  382. {
  383. $this
  384. ->if($prompt = new mock\script\prompt())
  385. ->and($this->calling($prompt)->ask = $answer = uniqid())
  386. ->and($script = new mock\script(uniqid()))
  387. ->and($script->setPrompt($prompt))
  388. ->then
  389. ->string($script->prompt($message = uniqid()))->isEqualTo($answer)
  390. ->mock($prompt)->call('ask')->withIdenticalArguments($message)->once()
  391. ->string($script->prompt(($message = ' ' . $message) . "\t\n"))->isEqualTo($answer)
  392. ->mock($prompt)->call('ask')->withIdenticalArguments($message)->once()
  393. ->if($this->calling($prompt)->ask = ' ' . ($answer = uniqid()) . "\t")
  394. ->then
  395. ->string($script->prompt($message = uniqid()))->isEqualTo($answer)
  396. ->mock($prompt)->call('ask')->withIdenticalArguments($message)->once()
  397. ;
  398. }
  399. public function testWriteMessage()
  400. {
  401. $this
  402. ->if($outputWriter = new mock\writers\std\out())
  403. ->and($this->calling($outputWriter)->write->doesNothing())
  404. ->and($script = new mock\script(uniqid()))
  405. ->and($script->setOutputWriter($outputWriter))
  406. ->then
  407. ->object($script->writeMessage($message = uniqid()))->isIdenticalTo($script)
  408. ->mock($outputWriter)
  409. ->call('write')
  410. ->withArguments($message)
  411. ->after($this->mock($outputWriter)->call('removeDecorators')->once())
  412. ->once()
  413. ;
  414. }
  415. public function testWriteInfo()
  416. {
  417. $this
  418. ->if($infoWriter = new mock\writers\std\out())
  419. ->and($this->calling($infoWriter)->write->doesNothing())
  420. ->and($script = new mock\script(uniqid()))
  421. ->and($script->setInfoWriter($infoWriter))
  422. ->then
  423. ->object($script->writeInfo($info = uniqid()))->isIdenticalTo($script)
  424. ->mock($infoWriter)->call('write')->withArguments($info)->once()
  425. ;
  426. }
  427. public function testWriteWarning()
  428. {
  429. $this
  430. ->if($errorWriter = new mock\writers\std\err())
  431. ->and($this->calling($errorWriter)->clear = $errorWriter)
  432. ->and($this->calling($errorWriter)->write->doesNothing())
  433. ->and($script = new mock\script(uniqid()))
  434. ->and($script->setWarningWriter($errorWriter))
  435. ->then
  436. ->object($script->writeWarning($warning = uniqid()))->isIdenticalTo($script)
  437. ->mock($errorWriter)->call('write')->withArguments($warning)->once()
  438. ;
  439. }
  440. public function testWriteError()
  441. {
  442. $this
  443. ->if($errorWriter = new mock\writers\std\err())
  444. ->and($this->calling($errorWriter)->clear = $errorWriter)
  445. ->and($this->calling($errorWriter)->write->doesNothing())
  446. ->and($script = new mock\script(uniqid()))
  447. ->and($script->setErrorWriter($errorWriter))
  448. ->then
  449. ->object($script->writeError($message = uniqid()))->isIdenticalTo($script)
  450. ->mock($errorWriter)->call('write')->withIdenticalArguments($message)->once()
  451. ;
  452. }
  453. public function testVerbose()
  454. {
  455. $this
  456. ->if($script = new mock\script(uniqid()))
  457. ->and($script->setInfoWriter($infoWriter = new mock\writers\std\out()))
  458. ->and($this->calling($infoWriter)->write->doesNothing())
  459. ->then
  460. ->object($script->verbose($message = uniqid()))->isIdenticalTo($script)
  461. ->mock($infoWriter)->call('write')->withIdenticalArguments($message . PHP_EOL)->never()
  462. ->if($script->increaseVerbosityLevel())
  463. ->then
  464. ->object($script->verbose($message = uniqid()))->isIdenticalTo($script)
  465. ->mock($infoWriter)->call('write')->withIdenticalArguments($message)->once()
  466. ->object($script->verbose($message, 1))->isIdenticalTo($script)
  467. ->mock($infoWriter)->call('write')->withIdenticalArguments($message)->twice()
  468. ->object($script->verbose($message, rand(2, PHP_INT_MAX)))->isIdenticalTo($script)
  469. ->mock($infoWriter)->call('write')->withIdenticalArguments($message)->twice()
  470. ->object($script->verbose($message = uniqid(), 0))->isIdenticalTo($script)
  471. ->mock($infoWriter)->call('write')->withIdenticalArguments($message)->never()
  472. ->object($script->verbose($message, 1))->isIdenticalTo($script)
  473. ->mock($infoWriter)->call('write')->withIdenticalArguments($message)->once()
  474. ;
  475. }
  476. public function testClearMessage()
  477. {
  478. $this
  479. ->if($script = new mock\script(uniqid()))
  480. ->and($script->setOutputWriter($outputWriter = new mock\writers\std\out()))
  481. ->and($this->calling($outputWriter)->clear->doesNothing())
  482. ->then
  483. ->object($script->clearMessage($message = uniqid()))->isIdenticalTo($script)
  484. ->mock($outputWriter)->call('clear')->once()
  485. ;
  486. }
  487. public function testWriteLabel()
  488. {
  489. $this
  490. ->if($script = new mock\script(uniqid()))
  491. ->and($script->setHelpWriter($helpWriter = new mock\writers\std\out()))
  492. ->and($this->calling($helpWriter)->write->doesNothing())
  493. ->then
  494. ->object($script->writeLabel($label = uniqid(), $message = uniqid()))->isIdenticalTo($script)
  495. ->mock($helpWriter)->call('write')->withIdenticalArguments($label . ': ' . $message)->once()
  496. ->object($script->writeLabel($label, $message, 0))->isIdenticalTo($script)
  497. ->mock($helpWriter)->call('write')->withIdenticalArguments($label . ': ' . $message)->exactly(2)
  498. ->object($script->writeLabel(($label = ' ' . $label) . PHP_EOL, ' ' . $message . ' ' . PHP_EOL))->isIdenticalTo($script)
  499. ->mock($helpWriter)->call('write')->withIdenticalArguments($label . ': ' . $message)->once()
  500. ->object($script->writeLabel($label, $message, 0))->isIdenticalTo($script)
  501. ->mock($helpWriter)->call('write')->withIdenticalArguments($label . ': ' . $message)->exactly(2)
  502. ->object($script->writeLabel($label = uniqid(), $message = uniqid(), 1))->isIdenticalTo($script)
  503. ->mock($helpWriter)->call('write')->withIdenticalArguments(atoum\script::padding . $label . ': ' . $message)->once()
  504. ->object($script->writeLabel($label, $message, 2))->isIdenticalTo($script)
  505. ->mock($helpWriter)->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . $label . ': ' . $message)->once()
  506. ;
  507. }
  508. public function testWriteLabels()
  509. {
  510. $this
  511. ->if($script = new mock\script(uniqid()))
  512. ->and($script->setHelpWriter($helpWriter = new mock\writers\std\out()))
  513. ->and($this->calling($helpWriter)->write->doesNothing())
  514. ->then
  515. ->object($script->writeLabels(array($label = uniqid() => $message = uniqid())))->isIdenticalTo($script)
  516. ->mock($helpWriter)->call('write')->withIdenticalArguments(atoum\script::padding . $label . ': ' . $message)->once()
  517. ->object($script->writeLabels(
  518. array(
  519. $label1 = uniqid() => $message1 = uniqid(),
  520. $label2 = uniqid() => $message2 = uniqid(),
  521. $label3 = uniqid() => $message3 = uniqid()
  522. )
  523. )
  524. )
  525. ->isIdenticalTo($script)
  526. ->mock($helpWriter)
  527. ->call('write')->withIdenticalArguments(atoum\script::padding . $label1 . ': ' . $message1)->once()
  528. ->call('write')->withIdenticalArguments(atoum\script::padding . $label2 . ': ' . $message2)->once()
  529. ->call('write')->withIdenticalArguments(atoum\script::padding . $label3 . ': ' . $message3)->once()
  530. ->object($script->writeLabels(
  531. array(
  532. $label1 = uniqid() => $message1 = uniqid(),
  533. $label2 = ' ' . uniqid() => $message2 = uniqid(),
  534. $label3 = uniqid() => $message3 = uniqid()
  535. )
  536. )
  537. )
  538. ->isIdenticalTo($script)
  539. ->mock($helpWriter)
  540. ->call('write')->withIdenticalArguments(atoum\script::padding . ' ' . $label1 . ': ' . $message1)->once()
  541. ->call('write')->withIdenticalArguments(atoum\script::padding . $label2 . ': ' . $message2)->once()
  542. ->call('write')->withIdenticalArguments(atoum\script::padding . ' ' . $label3 . ': ' . $message3)->once()
  543. ->object($script->writeLabels(array(
  544. $label1 = uniqid() => $message1 = uniqid(),
  545. $label2 = 'xx' . uniqid() => $message2 = uniqid(),
  546. $label3 = uniqid() => $message3 = uniqid()
  547. ), 3
  548. )
  549. )
  550. ->isIdenticalTo($script)
  551. ->mock($helpWriter)
  552. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . ' ' . $label1 . ': ' . $message1)->once()
  553. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . $label2 . ': ' . $message2)->once()
  554. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . ' ' . $label3 . ': ' . $message3)->once()
  555. ->object($script->writeLabels(array(
  556. $label1 = uniqid() => $message1 = uniqid(),
  557. $label2 = 'xx' . uniqid() => ($message21 = uniqid()) . PHP_EOL . ($message22 = uniqid()),
  558. $label3 = uniqid() => $message3 = uniqid()
  559. ), 3
  560. )
  561. )
  562. ->isIdenticalTo($script)
  563. ->mock($helpWriter)
  564. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . ' ' . $label1 . ': ' . $message1)->once()
  565. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . $label2 . ': ' . $message21)->once()
  566. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . ' ' . ': ' . $message22)->once()
  567. ->call('write')->withIdenticalArguments(atoum\script::padding . atoum\script::padding . atoum\script::padding . ' ' . $label3 . ': ' . $message3)->once()
  568. ;
  569. }
  570. public function testGetDirectory()
  571. {
  572. $this
  573. ->given($script = new mock\script($name = uniqid()))
  574. ->and($script->setAdapter($adapter = new atoum\test\adapter()))
  575. ->and($adapter->is_dir = true)
  576. ->and($adapter->dirname = $directory = uniqid())
  577. ->then
  578. ->string($script->getDirectory())->isEqualTo($directory . DIRECTORY_SEPARATOR)
  579. ->if($adapter->dirname = $directory . DIRECTORY_SEPARATOR)
  580. ->then
  581. ->string($script->getDirectory())->isEqualTo($directory . DIRECTORY_SEPARATOR)
  582. ->if($adapter->is_dir = false)
  583. ->and($adapter->getcwd = $currentDirectory = uniqid())
  584. ->then
  585. ->string($script->getDirectory())->isEqualTo($currentDirectory . DIRECTORY_SEPARATOR)
  586. ->and($adapter->getcwd = $currentDirectory . DIRECTORY_SEPARATOR)
  587. ->then
  588. ->string($script->getDirectory())->isEqualTo($currentDirectory . DIRECTORY_SEPARATOR)
  589. ;
  590. }
  591. }