GithubActionReporterTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\CI;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\CI\GithubActionReporter;
  13. use Symfony\Component\Console\Output\BufferedOutput;
  14. class GithubActionReporterTest extends TestCase
  15. {
  16. public function testIsGithubActionEnvironment()
  17. {
  18. $prev = getenv('GITHUB_ACTIONS');
  19. putenv('GITHUB_ACTIONS');
  20. try {
  21. self::assertFalse(GithubActionReporter::isGithubActionEnvironment());
  22. putenv('GITHUB_ACTIONS=1');
  23. self::assertTrue(GithubActionReporter::isGithubActionEnvironment());
  24. } finally {
  25. putenv('GITHUB_ACTIONS'.($prev ? "=$prev" : ''));
  26. }
  27. }
  28. /**
  29. * @dataProvider annotationsFormatProvider
  30. */
  31. public function testAnnotationsFormat(string $type, string $message, string $file = null, int $line = null, int $col = null, string $expected)
  32. {
  33. $reporter = new GithubActionReporter($buffer = new BufferedOutput());
  34. $reporter->{$type}($message, $file, $line, $col);
  35. self::assertSame($expected.\PHP_EOL, $buffer->fetch());
  36. }
  37. public static function annotationsFormatProvider(): iterable
  38. {
  39. yield 'warning' => ['warning', 'A warning', null, null, null, '::warning::A warning'];
  40. yield 'error' => ['error', 'An error', null, null, null, '::error::An error'];
  41. yield 'debug' => ['debug', 'A debug log', null, null, null, '::debug::A debug log'];
  42. yield 'with message to escape' => [
  43. 'debug',
  44. "There are 100% chances\nfor this to be escaped properly\rRight?",
  45. null,
  46. null,
  47. null,
  48. '::debug::There are 100%25 chances%0Afor this to be escaped properly%0DRight?',
  49. ];
  50. yield 'with meta' => [
  51. 'warning',
  52. 'A warning',
  53. 'foo/bar.php',
  54. 2,
  55. 4,
  56. '::warning file=foo/bar.php,line=2,col=4::A warning',
  57. ];
  58. yield 'with file property to escape' => [
  59. 'warning',
  60. 'A warning',
  61. 'foo,bar:baz%quz.php',
  62. 2,
  63. 4,
  64. '::warning file=foo%2Cbar%3Abaz%25quz.php,line=2,col=4::A warning',
  65. ];
  66. yield 'without file ignores col & line' => ['warning', 'A warning', null, 2, 4, '::warning::A warning'];
  67. }
  68. }