You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage Tests app
  7. * @since 10.07.12
  8. *
  9. */
  10. require_once __DIR__ . '/../../util/profiler/Profiler.php';
  11. require_once __DIR__ . '/../../exception/GeneralException.php';
  12. require_once __DIR__ . '/../../exception/ErrorHTTPException.php';
  13. require_once __DIR__ . '/../../exception/Error404Exception.php';
  14. require_once __DIR__ . '/../../exception/ErrorHandler.php';
  15. require_once __DIR__ . '/../../app/CliController.php';
  16. require_once __DIR__ . '/../../Registry.php';
  17. require_once __DIR__ . '/../../Config.php';
  18. require_once __DIR__ . '/../../app/iCli.php';
  19. /**
  20. * @desc CliController tests
  21. * @author Aleksandr Demidov
  22. */
  23. class CliControllerTest extends PHPUnit_Framework_TestCase
  24. {
  25. protected $stream;
  26. public function testGetInstance()
  27. {
  28. $instance = CliController::getInstance();
  29. $this->assertInstanceOf('CliController', $instance);
  30. }
  31. public function testExecute()
  32. {
  33. Config::set('PROFILER', false);
  34. $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
  35. $cli_class->expects($this->once())
  36. ->method('run')
  37. ->with();
  38. CliController::getInstance()->execute($cli_class);
  39. }
  40. public function testExecuteWithProfiler()
  41. {
  42. ob_start();
  43. Config::set('PROFILER', true);
  44. $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
  45. $cli_class->expects($this->once())
  46. ->method('run')
  47. ->with();
  48. CliController::getInstance()->execute($cli_class);
  49. $output = ob_get_clean();
  50. $this->assertContains('Elapsed time:', $output);
  51. }
  52. /**
  53. * @runInSeparateProcess
  54. */
  55. public function testExecuteImplementErrorToFile()
  56. {
  57. Config::set('ErrorStream', __DIR__ . '/temp.txt');
  58. touch(Config::get('ErrorStream'));
  59. $cli_class = new StdClass();
  60. CliController::getInstance()->execute($cli_class);
  61. $this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', file_get_contents(Config::get('ErrorStream')));
  62. unlink(Config::get('ErrorStream'));
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testExecuteImplementErrorToConsole()
  68. {
  69. Config::set('ErrorStream', 'php://output');
  70. $cli_class = new StdClass();
  71. $this->expectOutputRegex('/.*Runner "' . get_class($cli_class) . '" need implement of "iCli" interface\..*/');
  72. CliController::getInstance()->execute($cli_class);
  73. }
  74. /**
  75. * @runInSeparateProcess
  76. */
  77. public function testExecuteWithRunThrowError()
  78. {
  79. Config::set('ErrorStream', 'php://output');
  80. Config::set('PROFILER', false);
  81. $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
  82. $cli_class->expects($this->once())
  83. ->method('run')
  84. ->with()
  85. ->will($this->returnCallback(array($this, 'callbackWithThrow')));
  86. $this->expectOutputRegex('/.*Error from callback\..*/');
  87. CliController::getInstance()->execute($cli_class);
  88. }
  89. public function callbackWithThrow()
  90. {
  91. throw new ErrorException('Error from callback.');
  92. }
  93. }