PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/scripts/runner.php

http://github.com/mageekguy/atoum
PHP | 1020 lines | 976 code | 44 blank | 0 comment | 1 complexity | 152f8fcb78fb78acaef1c0143ad9a396 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\scripts;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\cli,
  6. mageekguy\atoum\writer,
  7. mageekguy\atoum\mock\stream,
  8. mock\mageekguy\atoum as mock,
  9. mageekguy\atoum\scripts\runner as testedClass
  10. ;
  11. require_once __DIR__ . '/../../runner.php';
  12. class runner extends atoum\test
  13. {
  14. public function testClass()
  15. {
  16. $this->testedClass->extends('mageekguy\atoum\script\configurable');
  17. }
  18. public function testClassConstants()
  19. {
  20. $this
  21. ->string(testedClass::defaultConfigFile)->isEqualTo('.atoum.php')
  22. ->string(testedClass::defaultBootstrapFile)->isEqualTo('.bootstrap.atoum.php')
  23. ;
  24. }
  25. public function test__construct()
  26. {
  27. $this
  28. ->if($runner = new testedClass($name = uniqid()))
  29. ->then
  30. ->boolean($runner->hasDefaultArguments())->isFalse()
  31. ->array($runner->getDefaultArguments())->isEmpty()
  32. ->string($runner->getName())->isEqualTo($name)
  33. ->object($runner->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  34. ->object($runner->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  35. ->object($runner->getIncluder())->isInstanceOf('mageekguy\atoum\includer')
  36. ->object($runner->getRunner())->isInstanceOf('mageekguy\atoum\runner')
  37. ->variable($runner->getScoreFile())->isNull()
  38. ->array($runner->getReports())->isEmpty()
  39. ->array($runner->getArguments())->isEmpty()
  40. ->array($runner->getHelp())->isEqualTo(array(
  41. array(
  42. array('-h', '--help'),
  43. null,
  44. 'Display this help'
  45. ),
  46. array(
  47. array('-c', '--configurations'),
  48. '<file>...',
  49. 'Use all configuration files <file>'
  50. ),
  51. array(
  52. array('-v', '--version'),
  53. null,
  54. 'Display version'
  55. ),
  56. array(
  57. array('+verbose', '++verbose'),
  58. null,
  59. 'Enable verbose mode'
  60. ),
  61. array(
  62. array('--init'),
  63. '<path/to/directory>',
  64. sprintf($runner->getLocale()->_('Create configuration and bootstrap files in <path/to/directory> (Optional, default: %s)'), $runner->getDirectory())
  65. ),
  66. array(
  67. array('-p', '--php'),
  68. '<path/to/php/binary>',
  69. 'Path to PHP binary which must be used to run tests'
  70. ),
  71. array(
  72. array('-drt', '--default-report-title'),
  73. '<string>',
  74. 'Define default report title with <string>'
  75. ),
  76. array(
  77. array('-sf', '--score-file'),
  78. '<file>',
  79. 'Save score in file <file>'
  80. ),
  81. array(
  82. array('-mcn', '--max-children-number'),
  83. '<integer>',
  84. 'Maximum number of sub-processus which will be run simultaneously'
  85. ),
  86. array(
  87. array('-ncc', '--no-code-coverage'),
  88. null,
  89. 'Disable code coverage'
  90. ),
  91. array(
  92. array('-nccid', '--no-code-coverage-in-directories'),
  93. '<directory>...',
  94. 'Disable code coverage in directories <directory>'
  95. ),
  96. array(
  97. array('-nccfns', '--no-code-coverage-for-namespaces'),
  98. '<namespace>...',
  99. 'Disable code coverage for namespaces <namespace>'
  100. ),
  101. array(
  102. array('-nccfc', '--no-code-coverage-for-classes'),
  103. '<class>...',
  104. 'Disable code coverage for classes <class>'
  105. ),
  106. array(
  107. array('-nccfm', '--no-code-coverage-for-methods'),
  108. '<method>...',
  109. 'Disable code coverage for methods <method>'
  110. ),
  111. array(
  112. array('-f', '--files'),
  113. '<file>...',
  114. 'Execute all unit test files <file>'
  115. ),
  116. array(
  117. array('-d', '--directories'),
  118. '<directory>...',
  119. 'Execute unit test files in all <directory>'
  120. ),
  121. array(
  122. array('-tfe', '--test-file-extensions'),
  123. '<extension>...',
  124. 'Execute unit test files with one of extensions <extension>'
  125. ),
  126. array(
  127. array('-g', '--glob'),
  128. '<pattern>...',
  129. 'Execute unit test files which match <pattern>'
  130. ),
  131. array(
  132. array('-t', '--tags'),
  133. '<tag>...',
  134. 'Execute only unit test with tags <tag>'
  135. ),
  136. array(
  137. array('-m', '--methods'),
  138. '<class::method>...',
  139. 'Execute all <class::method>, * may be used as wildcard for class name or method name'
  140. ),
  141. array(
  142. array('-ns', '--namespaces'),
  143. '<namespace>...',
  144. 'Execute all classes in all namespaces <namespace>'
  145. ),
  146. array(
  147. array('-l', '--loop'),
  148. null,
  149. 'Execute tests in an infinite loop'
  150. ),
  151. array(
  152. array('--test-it'),
  153. null,
  154. 'Execute atoum unit tests'
  155. ),
  156. array(
  157. array('--test-all'),
  158. null,
  159. 'DEPRECATED, please do $runner->addTestsFromDirectory(\'path/to/default/tests/directory\') in a configuration file and use atoum without any argument instead'
  160. ),
  161. array(
  162. array('-ft', '--force-terminal'),
  163. null,
  164. 'Force output as in terminal'
  165. ),
  166. array(
  167. array('-bf', '--bootstrap-file'),
  168. '<file>',
  169. 'Include <file> before executing each test method'
  170. ),
  171. array(
  172. array('-ulr', '--use-light-report'),
  173. null,
  174. 'Use "light" CLI report'
  175. ),
  176. array(
  177. array('-utr', '--use-tap-report'),
  178. null,
  179. 'Use TAP report'
  180. ),
  181. array(
  182. array('--debug'),
  183. null,
  184. 'Enable debug mode'
  185. ),
  186. array(
  187. array('-xc', '--xdebug-config'),
  188. null,
  189. 'Set XDEBUG_CONFIG variable'
  190. ),
  191. array(
  192. array('-fivm', '--fail-if-void-methods'),
  193. null,
  194. 'Make the test suite fail if there is at least one void test method'
  195. ),
  196. array(
  197. array('-fism', '--fail-if-skipped-methods'),
  198. null,
  199. 'Make the test suite fail if there is at least one skipped test method'
  200. )
  201. )
  202. )
  203. ->if($runner = new testedClass($name = uniqid(), $adapter = new atoum\adapter()))
  204. ->then
  205. ->string($runner->getName())->isEqualTo($name)
  206. ->object($runner->getAdapter())->isIdenticalTo($adapter)
  207. ->object($runner->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  208. ->object($runner->getIncluder())->isInstanceOf('mageekguy\atoum\includer')
  209. ->object($runner->getRunner())->isInstanceOf('mageekguy\atoum\runner')
  210. ->variable($runner->getScoreFile())->isNull()
  211. ->array($runner->getArguments())->isEmpty()
  212. ->array($runner->getHelp())->isEqualTo(array(
  213. array(
  214. array('-h', '--help'),
  215. null,
  216. 'Display this help'
  217. ),
  218. array(
  219. array('-c', '--configurations'),
  220. '<file>...',
  221. 'Use all configuration files <file>'
  222. ),
  223. array(
  224. array('-v', '--version'),
  225. null,
  226. 'Display version'
  227. ),
  228. array(
  229. array('+verbose', '++verbose'),
  230. null,
  231. 'Enable verbose mode'
  232. ),
  233. array(
  234. array('--init'),
  235. '<path/to/directory>',
  236. sprintf($runner->getLocale()->_('Create configuration and bootstrap files in <path/to/directory> (Optional, default: %s)'), $runner->getDirectory())
  237. ),
  238. array(
  239. array('-p', '--php'),
  240. '<path/to/php/binary>',
  241. 'Path to PHP binary which must be used to run tests'
  242. ),
  243. array(
  244. array('-drt', '--default-report-title'),
  245. '<string>',
  246. 'Define default report title with <string>'
  247. ),
  248. array(
  249. array('-sf', '--score-file'),
  250. '<file>',
  251. 'Save score in file <file>'
  252. ),
  253. array(
  254. array('-mcn', '--max-children-number'),
  255. '<integer>',
  256. 'Maximum number of sub-processus which will be run simultaneously'
  257. ),
  258. array(
  259. array('-ncc', '--no-code-coverage'),
  260. null,
  261. 'Disable code coverage'
  262. ),
  263. array(
  264. array('-nccid', '--no-code-coverage-in-directories'),
  265. '<directory>...',
  266. 'Disable code coverage in directories <directory>'
  267. ),
  268. array(
  269. array('-nccfns', '--no-code-coverage-for-namespaces'),
  270. '<namespace>...',
  271. 'Disable code coverage for namespaces <namespace>'
  272. ),
  273. array(
  274. array('-nccfc', '--no-code-coverage-for-classes'),
  275. '<class>...',
  276. 'Disable code coverage for classes <class>'
  277. ),
  278. array(
  279. array('-nccfm', '--no-code-coverage-for-methods'),
  280. '<method>...',
  281. 'Disable code coverage for methods <method>'
  282. ),
  283. array(
  284. array('-f', '--files'),
  285. '<file>...',
  286. 'Execute all unit test files <file>'
  287. ),
  288. array(
  289. array('-d', '--directories'),
  290. '<directory>...',
  291. 'Execute unit test files in all <directory>'
  292. ),
  293. array(
  294. array('-tfe', '--test-file-extensions'),
  295. '<extension>...',
  296. 'Execute unit test files with one of extensions <extension>'
  297. ),
  298. array(
  299. array('-g', '--glob'),
  300. '<pattern>...',
  301. 'Execute unit test files which match <pattern>'
  302. ),
  303. array(
  304. array('-t', '--tags'),
  305. '<tag>...',
  306. 'Execute only unit test with tags <tag>'
  307. ),
  308. array(
  309. array('-m', '--methods'),
  310. '<class::method>...',
  311. 'Execute all <class::method>, * may be used as wildcard for class name or method name'
  312. ),
  313. array(
  314. array('-ns', '--namespaces'),
  315. '<namespace>...',
  316. 'Execute all classes in all namespaces <namespace>'
  317. ),
  318. array(
  319. array('-l', '--loop'),
  320. null,
  321. 'Execute tests in an infinite loop'
  322. ),
  323. array(
  324. array('--test-it'),
  325. null,
  326. 'Execute atoum unit tests'
  327. ),
  328. array(
  329. array('--test-all'),
  330. null,
  331. 'DEPRECATED, please do $runner->addTestsFromDirectory(\'path/to/default/tests/directory\') in a configuration file and use atoum without any argument instead'
  332. ),
  333. array(
  334. array('-ft', '--force-terminal'),
  335. null,
  336. 'Force output as in terminal'
  337. ),
  338. array(
  339. array('-bf', '--bootstrap-file'),
  340. '<file>',
  341. 'Include <file> before executing each test method'
  342. ),
  343. array(
  344. array('-ulr', '--use-light-report'),
  345. null,
  346. 'Use "light" CLI report'
  347. ),
  348. array(
  349. array('-utr', '--use-tap-report'),
  350. null,
  351. 'Use TAP report'
  352. ),
  353. array(
  354. array('--debug'),
  355. null,
  356. 'Enable debug mode'
  357. ),
  358. array(
  359. array('-xc', '--xdebug-config'),
  360. null,
  361. 'Set XDEBUG_CONFIG variable'
  362. ),
  363. array(
  364. array('-fivm', '--fail-if-void-methods'),
  365. null,
  366. 'Make the test suite fail if there is at least one void test method'
  367. ),
  368. array(
  369. array('-fism', '--fail-if-skipped-methods'),
  370. null,
  371. 'Make the test suite fail if there is at least one skipped test method'
  372. )
  373. )
  374. )
  375. ;
  376. }
  377. public function testSetArguments()
  378. {
  379. $this
  380. ->if($runner = new testedClass($name = uniqid()))
  381. ->then
  382. ->object($runner->setArguments(array()))->isIdenticalTo($runner)
  383. ->array($runner->getArguments())->isEmpty()
  384. ->object($runner->setArguments($arguments = array(uniqid(), uniqid(), uniqid())))->isIdenticalTo($runner)
  385. ->array($runner->getArguments())->isEqualTo($arguments)
  386. ;
  387. }
  388. public function testUseConfigFile()
  389. {
  390. $this
  391. ->if($runner = new testedClass(uniqid()))
  392. ->and($runner->setLocale($locale = new \mock\mageekguy\atoum\locale()))
  393. ->then
  394. ->exception(function() use ($runner, & $file) {
  395. $runner->useConfigFile($file = uniqid());
  396. }
  397. )
  398. ->isInstanceOf('mageekguy\atoum\includer\exception')
  399. ->hasMessage('Unable to find configuration file \'' . $file . '\'')
  400. ->mock($locale)->call('_')->withArguments('Unable to find configuration file \'%s\'')->once()
  401. ->if($configFile = stream::get())
  402. ->and($configFile->file_get_contents = '<?php $runner->disableCodeCoverage(); ?>')
  403. ->then
  404. ->boolean($runner->getRunner()->codeCoverageIsEnabled())->isTrue()
  405. ->object($runner->useConfigFile((string) $configFile))->isIdenticalTo($runner)
  406. ->boolean($runner->getRunner()->codeCoverageIsEnabled())->isFalse()
  407. ;
  408. }
  409. public function testAddTestAllDirectory()
  410. {
  411. $this
  412. ->if($runner = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  413. ->and($stderr = new \mock\mageekguy\atoum\writers\std\err())
  414. ->and($this->calling($stderr)->clear = $stderr)
  415. ->and($this->calling($stderr)->write = function() {})
  416. ->and($runner->setErrorWriter($stderr))
  417. ->then
  418. ->object($runner->addTestAllDirectory(uniqid()))->isIdenticalTo($runner)
  419. ->mock($stderr)->call('write')->withArguments('--test-all argument is deprecated, please replace call to mageekguy\atoum\scripts\runner::addTestAllDirectory(\'path/to/default/tests/directory\') by $runner->addTestsFromDirectory(\'path/to/default/tests/directory\') in your configuration files and use atoum without any argument instead')->once()
  420. ;
  421. }
  422. public function testAddDefaultReport()
  423. {
  424. $this
  425. ->if($runner = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  426. ->then
  427. ->object($report = $runner->addDefaultReport())->isInstanceOf('mageekguy\atoum\reports\realtime\cli')
  428. ->array($report->getWriters())->isEqualTo(array(new atoum\writers\std\out()))
  429. ;
  430. }
  431. public function testAddReport()
  432. {
  433. $this
  434. ->if($runner = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  435. ->then
  436. ->object($runner->addReport($report = new \mock\mageekguy\atoum\report()))->isIdenticalTo($runner)
  437. ->array($runner->getReports())->isEqualTo(array($report))
  438. ->object($runner->addReport($otherReport = new \mock\mageekguy\atoum\report()))->isIdenticalTo($runner)
  439. ->array($runner->getReports())->isEqualTo(array($report, $otherReport))
  440. ;
  441. }
  442. public function testSetReport()
  443. {
  444. $this
  445. ->if($runner = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  446. ->then
  447. ->object($runner->setReport($report = new \mock\mageekguy\atoum\report()))->isIdenticalTo($runner)
  448. ->array($runner->getReports())->isEqualTo(array($report))
  449. ->object($runner->setReport($otherReport = new \mock\mageekguy\atoum\report()))->isIdenticalTo($runner)
  450. ->array($runner->getReports())->isEqualTo(array($otherReport))
  451. ->if($runner->addReport($report))
  452. ->then
  453. ->array($runner->getReports())->isEqualTo(array($otherReport))
  454. ;
  455. }
  456. public function testSetNamespaces()
  457. {
  458. $this
  459. ->if($runner = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  460. ->then
  461. ->object($runner->testNamespaces(array()))->isIdenticalTo($runner)
  462. ->array($runner->getTestedNamespaces())->isEmpty()
  463. ->object($runner->testNamespaces(array('foo', '\bar', 'foo\bar\\', '\this\is\a\namespace\\')))->isIdenticalTo($runner)
  464. ->array($runner->getTestedNamespaces())->isEqualTo(array('foo', 'bar', 'foo\bar', 'this\is\a\namespace'))
  465. ;
  466. }
  467. public function testSetPhpPath()
  468. {
  469. $this
  470. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  471. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  472. ->then
  473. ->object($script->setPhpPath($phpPath = uniqid()))->isIdenticalTo($script)
  474. ->mock($runner)->call('setPhpPath')->withArguments($phpPath)->once()
  475. ;
  476. }
  477. public function testSetDefaultReportTitle()
  478. {
  479. $this
  480. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  481. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  482. ->then
  483. ->object($script->setDefaultReportTitle($reportTitle = uniqid()))->isIdenticalTo($script)
  484. ->mock($runner)->call('setDefaultReportTitle')->withArguments($reportTitle)->once()
  485. ;
  486. }
  487. public function testSetMaxChildrenNumber()
  488. {
  489. $this
  490. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  491. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  492. ->then
  493. ->object($script->setMaxChildrenNumber($childrenNumber = rand(1, PHP_INT_MAX)))->isIdenticalTo($script)
  494. ->mock($runner)->call('setMaxChildrenNumber')->withArguments($childrenNumber)->once()
  495. ;
  496. }
  497. public function testDisableCodeCoverage()
  498. {
  499. $this
  500. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  501. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  502. ->then
  503. ->object($script->disableCodeCoverage($childrenNumber = rand(1, PHP_INT_MAX)))->isIdenticalTo($script)
  504. ->mock($runner)->call('disableCodeCoverage')->withoutAnyArgument()->once()
  505. ;
  506. }
  507. public function testExcludeNamespacesFromCoverage()
  508. {
  509. $this
  510. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  511. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  512. ->and($this->calling($runner)->getCoverage = $coverage = new \mock\mageekguy\atoum\score\coverage())
  513. ->then
  514. ->object($script->excludeNamespacesFromCoverage(array('foo', 'bar')))->isIdenticalTo($script)
  515. ->mock($coverage)->call('excludeNamespace')
  516. ->withArguments('foo')->once()
  517. ->withArguments('bar')->once()
  518. ;
  519. }
  520. public function testExcludeDirectoriesFromCoverage()
  521. {
  522. $this
  523. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  524. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  525. ->and($this->calling($runner)->getCoverage = $coverage = new \mock\mageekguy\atoum\score\coverage())
  526. ->then
  527. ->object($script->excludeDirectoriesFromCoverage(array('foo', 'bar')))->isIdenticalTo($script)
  528. ->mock($coverage)->call('excludeDirectory')
  529. ->withArguments('foo')->once()
  530. ->withArguments('bar')->once()
  531. ;
  532. }
  533. public function testExcludeClassesFromCoverage()
  534. {
  535. $this
  536. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  537. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  538. ->and($this->calling($runner)->getCoverage = $coverage = new \mock\mageekguy\atoum\score\coverage())
  539. ->then
  540. ->object($script->excludeClassesFromCoverage(array('foo', 'bar')))->isIdenticalTo($script)
  541. ->mock($coverage)->call('excludeClass')
  542. ->withArguments('foo')->once()
  543. ->withArguments('bar')->once()
  544. ;
  545. }
  546. public function testAddTest()
  547. {
  548. $this
  549. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  550. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  551. ->then
  552. ->object($script->addTest($testPath = uniqid()))->isIdenticalTo($script)
  553. ->mock($runner)->call('addTest')->withArguments($testPath)->once()
  554. ;
  555. }
  556. public function testAddTests()
  557. {
  558. $this
  559. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  560. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  561. ->then
  562. ->object($script->addTests(array($testPath1 = uniqid(), $testPath2 = uniqid())))->isIdenticalTo($script)
  563. ->mock($runner)
  564. ->call('addTest')
  565. ->withArguments($testPath1)->once()
  566. ->withArguments($testPath2)->once()
  567. ;
  568. }
  569. public function testAddTestsFromDirectory()
  570. {
  571. $this
  572. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  573. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  574. ->and($this->calling($runner)->addTestsFromDirectory->doesNothing())
  575. ->then
  576. ->object($script->addTestsFromDirectory($directory = uniqid()))->isIdenticalTo($script)
  577. ->mock($runner)->call('addTestsFromDirectory')->withArguments($directory)->once()
  578. ;
  579. }
  580. public function testAddTestsFromDirectories()
  581. {
  582. $this
  583. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  584. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  585. ->and($this->calling($runner)->addTestsFromDirectory->doesNothing())
  586. ->then
  587. ->object($script->addTestsFromDirectories(array($directory1 = uniqid(), $directory2 = uniqid())))->isIdenticalTo($script)
  588. ->mock($runner)
  589. ->call('addTestsFromDirectory')
  590. ->withArguments($directory1)->once()
  591. ->withArguments($directory2)->once()
  592. ;
  593. }
  594. public function testAddTestsFromPattern()
  595. {
  596. $this
  597. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  598. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  599. ->and($this->calling($runner)->addTestsFromPattern->doesNothing())
  600. ->then
  601. ->object($script->addTestsFromPattern($pattern = uniqid()))->isIdenticalTo($script)
  602. ->mock($runner)->call('addTestsFromPattern')->withArguments($pattern)->once()
  603. ;
  604. }
  605. public function testAddTestsFromPatterns()
  606. {
  607. $this
  608. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  609. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  610. ->and($this->calling($runner)->addTestsFromPattern->doesNothing())
  611. ->then
  612. ->object($script->addTestsFromPatterns(array($pattern1 = uniqid(), $pattern2 = uniqid())))->isIdenticalTo($script)
  613. ->mock($runner)
  614. ->call('addTestsFromPattern')
  615. ->withArguments($pattern1)->once()
  616. ->withArguments($pattern2)->once()
  617. ;
  618. }
  619. public function testAcceptTestFileExtensions()
  620. {
  621. $this
  622. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  623. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  624. ->and($this->calling($runner)->acceptTestFileExtensions->doesNothing())
  625. ->then
  626. ->object($script->acceptTestFileExtensions($testFileExtensions = array(uniqid(), uniqid())))->isIdenticalTo($script)
  627. ->mock($runner)->call('acceptTestFileExtensions')->withArguments($testFileExtensions)->once()
  628. ;
  629. }
  630. public function testSetBootstrapFile()
  631. {
  632. $this
  633. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  634. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  635. ->and($this->calling($runner)->setBootstrapFile->doesNothing())
  636. ->then
  637. ->object($script->setBootstrapFile($bootstrapFile = uniqid()))->isIdenticalTo($script)
  638. ->mock($runner)->call('setBootstrapFile')->withArguments($bootstrapFile)->once()
  639. ;
  640. }
  641. public function testSetXdebugConfig()
  642. {
  643. $this
  644. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  645. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  646. ->and($this->calling($runner)->setXdebugConfig->doesNothing())
  647. ->then
  648. ->object($script->setXdebugConfig($xdebugConfig = uniqid()))->isIdenticalTo($script)
  649. ->mock($runner)->call('setXdebugConfig')->withArguments($xdebugConfig)->once()
  650. ;
  651. }
  652. public function testFailIfVoidMethods()
  653. {
  654. $this
  655. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  656. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  657. ->then
  658. ->object($script->failIfVoidMethods())->isIdenticalTo($script)
  659. ->mock($runner)->call('failIfVoidMethods')->once()
  660. ;
  661. }
  662. public function testDoNotFailIfVoidMethods()
  663. {
  664. $this
  665. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  666. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  667. ->then
  668. ->object($script->doNotFailIfVoidMethods())->isIdenticalTo($script)
  669. ->mock($runner)->call('doNotFailIfVoidMethods')->once()
  670. ;
  671. }
  672. public function testShouldFailIfVoidMethods()
  673. {
  674. $this
  675. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  676. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  677. ->then
  678. ->boolean($script->shouldFailIfVoidMethods())->isFalse()
  679. ->mock($runner)->call('shouldFailIfVoidMethods')->once()
  680. ->if($this->calling($runner)->shouldFailIfVoidMethods = true)
  681. ->then
  682. ->boolean($script->shouldFailIfVoidMethods())->isTrue()
  683. ->if($this->calling($runner)->shouldFailIfVoidMethods = false)
  684. ->then
  685. ->boolean($script->shouldFailIfVoidMethods())->isFalse()
  686. ;
  687. }
  688. public function testFailIfSkippedMethods()
  689. {
  690. $this
  691. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  692. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  693. ->then
  694. ->object($script->failIfSkippedMethods())->isIdenticalTo($script)
  695. ->mock($runner)->call('failIfSkippedMethods')->once()
  696. ;
  697. }
  698. public function testDoNotFailIfSkippedMethods()
  699. {
  700. $this
  701. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  702. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  703. ->then
  704. ->object($script->doNotFailIfSkippedMethods())->isIdenticalTo($script)
  705. ->mock($runner)->call('doNotFailIfSkippedMethods')->once()
  706. ;
  707. }
  708. public function testShouldFailIfSkippedMethods()
  709. {
  710. $this
  711. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  712. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  713. ->then
  714. ->boolean($script->shouldFailIfSkippedMethods())->isFalse()
  715. ->mock($runner)->call('shouldFailIfSkippedMethods')->once()
  716. ->if($this->calling($runner)->shouldFailIfSkippedMethods = true)
  717. ->then
  718. ->boolean($script->shouldFailIfSkippedMethods())->isTrue()
  719. ->if($this->calling($runner)->shouldFailIfSkippedMethods = false)
  720. ->then
  721. ->boolean($script->shouldFailIfSkippedMethods())->isFalse()
  722. ;
  723. }
  724. public function testEnableDebugMode()
  725. {
  726. $this
  727. ->if($script = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  728. ->and($script->setRunner($runner = new \mock\mageekguy\atoum\runner()))
  729. ->and($this->calling($runner)->enableDebugMode->doesNothing())
  730. ->then
  731. ->object($script->enableDebugMode())->isIdenticalTo($script)
  732. ->mock($runner)->call('enableDebugMode')->withoutAnyArgument()->once()
  733. ;
  734. }
  735. public function testAddDefaultArguments()
  736. {
  737. $this
  738. ->if($runner = new \mock\mageekguy\atoum\scripts\runner(uniqid()))
  739. ->then
  740. ->object($runner->addDefaultArguments($arg1 = uniqid()))->isInstanceOf($runner)
  741. ->boolean($runner->hasDefaultArguments())->isTrue()
  742. ->array($runner->getDefaultArguments())->isEqualTo(array($arg1))
  743. ->object($runner->addDefaultArguments($arg2 = uniqid(), $arg3 = uniqid()))->isInstanceOf($runner)
  744. ->boolean($runner->hasDefaultArguments())->isTrue()
  745. ->array($runner->getDefaultArguments())->isEqualTo(array($arg1, $arg2, $arg3))
  746. ;
  747. }
  748. public function testInit()
  749. {
  750. $this
  751. ->given($runner = new testedClass(__FILE__))
  752. ->and($runner->setAdapter($adapter = new atoum\test\adapter()))
  753. ->and($runner->setInfoWriter($outputWriter = new \mock\mageekguy\atoum\writers\std\out()))
  754. ->and($runner->setPrompt($prompt = new \mock\mageekguy\atoum\script\prompt()))
  755. ->and($adapter->copy = true)
  756. ->and($adapter->file_exists = false)
  757. ->and($this->calling($outputWriter)->write = function() {})
  758. ->then
  759. ->object($runner->init())->isIdenticalTo($runner)
  760. ->mock($prompt)
  761. ->call('ask')
  762. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->never()
  763. ->mock($outputWriter)
  764. ->call('write')
  765. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' was successfully created in ' . $runner->getDirectory())->once()
  766. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' was successfully created in ' . $runner->getDirectory())->once()
  767. ->adapter($adapter)
  768. ->call('copy')
  769. ->withArguments(atoum\directory . '/resources/configurations/runner/atoum.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultConfigFile)->once()
  770. ->withArguments(atoum\directory . '/resources/configurations/runner/bootstrap.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultBootstrapFile)->once()
  771. ->if($this->resetAdapter($adapter))
  772. ->and($this->resetMock($outputWriter))
  773. ->then
  774. ->object($runner->init($directory = uniqid()))->isIdenticalTo($runner)
  775. ->mock($prompt)
  776. ->call('ask')
  777. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' already exists in ' . $directory . ', type \'Y\' to overwrite it...')->never()
  778. ->mock($outputWriter)
  779. ->call('write')
  780. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' was successfully created in ' . $directory . DIRECTORY_SEPARATOR)->once()
  781. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' was successfully created in ' . $directory . DIRECTORY_SEPARATOR)->once()
  782. ->adapter($adapter)
  783. ->call('copy')
  784. ->withArguments(atoum\directory . '/resources/configurations/runner/atoum.php.dist', $directory . DIRECTORY_SEPARATOR . testedClass::defaultConfigFile)->once()
  785. ->withArguments(atoum\directory . '/resources/configurations/runner/bootstrap.php.dist', $directory . DIRECTORY_SEPARATOR . testedClass::defaultBootstrapFile)->once()
  786. ->if($this->resetAdapter($adapter))
  787. ->and($this->resetMock($outputWriter))
  788. ->and($adapter->file_exists = true)
  789. ->and($this->calling($prompt)->ask = 'Y')
  790. ->then
  791. ->object($runner->init())->isIdenticalTo($runner)
  792. ->mock($prompt)
  793. ->call('ask')
  794. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->once()
  795. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->once()
  796. ->mock($outputWriter)
  797. ->call('write')
  798. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' was successfully created in ' . $runner->getDirectory())->once()
  799. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' was successfully created in ' . $runner->getDirectory())->once()
  800. ->adapter($adapter)
  801. ->call('copy')
  802. ->withArguments(atoum\directory . '/resources/configurations/runner/atoum.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultConfigFile)->once()
  803. ->withArguments(atoum\directory . '/resources/configurations/runner/bootstrap.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultBootstrapFile)->once()
  804. ->if($this->resetAdapter($adapter))
  805. ->and($this->resetMock($outputWriter))
  806. ->and($this->resetMock($prompt))
  807. ->and($adapter->file_exists = true)
  808. ->and($this->calling($prompt)->ask = 'y')
  809. ->then
  810. ->object($runner->init())->isIdenticalTo($runner)
  811. ->mock($prompt)
  812. ->call('ask')
  813. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->once()
  814. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->once()
  815. ->mock($outputWriter)
  816. ->call('write')
  817. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' was successfully created in ' . $runner->getDirectory())->never()
  818. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' was successfully created in ' . $runner->getDirectory())->never()
  819. ->adapter($adapter)
  820. ->call('copy')
  821. ->withArguments(atoum\directory . '/resources/configurations/runner/atoum.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultConfigFile)->never()
  822. ->withArguments(atoum\directory . '/resources/configurations/runner/bootstrap.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultBootstrapFile)->never()
  823. ->if($this->resetAdapter($adapter))
  824. ->and($this->resetMock($outputWriter))
  825. ->and($this->resetMock($prompt))
  826. ->and($adapter->file_exists = true)
  827. ->and($this->calling($prompt)->ask = uniqid())
  828. ->then
  829. ->object($runner->init())->isIdenticalTo($runner)
  830. ->mock($prompt)
  831. ->call('ask')
  832. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->once()
  833. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' already exists in ' . $runner->getDirectory() . ', type \'Y\' to overwrite it...')->once()
  834. ->mock($outputWriter)
  835. ->call('write')
  836. ->withArguments('Default configuration file \'' . testedClass::defaultConfigFile . '\' was successfully created in ' . $runner->getDirectory() . PHP_EOL)->never()
  837. ->withArguments('Default bootstrap file \'' . testedClass::defaultBootstrapFile . '\' was successfully created in ' . $runner->getDirectory() . PHP_EOL)->never()
  838. ->adapter($adapter)
  839. ->call('copy')
  840. ->withArguments(atoum\directory . '/resources/configurations/runner/atoum.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultConfigFile)->never()
  841. ->withArguments(atoum\directory . '/resources/configurations/runner/bootstrap.php.dist', __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultBootstrapFile)->never()
  842. ->if($this->calling($prompt)->ask = 'Y')
  843. ->and($adapter->copy = false)
  844. ->then
  845. ->exception(function() use ($runner) { $runner->init(); })
  846. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  847. ->hasMessage('Unable to write \'' . atoum\directory . '/resources/configurations/runner/atoum.php.dist\' to \'' . __DIR__ . DIRECTORY_SEPARATOR . testedClass::defaultConfigFile . '\'')
  848. ;
  849. }
  850. public function testSetInfoWriter()
  851. {
  852. $this
  853. ->given($runner = new testedClass(uniqid()))
  854. ->then
  855. ->object($runner->setInfoWriter($errorWriter = new atoum\writers\std\err()))->isIdenticalTo($runner)
  856. ->object($runner->getInfoWriter())->isIdenticalTo($errorWriter)
  857. ->given(
  858. $defaultInfoWriter = new atoum\writers\std\out(),
  859. $defaultInfoWriter
  860. ->addDecorator(new writer\decorators\rtrim())
  861. ->addDecorator(new writer\decorators\eol())
  862. ->addDecorator(new atoum\cli\clear())
  863. ->addDecorator(new cli\colorizer('0;32'))
  864. )
  865. ->then
  866. ->object($runner->setInfoWriter())->isIdenticalTo($runner)
  867. ->object($runner->getInfoWriter())->isEqualTo($defaultInfoWriter)
  868. ;
  869. }
  870. public function testSetWarningWriter()
  871. {
  872. $this
  873. ->given($runner = new testedClass(uniqid()))
  874. ->then
  875. ->object($runner->setWarningWriter($warningWriter = new atoum\writers\std\err()))->isIdenticalTo($runner)
  876. ->object($runner->getWarningWriter())->isIdenticalTo($warningWriter)
  877. ->given($colorizer = new cli\colorizer('0;33'))
  878. ->and($colorizer->setPattern('/^([^:]+:)/'))
  879. ->and($defaultWarningWriter = new atoum\writers\std\err())
  880. ->and($defaultWarningWriter->addDecorator($colorizer))
  881. ->then
  882. ->object($runner->setWarningWriter())->isIdenticalTo($runner)
  883. ->object($runner->getWarningWriter())->isEqualTo($defaultWarningWriter)
  884. ;
  885. }
  886. public function testSetErrorWriter()
  887. {
  888. $this
  889. ->given($runner = new testedClass(uniqid()))
  890. ->then
  891. ->object($runner->setErrorWriter($errorWriter = new atoum\writers\std\err()))->isIdenticalTo($runner)
  892. ->object($runner->getErrorWriter())->isIdenticalTo($errorWriter)
  893. ->given(
  894. $colorizer = new cli\colorizer('0;31'),
  895. $colorizer->setPattern('/^([^:]+:)/'),
  896. $defaultErrorWriter = new atoum\writers\std\err(),
  897. $defaultErrorWriter
  898. ->addDecorator(new writer\decorators\trim())
  899. ->addDecorator(new writer\decorators\prompt($runner->getLocale()->_('Error: ')))
  900. ->addDecorator(new writer\decorators\eol())
  901. ->addDecorator(new atoum\cli\clear())
  902. ->addDecorator($colorizer)
  903. )
  904. ->then
  905. ->object($runner->setErrorWriter())->isIdenticalTo($runner)
  906. ->object($runner->getErrorWriter())->isEqualTo($defaultErrorWriter)
  907. ;
  908. }
  909. public function testHelp()
  910. {
  911. $this
  912. ->if($argumentsParser = new mock\script\arguments\parser())
  913. ->and($this->calling($argumentsParser)->addHandler = function() {})
  914. ->and($locale = new mock\locale())
  915. ->and($this->calling($locale)->_ = function($string) { return $string; })
  916. ->and($helpWriter = new mock\writers\std\out())
  917. ->and($this->calling($helpWriter)->write = function() {})
  918. ->and($runner = new testedClass($name = uniqid()))
  919. ->and($runner->setArgumentsParser($argumentsParser))
  920. ->and($runner->setLocale($locale))
  921. ->and($runner->setHelpWriter($helpWriter))
  922. ->then
  923. ->object($runner->help())->isIdenticalTo($runner)
  924. ->mock($helpWriter)->call('write')
  925. ->atLeastOnce()
  926. ->withArguments('Usage: ' . $name . ' [path/to/test/file] [options]' . PHP_EOL)->once()
  927. ;
  928. }
  929. public function testRun()
  930. {
  931. $this
  932. ->if($locale = new mock\locale())
  933. ->and($this->calling($locale)->_ = function($string) { return $string; })
  934. ->and($helpWriter = new mock\writers\std\out())
  935. ->and($this->calling($helpWriter)->write = function() {})
  936. ->and($errorWriter = new mock\writers\std\err())
  937. ->and($this->calling($errorWriter)->clear = $errorWriter)
  938. ->and($this->calling($errorWriter)->write = $errorWriter)
  939. ->and($runner = new mock\runner())
  940. ->and($this->calling($runner)->getTestPaths = array())
  941. ->and($this->calling($runner)->getDeclaredTestClasses = array())
  942. ->and($this->calling($runner)->run = function() {})
  943. ->and($script = new testedClass($name = uniqid()))
  944. ->and($script->setLocale($locale))
  945. ->and($script->setHelpWriter($helpWriter))
  946. ->and($script->setErrorWriter($errorWriter))
  947. ->and($script->setRunner($runner))
  948. ->then
  949. ->object($script->run())->isIdenticalTo($script)
  950. ->mock($locale)
  951. ->call('_')
  952. ->withArguments('No test found')->once()
  953. ->mock($errorWriter)->call('write')->withArguments('No test found')->once()
  954. ;
  955. }
  956. public function testAutorun()
  957. {
  958. $this
  959. ->if($script = new testedClass(uniqid()))
  960. ->and($script->setAdapter($adapter = new atoum\test\adapter()))
  961. ->and($adapter->realpath = function($path) { return $path; })
  962. ->when(function() { if (isset($_SERVER['argv']) === true) { unset($_SERVER['argv']); } })
  963. ->then
  964. ->boolean($script->autorun())->isTrue()
  965. ->if($_SERVER['argv'] = array())
  966. ->then
  967. ->boolean($script->autorun())->isTrue()
  968. ->if($_SERVER['argv'][0] = $script->getName())
  969. ->then
  970. ->boolean($script->autorun())->isFalse()
  971. ->if($adapter->realpath = uniqid())
  972. ->then
  973. ->boolean($script->autorun())->isTrue()
  974. ;
  975. }
  976. }