PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/units/classes/scripts/builder.php

http://github.com/mageekguy/atoum
PHP | 664 lines | 633 code | 31 blank | 0 comment | 1 complexity | 0aa48bc04ba91ae9d885f0de204498a2 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\scripts;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\mock,
  6. mageekguy\atoum\scripts,
  7. mageekguy\atoum\scripts\builder\vcs,
  8. mageekguy\atoum\scripts\builder as testedClass
  9. ;
  10. require_once __DIR__ . '/../../runner.php';
  11. class builder extends atoum\test
  12. {
  13. public function beforeTestMethod($testMethod)
  14. {
  15. if (extension_loaded('svn') === false)
  16. {
  17. define('SVN_REVISION_HEAD', -1);
  18. define('PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS', 1);
  19. define('SVN_AUTH_PARAM_DEFAULT_USERNAME', 2);
  20. define('SVN_AUTH_PARAM_DEFAULT_PASSWORD', 3);
  21. }
  22. }
  23. public function testClass()
  24. {
  25. $this->testedClass->extends('mageekguy\atoum\script\configurable');
  26. }
  27. public function testClassConstants()
  28. {
  29. $this
  30. ->string(scripts\builder::defaultUnitTestRunnerScript)->isEqualTo('scripts/runner.php')
  31. ->string(scripts\builder::defaultPharGeneratorScript)->isEqualTo('scripts/phar/generator.php')
  32. ;
  33. }
  34. public function test__construct()
  35. {
  36. $this
  37. ->if($builder = new testedClass($name = uniqid()))
  38. ->then
  39. ->string($builder->getName())->isEqualTo($name)
  40. ->object($builder->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  41. ->object($builder->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  42. ->object($builder->getArgumentsParser())->isInstanceOf('mageekguy\atoum\script\arguments\parser')
  43. ->object($builder->getOutputWriter())->isInstanceOf('mageekguy\atoum\writers\std\out')
  44. ->object($builder->getErrorWriter())->isInstanceOf('mageekguy\atoum\writers\std\err')
  45. ->array($builder->getRunnerConfigurationFiles())->isEmpty()
  46. ->variable($builder->getVersion())->isNull()
  47. ->variable($builder->getWorkingDirectory())->isNull()
  48. ->variable($builder->getDestinationDirectory())->isNull()
  49. ->variable($builder->getErrorsDirectory())->isNull()
  50. ->variable($builder->getScoreDirectory())->isNull()
  51. ->variable($builder->getRevisionFile())->isNull()
  52. ->string($builder->getUnitTestRunnerScript())->isEqualTo(scripts\builder::defaultUnitTestRunnerScript)
  53. ->string($builder->getPharGeneratorScript())->isEqualTo(scripts\builder::defaultPharGeneratorScript)
  54. ->variable($builder->getReportTitle())->isNull()
  55. ->object($builder->getVcs())->isInstanceOf('mageekguy\atoum\scripts\builder\vcs\svn')
  56. ->variable($builder->getTaggerEngine())->isNull()
  57. ->if($builder = new testedClass($name = uniqid(), $adapter = new atoum\adapter()))
  58. ->then
  59. ->string($builder->getName())->isEqualTo($name)
  60. ->object($builder->getAdapter())->isIdenticalTo($adapter)
  61. ->object($builder->getLocale())->isInstanceOf('mageekguy\atoum\locale')
  62. ->object($builder->getArgumentsParser())->isInstanceOf('mageekguy\atoum\script\arguments\parser')
  63. ->object($builder->getOutputWriter())->isInstanceOf('mageekguy\atoum\writers\std\out')
  64. ->object($builder->getErrorWriter())->isInstanceOf('mageekguy\atoum\writers\std\err')
  65. ->array($builder->getRunnerConfigurationFiles())->isEmpty()
  66. ->variable($builder->getVersion())->isNull()
  67. ->variable($builder->getWorkingDirectory())->isNull()
  68. ->variable($builder->getDestinationDirectory())->isNull()
  69. ->variable($builder->getErrorsDirectory())->isNull()
  70. ->variable($builder->getScoreDirectory())->isNull()
  71. ->variable($builder->getRevisionFile())->isNull()
  72. ->string($builder->getUnitTestRunnerScript())->isEqualTo(scripts\builder::defaultUnitTestRunnerScript)
  73. ->string($builder->getPharGeneratorScript())->isEqualTo(scripts\builder::defaultPharGeneratorScript)
  74. ->variable($builder->getReportTitle())->isNull()
  75. ->object($builder->getVcs())->isInstanceOf('mageekguy\atoum\scripts\builder\vcs\svn')
  76. ->variable($builder->getTaggerEngine())->isNull()
  77. ;
  78. }
  79. public function testSetPhp()
  80. {
  81. $this
  82. ->if($builder = new testedClass(uniqid()))
  83. ->then
  84. ->object($builder->setPhp($php = new atoum\php()))->isIdenticalTo($builder)
  85. ->object($builder->getPhp())->isIdenticalTo($php)
  86. ->object($builder->setPhp())->isIdenticalTo($builder)
  87. ->object($builder->getPhp())
  88. ->isEqualTo(new atoum\php())
  89. ->isNotIdenticalTo($php)
  90. ;
  91. }
  92. public function testSetVersion()
  93. {
  94. $this
  95. ->if($builder = new testedClass(uniqid()))
  96. ->then
  97. ->object($builder->setVersion($tag = uniqid()))->isIdenticalTo($builder)
  98. ->string($builder->getVersion())->isIdenticalTo($tag)
  99. ->object($builder->setVersion($tag = rand(1, PHP_INT_MAX)))->isIdenticalTo($builder)
  100. ->string($builder->getVersion())->isIdenticalTo((string) $tag)
  101. ;
  102. }
  103. public function testGetPhpPath()
  104. {
  105. $this
  106. ->if($builder = new testedClass(uniqid()))
  107. ->then
  108. ->string($builder->getPhpPath())->isEqualTo($builder->getPhp()->getBinaryPath())
  109. ;
  110. }
  111. public function testSetPhpPath()
  112. {
  113. $this
  114. ->if($builder = new testedClass(uniqid()))
  115. ->then
  116. ->object($builder->setPhpPath($phpPath = uniqid()))->isIdenticalTo($builder)
  117. ->string($builder->getPhpPath())->isIdenticalTo($phpPath)
  118. ;
  119. }
  120. public function testSetReportTitle()
  121. {
  122. $this
  123. ->if($builder = new testedClass(uniqid()))
  124. ->then
  125. ->object($builder->setReportTitle($reportTitle = uniqid()))->isIdenticalTo($builder)
  126. ->string($builder->getReportTitle())->isEqualTo($reportTitle)
  127. ->object($builder->setReportTitle($reportTitle = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isIdenticalTo($builder)
  128. ->string($builder->getReportTitle())->isEqualTo((string) $reportTitle)
  129. ;
  130. }
  131. public function testSetVcs()
  132. {
  133. $this
  134. ->if($builder = new testedClass(uniqid()))
  135. ->and->mockGenerator->shunt('__construct')
  136. ->then
  137. ->object($builder->setVcs($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs()))->isIdenticalTo($builder)
  138. ->object($builder->getVcs())->isIdenticalTo($vcs)
  139. ;
  140. }
  141. public function testSetTaggerEngine()
  142. {
  143. $this
  144. ->if($builder = new testedClass(uniqid()))
  145. ->then
  146. ->object($builder->setTaggerEngine($taggerEngine = new atoum\scripts\tagger\engine()))->isIdenticalTo($builder)
  147. ->object($builder->getTaggerEngine())->isIdenticalTo($taggerEngine)
  148. ;
  149. }
  150. public function testSetUnitTestRunnerScript()
  151. {
  152. $this
  153. ->if($builder = new testedClass(uniqid()))
  154. ->then
  155. ->object($builder->setUnitTestRunnerScript($php = uniqid()))->isIdenticalTo($builder)
  156. ->string($builder->getUnitTestRunnerScript())->isIdenticalTo($php)
  157. ->object($builder->setUnitTestRunnerScript($php = rand(1, PHP_INT_MAX)))->isIdenticalTo($builder)
  158. ->string($builder->getUnitTestRunnerScript())->isIdenticalTo((string) $php)
  159. ;
  160. }
  161. public function testSetPharGeneratorScript()
  162. {
  163. $this
  164. ->if($builder = new testedClass(uniqid()))
  165. ->then
  166. ->object($builder->setPharGeneratorScript($php = uniqid()))->isIdenticalTo($builder)
  167. ->string($builder->getPharGeneratorScript())->isIdenticalTo($php)
  168. ->object($builder->setPharGeneratorScript($php = rand(1, PHP_INT_MAX)))->isIdenticalTo($builder)
  169. ->string($builder->getPharGeneratorScript())->isIdenticalTo((string) $php)
  170. ;
  171. }
  172. public function testSetScoreDirectory()
  173. {
  174. $this
  175. ->if($builder = new testedClass(uniqid()))
  176. ->then
  177. ->object($builder->setScoreDirectory($scoreDirectory = uniqid()))->isIdenticalTo($builder)
  178. ->string($builder->getScoreDirectory())->isEqualTo($scoreDirectory)
  179. ->object($builder->setScoreDirectory($directory = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isIdenticalTo($builder)
  180. ->string($builder->getScoreDirectory())->isEqualTo($directory)
  181. ->object($builder->setScoreDirectory(($directory = uniqid()) . DIRECTORY_SEPARATOR))->isIdenticalTo($builder)
  182. ->string($builder->getScoreDirectory())->isEqualTo($directory)
  183. ;
  184. }
  185. public function testSetErrorsDirectory()
  186. {
  187. $this
  188. ->if($builder = new testedClass(uniqid()))
  189. ->then
  190. ->object($builder->setErrorsDirectory($errorsDirectory = uniqid()))->isIdenticalTo($builder)
  191. ->string($builder->getErrorsDirectory())->isEqualTo($errorsDirectory)
  192. ->object($builder->setErrorsDirectory($directory = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isIdenticalTo($builder)
  193. ->string($builder->getErrorsDirectory())->isEqualTo($directory)
  194. ->object($builder->setErrorsDirectory(($directory = uniqid()) . DIRECTORY_SEPARATOR))->isIdenticalTo($builder)
  195. ->string($builder->getErrorsDirectory())->isEqualTo($directory)
  196. ;
  197. }
  198. public function testSetDestinationDirectory()
  199. {
  200. $this
  201. ->if($builder = new testedClass(uniqid()))
  202. ->then
  203. ->object($builder->setDestinationDirectory($directory = uniqid()))->isIdenticalTo($builder)
  204. ->string($builder->getDestinationDirectory())->isEqualTo($directory)
  205. ->object($builder->setDestinationDirectory($directory = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isIdenticalTo($builder)
  206. ->string($builder->getDestinationDirectory())->isEqualTo($directory)
  207. ->object($builder->setDestinationDirectory(($directory = uniqid()) . DIRECTORY_SEPARATOR))->isIdenticalTo($builder)
  208. ->string($builder->getDestinationDirectory())->isEqualTo($directory)
  209. ;
  210. }
  211. public function testSetWorkingDirectory()
  212. {
  213. $this
  214. ->if($builder = new testedClass(uniqid()))
  215. ->then
  216. ->object($builder->setWorkingDirectory($directory = uniqid()))->isIdenticalTo($builder)
  217. ->string($builder->getWorkingDirectory())->isEqualTo($directory)
  218. ->object($builder->setWorkingDirectory($directory = rand(- PHP_INT_MAX, PHP_INT_MAX)))->isIdenticalTo($builder)
  219. ->string($builder->getWorkingDirectory())->isEqualTo((string) $directory)
  220. ->object($builder->setWorkingDirectory(($directory = uniqid()) . DIRECTORY_SEPARATOR))->isIdenticalTo($builder)
  221. ->string($builder->getWorkingDirectory())->isEqualTo($directory)
  222. ;
  223. }
  224. public function testSetRevisionFile()
  225. {
  226. $this
  227. ->if($builder = new testedClass(uniqid()))
  228. ->then
  229. ->object($builder->setRevisionFile($file = uniqid()))->isIdenticalTo($builder)
  230. ->string($builder->getRevisionFile())->isEqualTo($file)
  231. ;
  232. }
  233. public function testAddRunnerConfigurationFile()
  234. {
  235. $this
  236. ->if($builder = new testedClass(uniqid()))
  237. ->then
  238. ->object($builder->addRunnerConfigurationFile($file = uniqid()))->isIdenticalTo($builder)
  239. ->array($builder->getRunnerConfigurationFiles())->isEqualTo(array($file))
  240. ;
  241. }
  242. public function testSetRunFile()
  243. {
  244. $this
  245. ->if($builder = new testedClass(uniqid()))
  246. ->then
  247. ->object($builder->setRunFile($runFile = uniqid()))->isIdenticalTo($builder)
  248. ->string($builder->getRunFile())->isEqualTo($runFile)
  249. ;
  250. }
  251. public function testDisableUnitTestChecking()
  252. {
  253. $this
  254. ->if($builder = new testedClass(uniqid()))
  255. ->then
  256. ->boolean($builder->unitTestCheckingIsEnabled())->isTrue()
  257. ->object($builder->disableUnitTestChecking())->isIdenticalTo($builder)
  258. ->boolean($builder->unitTestCheckingIsEnabled())->isFalse()
  259. ;
  260. }
  261. public function testEnableUnitTestChecking()
  262. {
  263. $this
  264. ->if($builder = new testedClass(uniqid()))
  265. ->and($builder->disableUnitTestChecking())
  266. ->then
  267. ->boolean($builder->unitTestCheckingIsEnabled())->isFalse()
  268. ->object($builder->enableUnitTestChecking())->isIdenticalTo($builder)
  269. ->boolean($builder->unitTestCheckingIsEnabled())->isTrue()
  270. ;
  271. }
  272. public function testCheckUnitTests()
  273. {
  274. $this
  275. ->if($builder = new \mock\mageekguy\atoum\scripts\builder(uniqid(), $adapter = new atoum\test\adapter()))
  276. ->and($builder->disableUnitTestChecking())
  277. ->then
  278. ->boolean($builder->unitTestCheckingIsEnabled())->isFalse()
  279. ->boolean($builder->checkUnitTests())->isTrue()
  280. ->if($builder->enableUnitTestChecking())
  281. ->then
  282. ->exception(function() use ($builder) {
  283. $builder->checkUnitTests();
  284. }
  285. )
  286. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  287. ->hasMessage('Unable to check unit tests, working directory is undefined')
  288. ->if->mockGenerator->shunt('__construct')
  289. ->and($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs())
  290. ->and($this->calling($vcs)->exportRepository = function() {})
  291. ->and($builder->setVcs($vcs))
  292. ->and($php = new \mock\mageekguy\atoum\php())
  293. ->and($this->calling($php)->run = $php)
  294. ->and($builder->setPhp($php))
  295. ->and($builder->setWorkingDirectory($workingDirectory = uniqid()))
  296. ->and($builder->setUnitTestRunnerScript($unitTestRunnerScript = uniqid()))
  297. ->and($builder->setReportTitle($reportTitle = uniqid()))
  298. ->and($builder->addRunnerConfigurationFile($runnerConfigurationFile = uniqid()))
  299. ->and($score = new \mock\mageekguy\atoum\score())
  300. ->and($this->calling($score)->getFailNumber = 0)
  301. ->and($this->calling($score)->getExceptionNumber = 0)
  302. ->and($this->calling($score)->getErrorNumber = 0)
  303. ->and($adapter->sys_get_temp_dir = $tempDirectory = uniqid())
  304. ->and($adapter->tempnam = $scoreFile = uniqid())
  305. ->and($adapter->file_get_contents = $scoreFileContents = uniqid())
  306. ->and($adapter->unserialize = $score)
  307. ->and($adapter->unlink = true)
  308. ->and($this->calling($builder)->writeErrorInErrorsDirectory = function() {})
  309. ->then
  310. ->boolean($builder->checkUnitTests())->isTrue()
  311. ->mock($vcs)
  312. ->call('setWorkingDirectory')->withArguments($workingDirectory)->once()
  313. ->call('exportRepository')->once()
  314. ->adapter($adapter)
  315. ->call('sys_get_temp_dir')->once()
  316. ->call('tempnam')->withArguments($tempDirectory, '')->once()
  317. ->call('file_get_contents')->withArguments($scoreFile)->once()
  318. ->call('unserialize')->withArguments($scoreFileContents)->once()
  319. ->call('unlink')->withArguments($scoreFile)->once()
  320. ->mock($score)
  321. ->call('getFailNumber')->once()
  322. ->call('getExceptionNumber')->once()
  323. ->call('getErrorNumber')->once()
  324. ->mock($php)
  325. ->call('reset')
  326. ->before($this->mock($php)->call('run'))
  327. ->once()
  328. ->call('addOption')->withArguments('-f', $workingDirectory . \DIRECTORY_SEPARATOR . $unitTestRunnerScript)
  329. ->before($this->mock($php)->call('run'))
  330. ->once()
  331. ->call('addArgument')->withArguments('-ncc')
  332. ->before($this->mock($php)->call('run'))
  333. ->once()
  334. ->call('addArgument')->withArguments('-d', $workingDirectory . \DIRECTORY_SEPARATOR . 'tests' . \DIRECTORY_SEPARATOR . 'units' . \DIRECTORY_SEPARATOR . 'classes')
  335. ->before($this->mock($php)->call('run'))
  336. ->once()
  337. ->call('addArgument')->withArguments('-p', $php->getBinaryPath())
  338. ->before($this->mock($php)->call('run'))
  339. ->once()
  340. ->call('addArgument')->withArguments('-sf', $scoreFile)
  341. ->before($this->mock($php)->call('run'))
  342. ->once()
  343. ->call('addArgument')->withArguments('-c', $runnerConfigurationFile)
  344. ->before($this->mock($php)->call('run'))
  345. ->once()
  346. ->if($this->calling($php)->getExitCode = 127)
  347. ->then
  348. ->boolean($builder->checkUnitTests())->isFalse()
  349. ->mock($builder)
  350. ->call('writeErrorInErrorsDirectory')->withArguments('Unable to find \'' . $php->getBinaryPath() . '\' or it is not executable')->once()
  351. ->if($this->calling($php)->getExitCode = $exitCode = rand(1, 125))
  352. ->and($this->calling($php)->getStdErr = $stdErr = uniqid())
  353. ->then
  354. ->boolean($builder->checkUnitTests())->isFalse()
  355. ->mock($builder)
  356. ->call('writeErrorInErrorsDirectory')->withArguments($php . ' failed with exit code \'' . $exitCode . '\': ' . $stdErr)->once()
  357. ->if($this->calling($php)->getExitCode = $exitCode = rand(128, PHP_INT_MAX))
  358. ->then
  359. ->boolean($builder->checkUnitTests())->isFalse()
  360. ->mock($builder)
  361. ->call('writeErrorInErrorsDirectory')->withArguments($php . ' failed with exit code \'' . $exitCode . '\': ' . $stdErr)->once()
  362. ->if($this->calling($php)->getExitCode = 0)
  363. ->and($this->calling($php)->getStdErr = '')
  364. ->then
  365. ->boolean($builder->checkUnitTests())->isTrue()
  366. ->if($this->calling($php)->getStdErr = $stdErrContents = uniqid())
  367. ->then
  368. ->boolean($builder->checkUnitTests())->isFalse()
  369. ->mock($builder)
  370. ->call('writeErrorInErrorsDirectory')->withArguments($stdErrContents)->once()
  371. ->if($this->calling($php)->getStdErr = '')
  372. ->and($adapter->file_get_contents = false)
  373. ->and($this->resetMock($builder))
  374. ->then
  375. ->boolean($builder->checkUnitTests())->isFalse()
  376. ->mock($builder)
  377. ->call('writeErrorInErrorsDirectory')->withArguments('Unable to read score from file \'' . $scoreFile . '\'')->once()
  378. ->if($adapter->file_get_contents = $scoreFileContents)
  379. ->and($adapter->unserialize = false)
  380. ->then
  381. ->boolean($builder->checkUnitTests())->isFalse()
  382. ->mock($builder)
  383. ->call('writeErrorInErrorsDirectory')->withArguments('Unable to unserialize score from file \'' . $scoreFile . '\'')->once()
  384. ->if($adapter->unserialize = uniqid())
  385. ->then
  386. ->boolean($builder->checkUnitTests())->isFalse()
  387. ->mock($builder)
  388. ->call('writeErrorInErrorsDirectory')->withArguments('Contents of file \'' . $scoreFile . '\' is not a score')->once()
  389. ->if($adapter->unserialize = $score)
  390. ->and($adapter->unlink = false)
  391. ->then
  392. ->exception(function() use ($builder) {
  393. $builder->checkUnitTests();
  394. }
  395. )
  396. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  397. ->hasMessage('Unable to delete score file \'' . $scoreFile . '\'')
  398. ->if($adapter->unlink = true)
  399. ->and($this->calling($score)->getFailNumber = rand(1, PHP_INT_MAX))
  400. ->then
  401. ->boolean($builder->checkUnitTests())->isFalse()
  402. ->if($this->calling($score)->getFailNumber = 0)
  403. ->and($this->calling($score)->getExceptionNumber = rand(1, PHP_INT_MAX))
  404. ->then
  405. ->boolean($builder->checkUnitTests())->isFalse()
  406. ->if($this->calling($score)->getExceptionNumber = 0)
  407. ->and($this->calling($score)->getErrorNumber = rand(1, PHP_INT_MAX))
  408. ->then
  409. ->boolean($builder->checkUnitTests())->isFalse()
  410. ;
  411. }
  412. public function testDisablePharCreation()
  413. {
  414. $this
  415. ->if($builder = new testedClass(uniqid()))
  416. ->then
  417. ->boolean($builder->pharCreationIsEnabled())->isTrue()
  418. ->object($builder->disablePharCreation())->isIdenticalTo($builder)
  419. ->boolean($builder->pharCreationIsEnabled())->isFalse()
  420. ;
  421. }
  422. public function testEnablePharCreation()
  423. {
  424. $this
  425. ->if($builder = new testedClass(uniqid()))
  426. ->and($builder->disablePharCreation())
  427. ->then
  428. ->boolean($builder->pharCreationIsEnabled())->isFalse()
  429. ->object($builder->enablePharCreation())->isIdenticalTo($builder)
  430. ->boolean($builder->pharCreationIsEnabled())->isTrue()
  431. ;
  432. }
  433. public function testCreatePhar()
  434. {
  435. $this
  436. ->if($builder = new \mock\mageekguy\atoum\scripts\builder(uniqid(), $adapter = new atoum\test\adapter()))
  437. ->and($builder->setTaggerEngine($taggerEngine = new \mock\mageekguy\atoum\scripts\tagger\engine()))
  438. ->and($this->calling($taggerEngine)->tagVersion = function() {})
  439. ->and($builder->disablePharCreation())
  440. ->then
  441. ->boolean($builder->createPhar())->isTrue()
  442. ->if($builder->enablePharCreation())
  443. ->and->mockGenerator->shunt('__construct')
  444. ->and($builder->setVcs($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs()))
  445. ->and($this->calling($vcs)->getNextRevisions = array())
  446. ->and($this->calling($vcs)->exportRepository = function() {})
  447. ->then
  448. ->exception(function() use ($builder) {
  449. $builder->createPhar();
  450. }
  451. )
  452. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  453. ->hasMessage('Unable to create phar, destination directory is undefined')
  454. ->if($builder->setDestinationDirectory($destinationDirectory = uniqid()))
  455. ->then
  456. ->exception(function() use ($builder) {
  457. $builder->createPhar();
  458. }
  459. )
  460. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  461. ->hasMessage('Unable to create phar, working directory is undefined')
  462. ->if($builder->setWorkingDirectory($workingDirectory = uniqid()))
  463. ->and($builder->setPhp($php = new \mock\mageekguy\atoum\php()))
  464. ->and($this->calling($php)->run = $php)
  465. ->and($builder->setPharGeneratorScript($pharGeneratorScript = uniqid()))
  466. ->and($this->calling($builder)->writeErrorInErrorsDirectory = function() {})
  467. ->and($adapter->file_get_contents = false)
  468. ->then
  469. ->boolean($builder->createPhar())->isTrue()
  470. ->if($this->calling($vcs)->getNextRevisions = function() use (& $revision) { static $i = 0; return ++$i > 1 ? array() : array($revision = rand(1, PHP_INT_MAX)); })
  471. ->and($builder->disableUnitTestChecking())
  472. ->and($this->calling($php)->getExitCode = rand(1, PHP_INT_MAX))
  473. ->and($this->calling($php)->getStderr = $stderr = uniqid())
  474. ->then
  475. ->boolean($builder->createPhar())->isFalse()
  476. ->mock($builder)
  477. ->call('writeErrorInErrorsDirectory')->withArguments('Unable to run ' . $php . ': ' . $stderr)->once()
  478. ->mock($vcs)
  479. ->call('setRevision')->withArguments($revision)->once()
  480. ->call('setWorkingDirectory')->withArguments($workingDirectory)->once()
  481. ->call('exportRepository')->once()
  482. ->if($this->calling($php)->getExitCode = 0)
  483. ->and($adapter->date = $date = uniqid())
  484. ->and($this->calling($vcs)->getNextRevisions = function() use (& $revision) { static $i = 0; return ++$i > 1 ? array() : array($revision = rand(1, PHP_INT_MAX)); })
  485. ->and($this->resetMock($vcs))
  486. ->then
  487. ->boolean($builder->createPhar())->isTrue()
  488. ->mock($taggerEngine)
  489. ->call('setVersion')
  490. ->withArguments('nightly-' . $revision . '-' . $date)
  491. ->once()
  492. ->call('tagVersion')->atLeastOnce()
  493. ->mock($vcs)
  494. ->call('setRevision')->withArguments($revision)->once()
  495. ->call('setWorkingDirectory')->withArguments($workingDirectory)->once()
  496. ->call('exportRepository')->once()
  497. ->if($this->resetMock($vcs))
  498. ->and($this->resetMock($taggerEngine))
  499. ->and($this->calling($vcs)->getNextRevisions = function() use (& $revision) { static $i = 0; return ++$i > 1 ? array() : array($revision = rand(1, PHP_INT_MAX)); })
  500. ->then
  501. ->boolean($builder->createPhar($tag = uniqid()))->isTrue()
  502. ->mock($taggerEngine)
  503. ->call('setVersion')->withArguments($tag)->once()
  504. ->call('tagVersion')->once()
  505. ->mock($vcs)
  506. ->call('setRevision')->withArguments($revision)->once()
  507. ->call('setWorkingDirectory')->withArguments($workingDirectory)->once()
  508. ->call('exportRepository')->once()
  509. ->if($builder->setRevisionFile($revisionFile = uniqid()))
  510. ->and($adapter->file_get_contents = false)
  511. ->and($adapter->file_put_contents = function() {})
  512. ->and($this->resetMock($vcs))
  513. ->and($this->calling($vcs)->getNextRevisions = function() use (& $revision) { static $i = 0; return ++$i > 1 ? array() : array($revision = rand(1, PHP_INT_MAX)); })
  514. ->then
  515. ->boolean($builder->createPhar())->isTrue()
  516. ->adapter($adapter)
  517. ->call('file_get_contents')->withArguments($revisionFile)->once()
  518. ->call('file_put_contents')->withArguments($revisionFile, $revision, \LOCK_EX)->once()
  519. ->mock($vcs)
  520. ->call('setRevision')->withArguments($revision)->once()
  521. ->call('setWorkingDirectory')->withArguments($workingDirectory)->once()
  522. ->call('exportRepository')->once()
  523. ->if($this->resetMock($vcs))
  524. ->and($this->calling($vcs)->getNextRevisions = function() use (& $revision) { static $i = 0; return ++$i > 1 ? array() : array($revision = rand(1, PHP_INT_MAX)); })
  525. ->and($adapter->file_put_contents = false)
  526. ->then
  527. ->exception(function() use ($builder) {
  528. $builder->createPhar();
  529. }
  530. )
  531. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  532. ->hasMessage('Unable to save last revision in file \'' . $revisionFile . '\'')
  533. ->if($this->resetMock($vcs))
  534. ->and($this->calling($vcs)->getNextRevisions[1] = array(1, 2, 3))
  535. ->and($this->calling($vcs)->getNextRevisions[2] = array(2, 3))
  536. ->and($this->calling($vcs)->getNextRevisions[3] = array(3))
  537. ->and($this->calling($vcs)->getNextRevisions[4] = array())
  538. ->and($adapter->file_put_contents = function() {})
  539. ->and($adapter->resetCalls())
  540. ->then
  541. ->boolean($builder->createPhar())->isTrue()
  542. ->adapter($adapter)
  543. ->call('file_get_contents')->withArguments($revisionFile)->once()
  544. ->call('file_put_contents')->withArguments($revisionFile, 3, \LOCK_EX)->once()
  545. ->mock($vcs)
  546. ->call('setRevision')->withArguments(1)->once()
  547. ->call('setRevision')->withArguments(2)->once()
  548. ->call('setRevision')->withArguments(3)->once()
  549. ->call('setWorkingDirectory')->withArguments($workingDirectory)->atLeastOnce()
  550. ->call('exportRepository')->atLeastOnce()
  551. ->if($this->resetMock($vcs))
  552. ->and($this->calling($vcs)->getNextRevisions[1] = array(4))
  553. ->and($this->calling($vcs)->getNextRevisions[2] = array())
  554. ->and($adapter->file_get_contents = 1)
  555. ->and($adapter->resetCalls())
  556. ->then
  557. ->boolean($builder->createPhar())->isTrue()
  558. ->adapter($adapter)
  559. ->call('file_get_contents')->withArguments($revisionFile)->once()
  560. ->call('file_put_contents')->withArguments($revisionFile, 4, \LOCK_EX)->once()
  561. ->mock($vcs)
  562. ->call('setRevision')->withArguments(4)->once()
  563. ->call('setWorkingDirectory')->withArguments($workingDirectory)->once()
  564. ->call('exportRepository')->once()
  565. ;
  566. }
  567. public function testRun()
  568. {
  569. $this
  570. ->if($adapter = new atoum\test\adapter())
  571. ->and($adapter->file_get_contents = false)
  572. ->and($adapter->fopen = $runFileResource = uniqid())
  573. ->and($adapter->flock = true)
  574. ->and($adapter->getmypid = $pid = uniqid())
  575. ->and($adapter->fwrite = function() {})
  576. ->and($adapter->fclose = function() {})
  577. ->and($adapter->unlink = function() {})
  578. ->and($builder = new \mock\mageekguy\atoum\scripts\builder(uniqid(), $adapter))
  579. ->and($builder->setRunFile($runFile = uniqid()))
  580. ->and($this->calling($builder)->createPhar = function() {})
  581. ->then
  582. ->object($builder->run())->isIdenticalTo($builder)
  583. ->mock($builder)->call('createPhar')->once()
  584. ->adapter($adapter)
  585. ->call('file_get_contents')->withArguments($runFile)->once()
  586. ->call('fopen')->withArguments($runFile, 'w+')->once()
  587. ->call('flock')->withArguments($runFileResource, \LOCK_EX | \LOCK_NB)->once()
  588. ->call('fwrite')->withArguments($runFileResource, $pid)->once()
  589. ->call('fclose')->withArguments($runFileResource)->once()
  590. ->call('unlink')->withArguments($runFile)->once()
  591. ;
  592. }
  593. public function testWriteInErrorDirectory()
  594. {
  595. $this
  596. ->if($adapter = new atoum\test\adapter())
  597. ->and($adapter->file_put_contents = function() {})
  598. ->and($builder = new testedClass(uniqid(), $adapter))
  599. ->then
  600. ->variable($builder->getErrorsDirectory())->isNull()
  601. ->object($builder->writeErrorInErrorsDirectory(uniqid()))->isIdenticalTo($builder)
  602. ->adapter($adapter)->call('file_put_contents')->never()
  603. ->if($builder->setErrorsDirectory($errorDirectory = uniqid()))
  604. ->then
  605. ->string($builder->getErrorsDirectory())->isEqualTo($errorDirectory)
  606. ->exception(function() use ($builder) {
  607. $builder->writeErrorInErrorsDirectory(uniqid());
  608. }
  609. )
  610. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  611. ->hasMessage('Revision is undefined')
  612. ->adapter($adapter)->call('file_put_contents')->never()
  613. ->if->mockGenerator->shunt('__construct')
  614. ->and($builder->setVcs($vcs = new \mock\mageekguy\atoum\scripts\builder\vcs()))
  615. ->and($vcs->setRevision($revision = rand(1, PHP_INT_MAX)))
  616. ->then
  617. ->string($builder->getErrorsDirectory())->isEqualTo($errorDirectory)
  618. ->object($builder->writeErrorInErrorsDirectory($message = uniqid()))->isIdenticalTo($builder)
  619. ->adapter($adapter)->call('file_put_contents')->withArguments($errorDirectory . \DIRECTORY_SEPARATOR . $revision, $message, \LOCK_EX | \FILE_APPEND)->once()
  620. ->if($adapter->resetCalls())
  621. ->and($adapter->file_put_contents = false)
  622. ->then
  623. ->string($builder->getErrorsDirectory())->isEqualTo($errorDirectory)
  624. ->exception(function() use ($builder, & $message) {
  625. $builder->writeErrorInErrorsDirectory($message = uniqid());
  626. }
  627. )
  628. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  629. ->hasMessage('Unable to save error in file \'' . $errorDirectory . \DIRECTORY_SEPARATOR . $revision . '\'')
  630. ->adapter($adapter)->call('file_put_contents')->withArguments($errorDirectory . \DIRECTORY_SEPARATOR . $revision, $message, \LOCK_EX | \FILE_APPEND)->once()
  631. ;
  632. }
  633. }