PageRenderTime 22ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/php.php

https://github.com/agallou/atoum
PHP | 245 lines | 232 code | 13 blank | 0 comment | 5 complexity | 3f9d521382416060058265e041404ff0 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\php as testedClass
  6. ;
  7. require_once __DIR__ . '/../runner.php';
  8. class php extends atoum\test
  9. {
  10. public function test__construct()
  11. {
  12. $this
  13. ->if($php = new testedClass())
  14. ->then
  15. ->string($php->getBinaryPath())->isNotEmpty()
  16. ->object($php->getAdapter())->isEqualTo(new atoum\adapter())
  17. ->string($php->getStdout())->isEmpty()
  18. ->string($php->getStderr())->isEmpty()
  19. ->array($php->getOptions())->isEmpty()
  20. ->array($php->getArguments())->isEmpty()
  21. ->if($php = new testedClass($phpPath = uniqid(), $adapter = new atoum\adapter()))
  22. ->then
  23. ->string($php->getBinaryPath())->isEqualTo($phpPath)
  24. ->object($php->getAdapter())->isIdenticalTo($adapter)
  25. ->string($php->getStdout())->isEmpty()
  26. ->string($php->getStderr())->isEmpty()
  27. ->array($php->getOptions())->isEmpty()
  28. ->array($php->getArguments())->isEmpty()
  29. ;
  30. }
  31. public function test__toString()
  32. {
  33. $this
  34. ->if($php = new testedClass())
  35. ->then
  36. ->castToString($php)->isEqualTo(escapeshellcmd($php->getBinaryPath()))
  37. ->if($php->addOption($option1 = uniqid()))
  38. ->then
  39. ->castToString($php)->isEqualTo(escapeshellcmd($php->getBinaryPath() . ' ' . $option1))
  40. ->if($php->addOption($option2 = uniqid(), $option2Value = uniqid() . ' ' . uniqid()))
  41. ->then
  42. ->castToString($php)->isEqualTo(escapeshellcmd($php->getBinaryPath() . ' ' . $option1 . ' ' . $option2 . ' ' . $option2Value))
  43. ->if($php->addArgument($argument1 = uniqid()))
  44. ->then
  45. ->castToString($php)->isEqualTo(escapeshellcmd($php->getBinaryPath() . ' ' . $option1 . ' ' . $option2 . ' ' . $option2Value . ' -- ' . $argument1))
  46. ->if($php->addArgument($argument2 = uniqid(), $argument2Value = uniqid()))
  47. ->then
  48. ->castToString($php)->isEqualTo(escapeshellcmd($php->getBinaryPath() . ' ' . $option1 . ' ' . $option2 . ' ' . $option2Value . ' -- ' . $argument1 . ' ' . $argument2 . ' ' . escapeshellarg($argument2Value)))
  49. ;
  50. }
  51. public function testSetAdapter()
  52. {
  53. $this
  54. ->given($php = new testedClass())
  55. ->then
  56. ->object($php->setAdapter($adapter = new atoum\adapter()))->isIdenticalTo($php)
  57. ->object($php->getAdapter())->isIdenticalTo($adapter)
  58. ->object($php->setAdapter())->isIdenticalTo($php)
  59. ->object($php->getAdapter())
  60. ->isNotIdenticalTo($adapter)
  61. ->isEqualTo(new atoum\adapter())
  62. ;
  63. }
  64. public function testReset()
  65. {
  66. $this
  67. ->if($php = new testedClass())
  68. ->then
  69. ->object($php->reset())->isIdenticalTo($php)
  70. ->string($php->getBinaryPath())->isNotEmpty()
  71. ->array($php->getOptions())->isEmpty()
  72. ->array($php->getArguments())->isEmpty()
  73. ->if($php->setBinaryPath($binaryPath = uniqid()))
  74. ->and($php->addOption(uniqid()))
  75. ->and($php->addArgument(uniqid()))
  76. ->then
  77. ->object($php->reset())->isIdenticalTo($php)
  78. ->string($php->getBinaryPath())->isEqualTo($binaryPath)
  79. ->array($php->getOptions())->isEmpty()
  80. ->array($php->getArguments())->isEmpty()
  81. ;
  82. }
  83. public function testSetBinaryPath()
  84. {
  85. $this
  86. ->given($php = new atoum\php(null, $adapter = new atoum\test\adapter()))
  87. ->if($adapter->defined = function($constant) { return ($constant == 'PHP_BINARY'); })
  88. ->and($adapter->constant = function($constant) use (& $phpBinary) { return ($constant != 'PHP_BINARY' ? null : $phpBinary = uniqid()); })
  89. ->then
  90. ->object($php->setBinaryPath())->isIdenticalTo($php)
  91. ->string($php->getBinaryPath())->isEqualTo($phpBinary)
  92. ->if($adapter->defined = false)
  93. ->and($adapter->constant = null)
  94. ->and($adapter->getenv = function($variable) use (& $pearBinaryPath) { return ($variable != 'PHP_PEAR_PHP_BIN' ? false : $pearBinaryPath = uniqid()); })
  95. ->then
  96. ->object($php->setBinaryPath())->isIdenticalTo($php)
  97. ->string($php->getBinaryPath())->isEqualTo($pearBinaryPath)
  98. ->if($adapter->getenv = function($variable) use (& $phpBinPath) {
  99. switch ($variable)
  100. {
  101. case 'PHPBIN':
  102. return ($phpBinPath = uniqid());
  103. default:
  104. return false;
  105. }
  106. }
  107. )
  108. ->then
  109. ->object($php->setBinaryPath())->isIdenticalTo($php)
  110. ->string($php->getBinaryPath())->isEqualTo($phpBinPath)
  111. ->if($adapter->constant = function($constant) use (& $phpBinDir) { return ($constant != 'PHP_BINDIR' ? null : $phpBinDir = uniqid()); })
  112. ->and($adapter->getenv = false)
  113. ->then
  114. ->object($php->setBinaryPath())->isIdenticalTo($php)
  115. ->string($php->getBinaryPath())->isEqualTo($phpBinDir . '/php')
  116. ->object($php->setBinaryPath($phpPath = uniqid()))->isIdenticalTo($php)
  117. ->string($php->getBinaryPath())->isEqualTo($phpPath)
  118. ;
  119. }
  120. public function testAddOption()
  121. {
  122. $this
  123. ->if($php = new testedClass())
  124. ->then
  125. ->object($php->addOption($optionName = uniqid()))->isIdenticalTo($php)
  126. ->array($php->getOptions())->isEqualTo(array($optionName => null))
  127. ->object($php->addOption($optionName))->isIdenticalTo($php)
  128. ->array($php->getOptions())->isEqualTo(array($optionName => null))
  129. ->object($php->addOption($otherOptionName = uniqid()))->isIdenticalTo($php)
  130. ->array($php->getOptions())->isEqualTo(array($optionName => null, $otherOptionName => null))
  131. ->object($php->addOption($anotherOptionName = uniqid(), $optionValue = uniqid()))->isIdenticalTo($php)
  132. ->array($php->getOptions())->isEqualTo(array($optionName => null, $otherOptionName => null, $anotherOptionName => $optionValue))
  133. ->object($php->addOption($anotherOptionName, $anotherOptionValue = uniqid()))->isIdenticalTo($php)
  134. ->array($php->getOptions())->isEqualTo(array($optionName => null, $otherOptionName => null, $anotherOptionName => $anotherOptionValue))
  135. ->object($php->addOption($emptyOption = uniqid(), ''))->isIdenticalTo($php)
  136. ->array($php->getOptions())->isEqualTo(array($optionName => null, $otherOptionName => null, $anotherOptionName => $anotherOptionValue, $emptyOption => null))
  137. ;
  138. }
  139. public function testAddArgument()
  140. {
  141. $this
  142. ->if($php = new testedClass())
  143. ->then
  144. ->object($php->addArgument($argument1 = uniqid()))->isIdenticalTo($php)
  145. ->array($php->getArguments())->isEqualTo(array(array($argument1 => null)))
  146. ->object($php->addArgument($argument1))->isIdenticalTo($php)
  147. ->array($php->getArguments())->isEqualTo(array(array($argument1 => null), array($argument1 => null)))
  148. ->object($php->addArgument($argument2 = uniqid()))->isIdenticalTo($php)
  149. ->array($php->getArguments())->isEqualTo(array(array($argument1 => null), array($argument1 => null), array($argument2 => null)))
  150. ->object($php->addArgument($emptyArgument = uniqid(), ''))->isIdenticalTo($php)
  151. ->array($php->getArguments())->isEqualTo(array(array($argument1 => null), array($argument1 => null), array($argument2 => null), array($emptyArgument => null)))
  152. ;
  153. }
  154. public function testRun()
  155. {
  156. $this
  157. ->if($php = new testedClass($phpPath = uniqid(), $adapter = new atoum\test\adapter()))
  158. ->and($adapter->proc_open = false)
  159. ->then
  160. ->exception(function() use ($php, & $code) { $php->run($code = uniqid()); })
  161. ->isInstanceOf('mageekguy\atoum\cli\command\exception')
  162. ->hasMessage('Unable to run \'' . $php . '\'')
  163. ->adapter($adapter)
  164. ->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()
  165. ->if($php = new testedClass($phpPath = uniqid(), $adapter))
  166. ->and($code = uniqid())
  167. ->and($adapter->proc_open = function($command, $descriptors, & $streams) use (& $phpResource, & $stdin, & $stdout, & $stderr) { $streams = array($stdin = uniqid(), $stdout = uniqid(), $stderr = uniqid); return ($phpResource = uniqid()); })
  168. ->and($adapter->fwrite = strlen($code))
  169. ->and($adapter->fclose = null)
  170. ->and($adapter->stream_set_blocking = null)
  171. ->then
  172. ->object($php->run($code))->isIdenticalTo($php)
  173. ->adapter($adapter)
  174. ->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()
  175. ->call('fwrite')->withArguments($stdin, $code, strlen($code))->once()
  176. ->call('fclose')->withArguments($stdin)->once()
  177. ->call('stream_set_blocking')->withArguments($stdout)->once()
  178. ->call('stream_set_blocking')->withArguments($stderr)->once()
  179. ->if($php = new testedClass($phpPath = uniqid(), $adapter))
  180. ->and($adapter->resetCalls())
  181. ->and($adapter->fwrite[1] = 4)
  182. ->and($adapter->fwrite[2] = strlen($code) - 4)
  183. ->then
  184. ->object($php->run($code))->isIdenticalTo($php)
  185. ->adapter($adapter)
  186. ->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()
  187. ->call('fwrite')->withArguments($stdin, $code, strlen($code))->once()
  188. ->call('fwrite')->withArguments($stdin, substr($code, 4), strlen($code) - 4)->once()
  189. ->call('fclose')->withArguments($stdin)->once()
  190. ->call('stream_set_blocking')->withArguments($stdout)->once()
  191. ->call('stream_set_blocking')->withArguments($stderr)->once()
  192. ->if($php = new testedClass($phpPath = uniqid(), $adapter))
  193. ->and($php->addOption('firstOption'))
  194. ->then
  195. ->object($php->run($code))->isIdenticalTo($php)
  196. ->adapter($adapter)
  197. ->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()
  198. ->if($php = new testedClass($phpPath = uniqid(), $adapter))
  199. ->and($php->addOption('firstOption'))
  200. ->and($php->addOption('secondOption', 'secondOptionValue'))
  201. ->then
  202. ->object($php->run($code))->isIdenticalTo($php)
  203. ->adapter($adapter)
  204. ->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()
  205. ->if($php = new testedClass($phpPath = uniqid(), $adapter))
  206. ->and($php->addArgument($argument1 = uniqid()))
  207. ->and($php->addArgument($argument2 = uniqid()))
  208. ->then
  209. ->object($php->run($code))->isIdenticalTo($php)
  210. ->adapter($adapter)
  211. ->call('proc_open')->withArguments((string) $php, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), array())->once()
  212. ;
  213. }
  214. public function testGetExitCode()
  215. {
  216. $this
  217. ->if($php = new testedClass(null, $adapter = new atoum\test\adapter()))
  218. ->then
  219. ->variable($php->getExitCode())->isNull()
  220. ->if($adapter->proc_open = function($command, $descriptors, & $streams) use (& $phpResource, & $stdin, & $stdout, & $stderr) { $streams = array($stdin = uniqid(), $stdout = uniqid(), $stderr = uniqid); return ($phpResource = uniqid()); })
  221. ->and($adapter->fclose = null)
  222. ->and($adapter->stream_set_blocking = null)
  223. ->and($adapter->stream_get_contents = null)
  224. ->and($adapter->proc_close = null)
  225. ->and($adapter->proc_get_status[1] = array('running' => true))
  226. ->and($adapter->proc_get_status[2] = array('running' => false, 'exitcode' => $exitCode = rand(0, PHP_INT_MAX)))
  227. ->and($php->run())
  228. ->then
  229. ->variable($php->getExitCode())->isEqualTo($exitCode)
  230. ;
  231. }
  232. }