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.

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