|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Tests\Instrumentation\PostgreSql\Integration; |
| 6 | + |
| 7 | +use OpenTelemetry\Contrib\Instrumentation\PostgreSql\PgSqlTracker; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +class PgSqlTrackerTest extends TestCase |
| 10 | +{ |
| 11 | + public function test_query_split(): void |
| 12 | + { |
| 13 | + $sql = "SELECT 1; SELECT 2;"; |
| 14 | + $queries = PgSqlTracker::splitQueries($sql); |
| 15 | + |
| 16 | + $this->assertSame(['SELECT 1;', 'SELECT 2;'], $queries); |
| 17 | + } |
| 18 | + |
| 19 | + public function test_parse_standard_connection_string(): void |
| 20 | + { |
| 21 | + $result = PgSqlTracker::parsePgConnString("host=localhost port=5432 dbname=mydb user=otel password=secret"); |
| 22 | + |
| 23 | + $this->assertSame('localhost', $result['host']); |
| 24 | + $this->assertSame('5432', $result['port']); |
| 25 | + $this->assertSame('mydb', $result['dbname']); |
| 26 | + $this->assertSame('otel', $result['user']); |
| 27 | + $this->assertSame('secret', $result['password']); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_parse_quoted_values(): void |
| 31 | + { |
| 32 | + $result = PgSqlTracker::parsePgConnString("host='localhost' dbname=\"my db\" user='user name'"); |
| 33 | + |
| 34 | + $this->assertSame('localhost', $result['host']); |
| 35 | + $this->assertSame('my db', $result['dbname']); |
| 36 | + $this->assertSame('user name', $result['user']); |
| 37 | + } |
| 38 | + public function test_parse_socket_only(): void |
| 39 | + { |
| 40 | + $result = PgSqlTracker::parsePgConnString("dbname=mydb user=postgres"); |
| 41 | + |
| 42 | + $this->assertArrayNotHasKey('host', $result); |
| 43 | + $this->assertSame('mydb', $result['dbname']); |
| 44 | + $this->assertSame('postgres', $result['user']); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_parse_empty_string(): void |
| 48 | + { |
| 49 | + $result = PgSqlTracker::parsePgConnString(""); |
| 50 | + $this->assertSame([], $result); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_parse_attributes_from_connstring_with_host(): void |
| 54 | + { |
| 55 | + $connString = "host=localhost port=5432 dbname=testdb user=otel"; |
| 56 | + $attrs = PgSqlTracker::parseAttributesFromConnectionString($connString); |
| 57 | + |
| 58 | + $this->assertSame('localhost', $attrs['server.address']); |
| 59 | + $this->assertSame('5432', $attrs['server.port']); |
| 60 | + $this->assertSame('testdb', $attrs['db.namespace']); |
| 61 | + $this->assertSame('postgresql', $attrs['db.system.name']); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_parse_attributes_from_connstring_socket(): void |
| 65 | + { |
| 66 | + $connString = "dbname=testdb user=otel"; |
| 67 | + $attrs = PgSqlTracker::parseAttributesFromConnectionString($connString); |
| 68 | + |
| 69 | + $this->assertSame(null, $attrs['server.address']); |
| 70 | + $this->assertSame('testdb', $attrs['db.namespace']); |
| 71 | + $this->assertSame('postgresql', $attrs['db.system.name']); |
| 72 | + } |
| 73 | + |
| 74 | + public function test_basic_split(): void |
| 75 | + { |
| 76 | + $sql = "SELECT * FROM users; INSERT INTO logs (message) VALUES ('test');"; |
| 77 | + $expected = [ |
| 78 | + 'SELECT * FROM users;', |
| 79 | + "INSERT INTO logs (message) VALUES ('test');", |
| 80 | + ]; |
| 81 | + |
| 82 | + $result = PgSqlTracker::splitQueries($sql); |
| 83 | + $this->assertEquals($expected, $result); |
| 84 | + } |
| 85 | + |
| 86 | + public function test_whitespace_variants(): void |
| 87 | + { |
| 88 | + $sql = " SELECT 1;\n\nINSERT INTO test VALUES (2); SELECT 3"; |
| 89 | + $expected = [ |
| 90 | + 'SELECT 1;', |
| 91 | + 'INSERT INTO test VALUES (2);', |
| 92 | + 'SELECT 3', |
| 93 | + ]; |
| 94 | + |
| 95 | + $result = PgSqlTracker::splitQueries($sql); |
| 96 | + $this->assertEquals($expected, $result); |
| 97 | + } |
| 98 | + |
| 99 | + public function test_semicolon_in_quotes(): void |
| 100 | + { |
| 101 | + $sql = "INSERT INTO x (text) VALUES ('abc;def'); SELECT 1;"; |
| 102 | + $expected = [ |
| 103 | + "INSERT INTO x (text) VALUES ('abc;def');", |
| 104 | + 'SELECT 1;', |
| 105 | + ]; |
| 106 | + |
| 107 | + $result = PgSqlTracker::splitQueries($sql); |
| 108 | + $this->assertEquals($expected, $result); |
| 109 | + } |
| 110 | + public function test_empty_input(): void |
| 111 | + { |
| 112 | + $sql = "\n\n\t "; |
| 113 | + $expected = []; |
| 114 | + $result = PgSqlTracker::splitQueries($sql); |
| 115 | + $this->assertEquals($expected, $result); |
| 116 | + } |
| 117 | + |
| 118 | +} |
| 119 | + |
0 commit comments