HelpTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace lithium\tests\cases\console\command;
  3. use lithium\console\command\Help;
  4. use lithium\console\Request;
  5. class HelpTest extends \lithium\test\Unit {
  6. public $request;
  7. public $classes = array();
  8. protected $_backup = array();
  9. public function setUp() {
  10. $this->classes = array('response' => 'lithium\tests\mocks\console\MockResponse');
  11. $this->_backup['cwd'] = getcwd();
  12. $this->_backup['_SERVER'] = $_SERVER;
  13. $_SERVER['argv'] = array();
  14. $this->request = new Request(array('input' => fopen('php://temp', 'w+')));
  15. $this->request->params = array('library' => 'build_test');
  16. }
  17. public function tearDown() {
  18. $_SERVER = $this->_backup['_SERVER'];
  19. chdir($this->_backup['cwd']);
  20. }
  21. public function testRun() {
  22. $command = new Help(array('request' => $this->request, 'classes' => $this->classes));
  23. $this->assertTrue($command->run());
  24. $expected = "COMMANDS via lithium\n";
  25. $expected = preg_quote($expected);
  26. $result = $command->response->output;
  27. $this->assertPattern("/{$expected}/", $result);
  28. $expected = preg_quote($expected);
  29. $result = $command->response->output;
  30. $pattern = "/\s+test\s+Runs a given set of tests and outputs the results\./ms";
  31. $this->assertPattern($pattern, $result);
  32. }
  33. public function testRunWithName() {
  34. $command = new Help(array(
  35. 'request' => $this->request, 'classes' => $this->classes
  36. ));
  37. $result = $command->run('Test');
  38. $this->assertTrue($result);
  39. $result = $command->run('test');
  40. $this->assertTrue($result);
  41. $expected = 'li3 test [--filters=<string>]';
  42. $expected .= ' [--format=<string>] [--verbose] [--plain] [<path>]';
  43. $expected = preg_quote($expected);
  44. $result = $command->response->output;
  45. $this->assertPattern("/{$expected}/", $result);
  46. $expected = "OPTIONS\n <path>\n";
  47. $expected = preg_quote($expected);
  48. $result = $command->response->output;
  49. $this->assertPattern("/{$expected}/", $result);
  50. $expected = "DESCRIPTION\n";
  51. $expected = preg_quote($expected);
  52. $result = $command->response->output;
  53. $this->assertPattern("/{$expected}/", $result);
  54. $expected = "Command `TestWithDashes` not found";
  55. $expected = preg_quote($expected);
  56. $result = $command->run('test-with-dashes');
  57. $this->assertFalse($result);
  58. $result = $command->response->error;
  59. $this->assertPattern("/{$expected}/", $result);
  60. $expected = "Command `TestWithUnderscores` not found";
  61. $expected = preg_quote($expected);
  62. $result = $command->run('test_with_underscores');
  63. $this->assertFalse($result);
  64. $result = $command->response->error;
  65. $this->assertPattern("/{$expected}/", $result);
  66. }
  67. /**
  68. * Tests that class and method help includes detailed descriptions as well as summary text.
  69. */
  70. public function testDocsIncludeDescription() {
  71. $command = new Help(array('request' => $this->request, 'classes' => $this->classes));
  72. $this->assertNull($command->api('lithium.core.Libraries'));
  73. $this->assertPattern('/Auto-loading classes/', $command->response->output);
  74. $command = new Help(array('request' => $this->request, 'classes' => $this->classes));
  75. $this->assertNull($command->api('lithium.core.Libraries::add'));
  76. $this->assertPattern('/Adding libraries/', $command->response->output);
  77. }
  78. public function testApiClass() {
  79. $command = new Help(array('request' => $this->request, 'classes' => $this->classes));
  80. $result = $command->api('lithium.util.Inflector');
  81. $this->assertNull($result);
  82. $expected = "Utility for modifying format of words";
  83. $expected = preg_quote($expected);
  84. $result = $command->response->output;
  85. $this->assertPattern("/{$expected}/", $result);
  86. }
  87. public function testApiMethod() {
  88. $command = new Help(array('request' => $this->request, 'classes' => $this->classes));
  89. $result = $command->api('lithium.util.Inflector', 'method');
  90. $this->assertNull($result);
  91. $expected = "rules";
  92. $expected = preg_quote($expected);
  93. $result = $command->response->output;
  94. $this->assertPattern("/{$expected}/", $result);
  95. }
  96. public function testApiMethodWithName() {
  97. $command = new Help(array(
  98. 'request' => $this->request, 'classes' => $this->classes
  99. ));
  100. $result = $command->api('lithium.util.Inflector', 'method', 'rules');
  101. $this->assertNull($result);
  102. $expected = "rules";
  103. $expected = preg_quote($expected);
  104. $result = $command->response->output;
  105. $this->assertPattern("/{$expected}/", $result);
  106. }
  107. public function testApiProperty() {
  108. $command = new Help(array(
  109. 'request' => $this->request, 'classes' => $this->classes
  110. ));
  111. $result = $command->api('lithium.net.Message', 'property');
  112. $this->assertNull($result);
  113. $expected = " --host=<string>\n The hostname for this endpoint.";
  114. $expected = preg_quote($expected);
  115. $result = $command->response->output;
  116. $this->assertPattern("/{$expected}/", $result);
  117. }
  118. public function testApiPropertyWithName() {
  119. $command = new Help(array(
  120. 'request' => $this->request, 'classes' => $this->classes
  121. ));
  122. $result = $command->api('lithium.net.Message', 'property');
  123. $this->assertNull($result);
  124. $expected = " --host=<string>\n The hostname for this endpoint.";
  125. $expected = preg_quote($expected);
  126. $result = $command->response->output;
  127. $this->assertPattern("/{$expected}/", $result);
  128. }
  129. public function testApiProperties() {
  130. $help = new Help(array(
  131. 'request' => $this->request, 'classes' => $this->classes
  132. ));
  133. $expected = null;
  134. $result = $help->api('lithium.tests.mocks.console.command.MockCommandHelp', 'property');
  135. $this->assertEqual($expected, $result);
  136. $expected = "\-\-long=<string>.*\-\-blong.*\-s";
  137. $result = $help->response->output;
  138. $this->assertPattern("/{$expected}/s", $result);
  139. }
  140. }
  141. ?>