/tests/CLITest.php

https://github.com/GunioRobot/phpca · PHP · 180 lines · 73 code · 24 blank · 83 comment · 0 complexity · e2fee81b83e53dd6e211b8836314e040 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2009 Stefan Priebsch <stefan@priebsch.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Stefan Priebsch nor the names of contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * @package PHPca
  33. * @author Stefan Priebsch <stefan@priebsch.de>
  34. * @copyright Stefan Priebsch <stefan@priebsch.de>. All rights reserved.
  35. * @license BSD License
  36. */
  37. namespace spriebsch\PHPca;
  38. /**
  39. * Tests for the CLI class.
  40. *
  41. * @author Stefan Priebsch <stefan@priebsch.de>
  42. * @copyright Stefan Priebsch <stefan@priebsch.de>. All rights reserved.
  43. */
  44. class CLITest extends \PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * We test CLI through a subclass that prevents exit on errors.
  48. */
  49. protected function setUp()
  50. {
  51. $this->cli = new TestCLISubclass(getcwd());
  52. }
  53. /**
  54. * Runs CLI (through a subclass that prevents exit on errors)
  55. * and captures its output through output buffering.
  56. * Returns a string with CLI's output.
  57. *
  58. * @param array $argv The command line arguments for CLI
  59. * @return string
  60. * @todo use output buffering annotation of PHPUnit?
  61. */
  62. protected function runCLI($argv)
  63. {
  64. ob_start();
  65. $this->cli->run($argv);
  66. $result = ob_get_contents();
  67. ob_end_clean();
  68. return $result;
  69. }
  70. /**
  71. * @covers spriebsch\PHPca\CLI
  72. */
  73. public function testRunShowsErrorOnUnknownOption()
  74. {
  75. $result = $this->runCLI(array('cli.php', '-unknown'));
  76. $this->assertContains('Error: Unknown option', $result);
  77. }
  78. /**
  79. * @covers spriebsch\PHPca\CLI
  80. */
  81. public function testRunShowsNameAndVersion()
  82. {
  83. $result = $this->runCLI(array('cli.php'));
  84. $this->assertContains('PHP Code Analyzer', $result);
  85. $this->assertContains(Application::$version, $result);
  86. }
  87. /**
  88. * @covers spriebsch\PHPca\CLI
  89. */
  90. public function testRunShowsUsageMessageWhenInvokedWithoutArguments()
  91. {
  92. $result = $this->runCLI(array('cli.php'));
  93. $this->assertContains('Usage:', $result);
  94. }
  95. /**
  96. * @covers spriebsch\PHPca\CLI
  97. */
  98. public function testRunShowsErrorMessageWhenInvokedWithoutArguments()
  99. {
  100. $result = $this->runCLI(array('cli.php'));
  101. $this->assertContains('Error: No path to PHP executable', $result);
  102. }
  103. /**
  104. * @covers spriebsch\PHPca\CLI
  105. */
  106. public function testRunShowsUsageMessageWhenInvokedWithShortHelpSwitch()
  107. {
  108. $result = $this->runCLI(array('cli.php', '-h'));
  109. $this->assertContains('Usage:', $result);
  110. }
  111. /**
  112. * @covers spriebsch\PHPca\CLI
  113. */
  114. public function testRunShowsUsageMessageWhenInvokedWithLongHelpSwitch()
  115. {
  116. $result = $this->runCLI(array('cli.php', '--help'));
  117. $this->assertContains('Usage:', $result);
  118. }
  119. /**
  120. * @covers spriebsch\PHPca\CLI
  121. */
  122. public function testRunDisplaysErrorWhenNoFilesToAnalyze()
  123. {
  124. $result = $this->runCLI(array('cli.php', '-p', trim(exec('which php')), __DIR__ . '/_testdata/Application/none'));
  125. $this->assertContains('Error: No PHP files to analyze', $result);
  126. }
  127. /**
  128. * @covers spriebsch\PHPca\CLI
  129. */
  130. public function testRunDisplaysFailMessageWhenAnalyzingFilesWithViolations()
  131. {
  132. $result = $this->runCLI(array('cli.php', '-p', trim(exec('which php')), __DIR__ . '/_testdata/Application/fail'));
  133. $this->assertContains("\nF\n", $result);
  134. $this->assertContains('FAIL', $result);
  135. }
  136. /**
  137. * @covers spriebsch\PHPca\CLI
  138. */
  139. public function testRunDisplayOkMessageWhenAnalyzingFilesWithoutViolations()
  140. {
  141. $result = $this->runCLI(array('cli.php', '-p', trim(exec('which php')), __DIR__ . '/_testdata/Application/pass'));
  142. $this->assertContains("\n.\n", $result);
  143. $this->assertContains('OK', $result);
  144. }
  145. /**
  146. * @covers spriebsch\PHPca\CLI
  147. */
  148. public function testRunDisplayLintErrorWhenAnalyzingFilesWithLintErrors()
  149. {
  150. $result = $this->runCLI(array('cli.php', '-p', trim(exec('which php')), __DIR__ . '/_testdata/lint_fail.php'));
  151. $this->assertContains("\nE\n", $result);
  152. $this->assertContains('Parse error:', $result);
  153. $this->assertContains('FAIL', $result);
  154. }
  155. }
  156. ?>