|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\TestUtils\Tests\Unit; |
| 6 | + |
| 7 | +use ArrayObject; |
| 8 | +use OpenTelemetry\API\Trace\SpanKind; |
| 9 | +use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; |
| 10 | +use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; |
| 11 | +use OpenTelemetry\SDK\Trace\TracerProvider; |
| 12 | +use OpenTelemetry\TestUtils\Fluent\TraceAssertionFailedException; |
| 13 | +use OpenTelemetry\TestUtils\TraceStructureAssertionTrait; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests for the TraceAssertionFailedException class. |
| 18 | + */ |
| 19 | +class TraceAssertionFailedExceptionTest extends TestCase |
| 20 | +{ |
| 21 | + use TraceStructureAssertionTrait; |
| 22 | + |
| 23 | + private ArrayObject $storage; |
| 24 | + private TracerProvider $tracerProvider; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + // Create a storage for the exported spans |
| 29 | + $this->storage = new ArrayObject(); |
| 30 | + |
| 31 | + // Create a TracerProvider with an InMemoryExporter |
| 32 | + $this->tracerProvider = new TracerProvider( |
| 33 | + new SimpleSpanProcessor( |
| 34 | + new InMemoryExporter($this->storage) |
| 35 | + ) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test that the TraceAssertionFailedException provides a visual diff when an assertion fails. |
| 41 | + */ |
| 42 | + public function test_trace_assertion_failed_exception_provides_visual_diff(): void |
| 43 | + { |
| 44 | + $tracer = $this->tracerProvider->getTracer('test-tracer'); |
| 45 | + |
| 46 | + // Create a root span |
| 47 | + $rootSpan = $tracer->spanBuilder('root-span') |
| 48 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 49 | + ->startSpan(); |
| 50 | + |
| 51 | + // Activate the root span |
| 52 | + $rootScope = $rootSpan->activate(); |
| 53 | + |
| 54 | + try { |
| 55 | + // Create a child span |
| 56 | + $childSpan = $tracer->spanBuilder('child-span') |
| 57 | + ->setSpanKind(SpanKind::KIND_INTERNAL) |
| 58 | + ->startSpan(); |
| 59 | + |
| 60 | + $childSpan->setAttribute('attribute.one', 'value1'); |
| 61 | + $childSpan->setAttribute('attribute.two', 42); |
| 62 | + |
| 63 | + $childSpan->end(); |
| 64 | + } finally { |
| 65 | + // End the root span |
| 66 | + $rootSpan->end(); |
| 67 | + |
| 68 | + // Detach the root scope |
| 69 | + $rootScope->detach(); |
| 70 | + } |
| 71 | + |
| 72 | + // Intentionally create an assertion that will fail |
| 73 | + try { |
| 74 | + $this->assertTrace($this->storage) |
| 75 | + ->hasRootSpan('root-span') |
| 76 | + ->withKind(SpanKind::KIND_SERVER) |
| 77 | + ->hasChild('child-span') |
| 78 | + ->withKind(SpanKind::KIND_INTERNAL) |
| 79 | + // This attribute doesn't exist, so it will fail |
| 80 | + ->withAttribute('attribute.three', 'value3') |
| 81 | + ->end() |
| 82 | + ->end(); |
| 83 | + |
| 84 | + // If we get here, the test failed |
| 85 | + $this->fail('Expected TraceAssertionFailedException was not thrown'); |
| 86 | + } catch (TraceAssertionFailedException $e) { |
| 87 | + // Verify that the exception message contains the expected and actual structures |
| 88 | + $message = $e->getMessage(); |
| 89 | + |
| 90 | + // Check that the message contains the expected structure |
| 91 | + $this->assertStringContainsString('Expected Trace Structure:', $message); |
| 92 | + $this->assertStringContainsString('Attribute "attribute.three"', $message); |
| 93 | + |
| 94 | + // Check that the message contains the actual structure |
| 95 | + $this->assertStringContainsString('Actual Trace Structure:', $message); |
| 96 | + $this->assertStringContainsString('Missing Attribute: "attribute.three"', $message); |
| 97 | + |
| 98 | + // Verify that the exception contains the expected and actual structures |
| 99 | + $this->assertNotEmpty($e->getExpectedStructure()); |
| 100 | + $this->assertNotEmpty($e->getActualStructure()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test that the TraceAssertionFailedException provides a visual diff when a child span is missing. |
| 106 | + */ |
| 107 | + public function test_trace_assertion_failed_exception_provides_visual_diff_for_missing_child(): void |
| 108 | + { |
| 109 | + $tracer = $this->tracerProvider->getTracer('test-tracer'); |
| 110 | + |
| 111 | + // Create a root span |
| 112 | + $rootSpan = $tracer->spanBuilder('root-span') |
| 113 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 114 | + ->startSpan(); |
| 115 | + |
| 116 | + $rootSpan->end(); |
| 117 | + |
| 118 | + // Intentionally create an assertion that will fail due to a missing child span |
| 119 | + try { |
| 120 | + $this->assertTrace($this->storage) |
| 121 | + ->hasRootSpan('root-span') |
| 122 | + ->withKind(SpanKind::KIND_SERVER) |
| 123 | + // This child span doesn't exist, so it will fail |
| 124 | + ->hasChild('non-existent-child') |
| 125 | + ->end(); |
| 126 | + |
| 127 | + // If we get here, the test failed |
| 128 | + $this->fail('Expected TraceAssertionFailedException was not thrown'); |
| 129 | + } catch (TraceAssertionFailedException $e) { |
| 130 | + // Verify that the exception message contains the expected and actual structures |
| 131 | + $message = $e->getMessage(); |
| 132 | + |
| 133 | + // Check that the message contains the expected structure |
| 134 | + $this->assertStringContainsString('Expected Trace Structure:', $message); |
| 135 | + $this->assertStringContainsString('Child Span: "non-existent-child"', $message); |
| 136 | + |
| 137 | + // Check that the message contains the actual structure |
| 138 | + $this->assertStringContainsString('Actual Trace Structure:', $message); |
| 139 | + $this->assertStringContainsString('Missing Child Span: "non-existent-child"', $message); |
| 140 | + |
| 141 | + // Verify that the exception contains the expected and actual structures |
| 142 | + $this->assertNotEmpty($e->getExpectedStructure()); |
| 143 | + $this->assertNotEmpty($e->getActualStructure()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test that the TraceAssertionFailedException provides a visual diff when a span event is missing. |
| 149 | + */ |
| 150 | + public function test_trace_assertion_failed_exception_provides_visual_diff_for_missing_event(): void |
| 151 | + { |
| 152 | + $tracer = $this->tracerProvider->getTracer('test-tracer'); |
| 153 | + |
| 154 | + // Create a root span |
| 155 | + $rootSpan = $tracer->spanBuilder('root-span') |
| 156 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 157 | + ->startSpan(); |
| 158 | + |
| 159 | + // Add an event to the root span |
| 160 | + $rootSpan->addEvent('event.one'); |
| 161 | + |
| 162 | + $rootSpan->end(); |
| 163 | + |
| 164 | + // Intentionally create an assertion that will fail due to a missing event |
| 165 | + try { |
| 166 | + $this->assertTrace($this->storage) |
| 167 | + ->hasRootSpan('root-span') |
| 168 | + ->withKind(SpanKind::KIND_SERVER) |
| 169 | + // This event doesn't exist, so it will fail |
| 170 | + ->hasEvent('non-existent-event') |
| 171 | + ->end(); |
| 172 | + |
| 173 | + // If we get here, the test failed |
| 174 | + $this->fail('Expected TraceAssertionFailedException was not thrown'); |
| 175 | + } catch (TraceAssertionFailedException $e) { |
| 176 | + // Verify that the exception message contains the expected and actual structures |
| 177 | + $message = $e->getMessage(); |
| 178 | + |
| 179 | + // Check that the message contains the expected structure |
| 180 | + $this->assertStringContainsString('Expected Trace Structure:', $message); |
| 181 | + $this->assertStringContainsString('Event: "non-existent-event"', $message); |
| 182 | + |
| 183 | + // Check that the message contains the actual structure |
| 184 | + $this->assertStringContainsString('Actual Trace Structure:', $message); |
| 185 | + $this->assertStringContainsString('Missing Event: "non-existent-event"', $message); |
| 186 | + |
| 187 | + // Verify that the exception contains the expected and actual structures |
| 188 | + $this->assertNotEmpty($e->getExpectedStructure()); |
| 189 | + $this->assertNotEmpty($e->getActualStructure()); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Test that the TraceAssertionFailedException provides a visual diff when a span kind is incorrect. |
| 195 | + */ |
| 196 | + public function test_trace_assertion_failed_exception_provides_visual_diff_for_incorrect_kind(): void |
| 197 | + { |
| 198 | + $tracer = $this->tracerProvider->getTracer('test-tracer'); |
| 199 | + |
| 200 | + // Create a root span |
| 201 | + $rootSpan = $tracer->spanBuilder('root-span') |
| 202 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 203 | + ->startSpan(); |
| 204 | + |
| 205 | + $rootSpan->end(); |
| 206 | + |
| 207 | + // Intentionally create an assertion that will fail due to an incorrect kind |
| 208 | + try { |
| 209 | + $this->assertTrace($this->storage) |
| 210 | + ->hasRootSpan('root-span') |
| 211 | + // This kind is incorrect, so it will fail |
| 212 | + ->withKind(SpanKind::KIND_CLIENT) |
| 213 | + ->end(); |
| 214 | + |
| 215 | + // If we get here, the test failed |
| 216 | + $this->fail('Expected TraceAssertionFailedException was not thrown'); |
| 217 | + } catch (TraceAssertionFailedException $e) { |
| 218 | + // Verify that the exception message contains the expected and actual structures |
| 219 | + $message = $e->getMessage(); |
| 220 | + |
| 221 | + // Check that the message contains the expected structure |
| 222 | + $this->assertStringContainsString('Expected Trace Structure:', $message); |
| 223 | + $this->assertStringContainsString('Kind: KIND_CLIENT', $message); |
| 224 | + |
| 225 | + // Check that the message contains the actual structure |
| 226 | + $this->assertStringContainsString('Actual Trace Structure:', $message); |
| 227 | + $this->assertStringContainsString('Kind: KIND_SERVER', $message); |
| 228 | + |
| 229 | + // Verify that the exception contains the expected and actual structures |
| 230 | + $this->assertNotEmpty($e->getExpectedStructure()); |
| 231 | + $this->assertNotEmpty($e->getActualStructure()); |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments