|
2 | 2 |
|
3 | 3 | use Illuminate\Support\Facades\Context; |
4 | 4 | use Naoray\LaravelGithubMonolog\Tracing\EnvironmentCollector; |
| 5 | +use Naoray\LaravelGithubMonolog\Tracing\GitInfoDetector; |
5 | 6 |
|
6 | 7 | beforeEach(function () { |
7 | | - $this->collector = new EnvironmentCollector; |
| 8 | + GitInfoDetector::resetCache(); |
8 | 9 | }); |
9 | 10 |
|
10 | 11 | afterEach(function () { |
11 | 12 | Context::flush(); |
| 13 | + GitInfoDetector::resetCache(); |
12 | 14 | }); |
13 | 15 |
|
14 | 16 | it('collects environment data', function () { |
15 | | - $this->collector->collect(); |
| 17 | + $collector = new EnvironmentCollector; |
| 18 | + $collector->collect(); |
16 | 19 |
|
17 | 20 | $environment = Context::get('environment'); |
18 | 21 |
|
|
22 | 25 | expect($environment['php_version'])->toBe(PHP_VERSION); |
23 | 26 | expect($environment['php_os'])->toBe(PHP_OS); |
24 | 27 | }); |
| 28 | + |
| 29 | +it('includes git data when available', function () { |
| 30 | + $detector = Mockery::mock(GitInfoDetector::class); |
| 31 | + $detector->shouldReceive('detect')->once()->andReturn([ |
| 32 | + 'git_hash' => 'abc1234', |
| 33 | + 'git_branch' => 'main', |
| 34 | + 'git_tag' => 'v1.0.0', |
| 35 | + 'git_dirty' => false, |
| 36 | + ]); |
| 37 | + |
| 38 | + $collector = new EnvironmentCollector($detector); |
| 39 | + $collector->collect(); |
| 40 | + |
| 41 | + $environment = Context::get('environment'); |
| 42 | + |
| 43 | + expect($environment) |
| 44 | + ->toHaveKey('git_hash', 'abc1234') |
| 45 | + ->toHaveKey('git_branch', 'main') |
| 46 | + ->toHaveKey('git_tag', 'v1.0.0') |
| 47 | + ->toHaveKey('git_dirty', false) |
| 48 | + ->toHaveKey('git_commit', 'abc1234'); |
| 49 | +}); |
| 50 | + |
| 51 | +it('falls back gracefully when git is not available', function () { |
| 52 | + $detector = Mockery::mock(GitInfoDetector::class); |
| 53 | + $detector->shouldReceive('detect')->once()->andReturn([]); |
| 54 | + |
| 55 | + $collector = new EnvironmentCollector($detector); |
| 56 | + $collector->collect(); |
| 57 | + |
| 58 | + $environment = Context::get('environment'); |
| 59 | + |
| 60 | + expect($environment) |
| 61 | + ->toHaveKey('git_commit') |
| 62 | + ->toHaveKey('app_env') |
| 63 | + ->toHaveKey('php_version'); |
| 64 | + |
| 65 | + expect($environment)->not->toHaveKey('git_hash'); |
| 66 | + expect($environment)->not->toHaveKey('git_branch'); |
| 67 | +}); |
| 68 | + |
| 69 | +it('uses config override for git_hash when app.git_commit is set', function () { |
| 70 | + config(['app.git_commit' => 'config-hash-override']); |
| 71 | + |
| 72 | + $detector = Mockery::mock(GitInfoDetector::class); |
| 73 | + $detector->shouldReceive('detect')->once()->andReturn([ |
| 74 | + 'git_hash' => 'auto-detected-hash', |
| 75 | + 'git_branch' => 'main', |
| 76 | + ]); |
| 77 | + |
| 78 | + $collector = new EnvironmentCollector($detector); |
| 79 | + $collector->collect(); |
| 80 | + |
| 81 | + $environment = Context::get('environment'); |
| 82 | + |
| 83 | + expect($environment) |
| 84 | + ->toHaveKey('git_hash', 'config-hash-override') |
| 85 | + ->toHaveKey('git_commit', 'config-hash-override') |
| 86 | + ->toHaveKey('git_branch', 'main'); |
| 87 | +}); |
| 88 | + |
| 89 | +it('skips git detection when git tracing is disabled', function () { |
| 90 | + config(['github-monolog.tracing.git' => false]); |
| 91 | + |
| 92 | + $detector = Mockery::mock(GitInfoDetector::class); |
| 93 | + $detector->shouldNotReceive('detect'); |
| 94 | + |
| 95 | + $collector = new EnvironmentCollector($detector); |
| 96 | + $collector->collect(); |
| 97 | + |
| 98 | + $environment = Context::get('environment'); |
| 99 | + |
| 100 | + expect($environment)->toHaveKey('git_commit'); |
| 101 | + expect($environment)->not->toHaveKey('git_hash'); |
| 102 | + expect($environment)->not->toHaveKey('git_branch'); |
| 103 | + expect($environment)->not->toHaveKey('git_tag'); |
| 104 | + expect($environment)->not->toHaveKey('git_dirty'); |
| 105 | +}); |
| 106 | + |
| 107 | +it('sets git_commit from git_hash when config override is not set', function () { |
| 108 | + config(['app.git_commit' => null]); |
| 109 | + |
| 110 | + $detector = Mockery::mock(GitInfoDetector::class); |
| 111 | + $detector->shouldReceive('detect')->once()->andReturn([ |
| 112 | + 'git_hash' => 'detected-abc', |
| 113 | + ]); |
| 114 | + |
| 115 | + $collector = new EnvironmentCollector($detector); |
| 116 | + $collector->collect(); |
| 117 | + |
| 118 | + $environment = Context::get('environment'); |
| 119 | + |
| 120 | + expect($environment) |
| 121 | + ->toHaveKey('git_hash', 'detected-abc') |
| 122 | + ->toHaveKey('git_commit', 'detected-abc'); |
| 123 | +}); |
| 124 | + |
| 125 | +it('enables git tracing by default', function () { |
| 126 | + $detector = Mockery::mock(GitInfoDetector::class); |
| 127 | + $detector->shouldReceive('detect')->once()->andReturn([ |
| 128 | + 'git_hash' => 'abc1234', |
| 129 | + ]); |
| 130 | + |
| 131 | + $collector = new EnvironmentCollector($detector); |
| 132 | + $collector->collect(); |
| 133 | + |
| 134 | + $environment = Context::get('environment'); |
| 135 | + |
| 136 | + expect($environment)->toHaveKey('git_hash', 'abc1234'); |
| 137 | +}); |
0 commit comments