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.

97 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 testExecuteImplementError()
  55. {
  56. $cli_class = new StdClass();
  57. $cli_controller = CliController::getInstance();
  58. $error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream');
  59. $this->stream = fopen('php://memory', 'rw');
  60. $error_stream_prop->setAccessible(true);
  61. $error_stream_prop->setValue($cli_controller, $this->stream);
  62. $cli_controller->execute($cli_class);
  63. rewind($this->stream);
  64. $this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', stream_get_contents($this->stream));
  65. }
  66. /**
  67. * @runInSeparateProcess
  68. */
  69. public function testExecuteWithRunThrowError()
  70. {
  71. Config::set('PROFILER', false);
  72. $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run'));
  73. $cli_class->expects($this->once())
  74. ->method('run')
  75. ->with()
  76. ->will($this->returnCallback(array($this, 'callbackWithThrow')));
  77. $cli_controller = CliController::getInstance();
  78. $error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream');
  79. $this->stream = fopen('php://memory', 'rw');
  80. $error_stream_prop->setAccessible(true);
  81. $error_stream_prop->setValue($cli_controller, $this->stream);
  82. $cli_controller->execute($cli_class);
  83. rewind($this->stream);
  84. $this->assertContains('Error from callback.', stream_get_contents($this->stream));
  85. }
  86. public function callbackWithThrow()
  87. {
  88. throw new ErrorException('Error from callback.');
  89. }
  90. }