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.

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