FeaturesTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\PHPStan;
  12. use RuntimeException;
  13. use Tests\AbstractTestCase;
  14. class FeaturesTest extends AbstractTestCase
  15. {
  16. /**
  17. * @SuppressWarnings(PHPMD.LongVariable)
  18. */
  19. protected $phpStanPreviousDirectory = '.';
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->phpStanPreviousDirectory = getcwd();
  24. chdir(__DIR__.'/../..');
  25. }
  26. protected function tearDown(): void
  27. {
  28. chdir($this->phpStanPreviousDirectory);
  29. parent::tearDown();
  30. }
  31. public function testAnalysesWithoutErrors(): void
  32. {
  33. $this->assertStringContainsString(
  34. '[OK] No errors',
  35. $this->analyze(__DIR__.'/project.neon')
  36. );
  37. }
  38. public function testAnalysesWithAnError(): void
  39. {
  40. $this->assertStringContainsString(
  41. '22 Static call to instance method Carbon\Carbon::foo().',
  42. $this->analyze(__DIR__.'/bad-project.neon')
  43. );
  44. }
  45. private function analyze(string $file): string
  46. {
  47. $output = shell_exec(implode(' ', [
  48. implode(DIRECTORY_SEPARATOR, ['.', 'vendor', 'bin', 'phpstan']),
  49. 'analyse',
  50. '--configuration='.escapeshellarg(realpath($file)),
  51. '--no-progress',
  52. '--no-interaction',
  53. '--level=0',
  54. escapeshellarg(realpath(__DIR__.'/Fixture.php')),
  55. ]));
  56. if (!\is_string($output)) {
  57. throw new RuntimeException('Executing phpstan returned '.var_export($output, true));
  58. }
  59. return $output;
  60. }
  61. }