MercurialProcessorTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Processor;
  11. use Monolog\Test\TestCase;
  12. class MercurialProcessorTest extends TestCase
  13. {
  14. private string $oldCwd;
  15. private string $testDir;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->oldCwd = getcwd();
  20. $this->testDir = sys_get_temp_dir().'/monolog-processor-mercurial-test';
  21. mkdir($this->testDir, recursive: true);
  22. chdir($this->testDir);
  23. }
  24. protected function tearDown(): void
  25. {
  26. parent::tearDown();
  27. chdir($this->oldCwd);
  28. if (!file_exists($this->testDir)) {
  29. return;
  30. }
  31. $items = new \RecursiveIteratorIterator(
  32. new \RecursiveDirectoryIterator($this->testDir, \RecursiveDirectoryIterator::SKIP_DOTS),
  33. \RecursiveIteratorIterator::CHILD_FIRST
  34. );
  35. foreach ($items as $item) {
  36. $item->isDir() ? rmdir((string) $item) : unlink((string) $item);
  37. }
  38. rmdir($this->testDir);
  39. }
  40. /**
  41. * @covers Monolog\Processor\MercurialProcessor::__invoke
  42. */
  43. public function testProcessor()
  44. {
  45. if (\defined('PHP_WINDOWS_VERSION_BUILD')) {
  46. exec("where hg 2>NUL", $output, $result);
  47. } else {
  48. exec("which hg 2>/dev/null >/dev/null", $output, $result);
  49. }
  50. if ($result != 0) {
  51. $this->markTestSkipped('hg is missing');
  52. return;
  53. }
  54. exec('hg init');
  55. exec('hg branch default');
  56. touch('test.txt');
  57. exec('hg add test.txt');
  58. exec('hg commit -u foo -m "initial commit"');
  59. $processor = new MercurialProcessor();
  60. $record = $processor($this->getRecord());
  61. $this->assertArrayHasKey('hg', $record->extra);
  62. $this->assertSame('default', $record->extra['hg']['branch']);
  63. $this->assertSame('0', $record->extra['hg']['revision']);
  64. }
  65. }