Skip to content

Commit 2df9284

Browse files
committed
fix: assert for failure cases
1 parent fb9b6d9 commit 2df9284

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

src/Utils/Test/tests/Unit/TraceStructureAssertionTraitTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,133 @@ public function test_assert_trace_structure_with_multiple_root_spans(): void
233233
// Assert the trace structure
234234
$this->assertTraceStructure($this->storage, $expectedStructure);
235235
}
236+
237+
/**
238+
* Test that assertTraceStructure fails when there are additional root spans.
239+
*/
240+
public function test_assert_fails_with_additional_root_spans(): void
241+
{
242+
$tracer = $this->tracerProvider->getTracer('test-tracer');
243+
244+
// Create two root spans
245+
$rootSpan1 = $tracer->spanBuilder('root-span-1')->startSpan();
246+
$rootSpan1->end();
247+
248+
$rootSpan2 = $tracer->spanBuilder('root-span-2')->startSpan();
249+
$rootSpan2->end();
250+
251+
// Define expected structure with only one root span
252+
$expectedStructure = [
253+
[
254+
'name' => 'root-span-1',
255+
],
256+
];
257+
258+
// Expect assertion to fail
259+
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
260+
$this->expectExceptionMessage('Expected 1 root spans, but found 2');
261+
262+
$this->assertTraceStructure($this->storage, $expectedStructure);
263+
}
264+
265+
/**
266+
* Test that assertTraceStructure fails when there are additional child spans.
267+
*/
268+
public function test_assert_fails_with_additional_child_spans(): void
269+
{
270+
$tracer = $this->tracerProvider->getTracer('test-tracer');
271+
272+
// Create a root span
273+
$rootSpan = $tracer->spanBuilder('root-span')->startSpan();
274+
$rootScope = $rootSpan->activate();
275+
276+
try {
277+
// Create two child spans
278+
$childSpan1 = $tracer->spanBuilder('child-span-1')->startSpan();
279+
$childSpan1->end();
280+
281+
$childSpan2 = $tracer->spanBuilder('child-span-2')->startSpan();
282+
$childSpan2->end();
283+
} finally {
284+
$rootSpan->end();
285+
$rootScope->detach();
286+
}
287+
288+
// Define expected structure with only one child span
289+
$expectedStructure = [
290+
[
291+
'name' => 'root-span',
292+
'children' => [
293+
[
294+
'name' => 'child-span-1',
295+
],
296+
],
297+
],
298+
];
299+
300+
// Expect assertion to fail
301+
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
302+
// We don't check the exact message as it might vary based on implementation details
303+
304+
$this->assertTraceStructure($this->storage, $expectedStructure);
305+
}
306+
307+
/**
308+
* Test that assertTraceStructure fails in strict mode when there are additional events.
309+
*/
310+
public function test_assert_fails_with_additional_events_in_strict_mode(): void
311+
{
312+
// Create a new test setup to avoid interference from previous tests
313+
$storage = new ArrayObject();
314+
$tracerProvider = new TracerProvider(
315+
new SimpleSpanProcessor(
316+
new InMemoryExporter($storage)
317+
)
318+
);
319+
320+
$tracer = $tracerProvider->getTracer('test-tracer');
321+
322+
// Create a span with multiple events
323+
$span = $tracer->spanBuilder('test-span')->startSpan();
324+
$span->addEvent('event-1');
325+
$span->addEvent('event-2');
326+
$span->end();
327+
328+
// Define expected structure with only one event
329+
$expectedStructure = [
330+
[
331+
'name' => 'test-span',
332+
'events' => [
333+
[
334+
'name' => 'event-1',
335+
],
336+
],
337+
],
338+
];
339+
340+
// Assert passes in non-strict mode
341+
$this->assertTraceStructure($storage, $expectedStructure, false);
342+
343+
// Create a new test setup for the strict mode test
344+
$strictStorage = new ArrayObject();
345+
$strictTracerProvider = new TracerProvider(
346+
new SimpleSpanProcessor(
347+
new InMemoryExporter($strictStorage)
348+
)
349+
);
350+
351+
$strictTracer = $strictTracerProvider->getTracer('test-tracer');
352+
353+
// Create the same span structure again
354+
$strictSpan = $strictTracer->spanBuilder('test-span')->startSpan();
355+
$strictSpan->addEvent('event-1');
356+
$strictSpan->addEvent('event-2');
357+
$strictSpan->end();
358+
359+
// Expect assertion to fail in strict mode
360+
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
361+
// We don't check the exact message as it might vary based on implementation details
362+
363+
$this->assertTraceStructure($strictStorage, $expectedStructure, true);
364+
}
236365
}

0 commit comments

Comments
 (0)