-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorServiceProviderTest.php
More file actions
223 lines (181 loc) · 7.83 KB
/
MonitorServiceProviderTest.php
File metadata and controls
223 lines (181 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Kirschbaum\Monitor\LogTimer;
use Kirschbaum\Monitor\Monitor;
use Kirschbaum\Monitor\MonitorServiceProvider;
use Kirschbaum\Monitor\Trace;
use Tests\TestCase;
class MonitorServiceProviderTest extends TestCase
{
public function test_registers_services_as_singletons()
{
// Test that services are registered
expect($this->app->bound(Monitor::class))->toBeTrue()
->and($this->app->bound(Trace::class))->toBeTrue()
->and($this->app->bound(LogTimer::class))->toBeTrue();
// Test singleton behavior - same instance returned
$monitor1 = $this->app->make(Monitor::class);
$monitor2 = $this->app->make(Monitor::class);
expect($monitor1)->toBe($monitor2);
$trace1 = $this->app->make(Trace::class);
$trace2 = $this->app->make(Trace::class);
expect($trace1)->toBe($trace2);
$timer1 = $this->app->make(LogTimer::class);
$timer2 = $this->app->make(LogTimer::class);
expect($timer1)->toBe($timer2);
}
public function test_services_return_correct_types()
{
expect($this->app->make(Monitor::class))->toBeInstanceOf(Monitor::class)
->and($this->app->make(Trace::class))->toBeInstanceOf(Trace::class)
->and($this->app->make(LogTimer::class))->toBeInstanceOf(LogTimer::class);
}
public function test_publishes_config_files()
{
// Test that publishing is registered
$provider = new MonitorServiceProvider($this->app);
$provider->boot();
$publishGroups = $provider::$publishGroups ?? [];
// Laravel 11 changed how this works, so let's test the files exist
$monitorConfigPath = __DIR__.'/../../config/monitor.php';
$loggingConfigPath = __DIR__.'/../../config/logging-monitor.php';
expect(file_exists($monitorConfigPath))->toBeTrue('monitor.php config file should exist')
->and(file_exists($loggingConfigPath))->toBeTrue('logging-monitor.php config file should exist');
}
public function test_console_auto_trace_logic_with_started_trace()
{
// Create a trace that has already started
$trace = new Trace;
$trace->start();
$originalId = $trace->id();
// Bind to container
$this->app->instance(Trace::class, $trace);
// Create a custom service provider to test console logic
$provider = new class($this->app) extends MonitorServiceProvider
{
public function test_boot_console_logic(): void
{
// Simulate the console condition manually (console=true, testing=false, already started)
if (true && ! false && ! app(Trace::class)->hasStarted()) {
app(Trace::class)->start();
}
}
};
$provider->test_boot_console_logic();
// Trace should remain unchanged (already started)
expect($trace->hasStarted())->toBeTrue()
->and($trace->id())->toBe($originalId);
}
public function test_console_auto_trace_logic_with_unstarted_trace()
{
// Create a fresh trace that hasn't started
$trace = new Trace;
expect($trace->hasNotStarted())->toBeTrue();
// Bind to container
$this->app->instance(Trace::class, $trace);
// Create a custom service provider to test console logic
$provider = new class($this->app) extends MonitorServiceProvider
{
public function test_boot_console_logic(): void
{
// Simulate the console condition manually (console=true, testing=false, not started)
if (true && ! false && ! app(Trace::class)->hasStarted()) {
app(Trace::class)->start();
}
}
};
$provider->test_boot_console_logic();
// Trace should now be started
expect($trace->hasStarted())->toBeTrue();
}
public function test_console_auto_trace_respects_testing_environment()
{
// Create a fresh trace
$trace = new Trace;
expect($trace->hasNotStarted())->toBeTrue();
$this->app->instance(Trace::class, $trace);
// Create a custom service provider to test testing environment logic
$provider = new class($this->app) extends MonitorServiceProvider
{
public function test_boot_console_logic(): void
{
// Simulate console=true, testing=true condition
if (true && ! true && ! app(Trace::class)->hasStarted()) {
app(Trace::class)->start();
}
}
};
$provider->test_boot_console_logic();
// Trace should NOT be started in testing environment
expect($trace->hasNotStarted())->toBeTrue();
}
public function test_console_auto_trace_respects_web_environment()
{
// Create a fresh trace
$trace = new Trace;
expect($trace->hasNotStarted())->toBeTrue();
$this->app->instance(Trace::class, $trace);
// Create a custom service provider to test web environment logic
$provider = new class($this->app) extends MonitorServiceProvider
{
public function test_boot_console_logic(): void
{
// Simulate console=false (web environment)
if (false && ! false && ! app(Trace::class)->hasStarted()) {
app(Trace::class)->start();
}
}
};
$provider->test_boot_console_logic();
// Trace should NOT be started in web environment
expect($trace->hasNotStarted())->toBeTrue();
}
public function test_app_binding_works_through_helper()
{
// Test that app() helper works with our registered services
expect(app(Monitor::class))->toBeInstanceOf(Monitor::class)
->and(app(Trace::class))->toBeInstanceOf(Trace::class)
->and(app(LogTimer::class))->toBeInstanceOf(LogTimer::class);
// Test singleton behavior through app() helper
expect(app(Monitor::class))->toBe(app(Monitor::class))
->and(app(Trace::class))->toBe(app(Trace::class))
->and(app(LogTimer::class))->toBe(app(LogTimer::class));
}
public function test_boot_method_integration()
{
// Test that boot() doesn't throw errors and performs expected operations
$provider = new MonitorServiceProvider($this->app);
// Test that boot can be called without throwing any exception
$exception = null;
try {
$provider->boot();
} catch (\Exception $e) {
$exception = $e;
}
expect($exception)->toBeNull();
}
public function test_service_provider_properties()
{
$provider = new MonitorServiceProvider($this->app);
// Test that service provider is properly configured
expect($provider)->toBeInstanceOf(MonitorServiceProvider::class)
->and(get_class($provider))->toBe(MonitorServiceProvider::class);
}
public function test_register_is_complete()
{
// Test the registration process itself by checking that register() method works
$provider = new MonitorServiceProvider($this->app);
// The services should already be bound by the test setup, so let's test that they are singletons
$monitor1 = $this->app->make(Monitor::class);
$monitor2 = $this->app->make(Monitor::class);
$trace1 = $this->app->make(Trace::class);
$trace2 = $this->app->make(Trace::class);
$timer1 = $this->app->make(LogTimer::class);
$timer2 = $this->app->make(LogTimer::class);
// Verify they are the same instances (singleton behavior)
expect($monitor1)->toBe($monitor2)
->and($trace1)->toBe($trace2)
->and($timer1)->toBe($timer2);
}
}