MercurialProcessorTest.php 2.1 KB

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