Skip to content

Commit 1d91e37

Browse files
committed
(test): Update tests for adapter refactor and query parser extraction
1 parent 6bad65c commit 1d91e37

9 files changed

Lines changed: 188 additions & 179 deletions

tests/AdapterActionsTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Utopia\Tests;
44

55
use PHPUnit\Framework\TestCase;
6-
use Utopia\Proxy\Adapter\HTTP\Swoole as HTTPAdapter;
7-
use Utopia\Proxy\Adapter\SMTP\Swoole as SMTPAdapter;
8-
use Utopia\Proxy\Adapter\TCP\Swoole as TCPAdapter;
6+
use Utopia\Proxy\Adapter;
7+
use Utopia\Proxy\Adapter\TCP as TCPAdapter;
8+
use Utopia\Proxy\Protocol;
99
use Utopia\Proxy\Resolver\Exception as ResolverException;
1010

1111
class AdapterActionsTest extends TestCase
@@ -23,9 +23,9 @@ protected function setUp(): void
2323

2424
public function test_resolver_is_assigned_to_adapters(): void
2525
{
26-
$http = new HTTPAdapter($this->resolver);
26+
$http = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
2727
$tcp = new TCPAdapter($this->resolver, port: 5432);
28-
$smtp = new SMTPAdapter($this->resolver);
28+
$smtp = new Adapter($this->resolver, name: 'SMTP', protocol: Protocol::SMTP);
2929

3030
$this->assertSame($this->resolver, $http->resolver);
3131
$this->assertSame($this->resolver, $tcp->resolver);
@@ -35,18 +35,18 @@ public function test_resolver_is_assigned_to_adapters(): void
3535
public function test_resolve_routes_and_returns_endpoint(): void
3636
{
3737
$this->resolver->setEndpoint('127.0.0.1:8080');
38-
$adapter = new HTTPAdapter($this->resolver);
38+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
3939
$adapter->setSkipValidation(true);
4040

4141
$result = $adapter->route('api.example.com');
4242

4343
$this->assertSame('127.0.0.1:8080', $result->endpoint);
44-
$this->assertSame('http', $result->protocol);
44+
$this->assertSame(Protocol::HTTP, $result->protocol);
4545
}
4646

4747
public function test_notify_connect_delegates_to_resolver(): void
4848
{
49-
$adapter = new HTTPAdapter($this->resolver);
49+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
5050

5151
$adapter->notifyConnect('resource-123', ['extra' => 'data']);
5252

@@ -58,7 +58,7 @@ public function test_notify_connect_delegates_to_resolver(): void
5858

5959
public function test_notify_close_delegates_to_resolver(): void
6060
{
61-
$adapter = new HTTPAdapter($this->resolver);
61+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
6262

6363
$adapter->notifyClose('resource-123', ['extra' => 'data']);
6464

@@ -70,29 +70,29 @@ public function test_notify_close_delegates_to_resolver(): void
7070

7171
public function test_track_activity_delegates_to_resolver_with_throttling(): void
7272
{
73-
$adapter = new HTTPAdapter($this->resolver);
73+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
7474
$adapter->setActivityInterval(1); // 1 second throttle
7575

7676
// First call should trigger activity tracking
77-
$adapter->trackActivity('resource-123');
77+
$adapter->track('resource-123');
7878
$this->assertCount(1, $this->resolver->getActivities());
7979

8080
// Immediate second call should be throttled
81-
$adapter->trackActivity('resource-123');
81+
$adapter->track('resource-123');
8282
$this->assertCount(1, $this->resolver->getActivities());
8383

8484
// Wait for throttle interval to pass
8585
sleep(2);
8686

8787
// Third call should trigger activity tracking
88-
$adapter->trackActivity('resource-123');
88+
$adapter->track('resource-123');
8989
$this->assertCount(2, $this->resolver->getActivities());
9090
}
9191

9292
public function test_routing_error_throws_exception(): void
9393
{
9494
$this->resolver->setException(new ResolverException('No backend found'));
95-
$adapter = new HTTPAdapter($this->resolver);
95+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
9696

9797
$this->expectException(ResolverException::class);
9898
$this->expectExceptionMessage('No backend found');
@@ -103,7 +103,7 @@ public function test_routing_error_throws_exception(): void
103103
public function test_empty_endpoint_throws_exception(): void
104104
{
105105
$this->resolver->setEndpoint('');
106-
$adapter = new HTTPAdapter($this->resolver);
106+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
107107

108108
$this->expectException(ResolverException::class);
109109
$this->expectExceptionMessage('Resolver returned empty endpoint');
@@ -115,7 +115,7 @@ public function test_skip_validation_allows_private_i_ps(): void
115115
{
116116
// 10.0.0.1 is a private IP that would normally be blocked
117117
$this->resolver->setEndpoint('10.0.0.1:8080');
118-
$adapter = new HTTPAdapter($this->resolver);
118+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
119119
$adapter->setSkipValidation(true);
120120

121121
// Should not throw exception with validation disabled

tests/AdapterMetadataTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Utopia\Tests;
44

55
use PHPUnit\Framework\TestCase;
6-
use Utopia\Proxy\Adapter\HTTP\Swoole as HTTPAdapter;
7-
use Utopia\Proxy\Adapter\SMTP\Swoole as SMTPAdapter;
8-
use Utopia\Proxy\Adapter\TCP\Swoole as TCPAdapter;
6+
use Utopia\Proxy\Adapter;
7+
use Utopia\Proxy\Adapter\TCP as TCPAdapter;
8+
use Utopia\Proxy\Protocol;
99

1010
class AdapterMetadataTest extends TestCase
1111
{
@@ -22,29 +22,29 @@ protected function setUp(): void
2222

2323
public function test_http_adapter_metadata(): void
2424
{
25-
$adapter = new HTTPAdapter($this->resolver);
25+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP, description: 'HTTP proxy adapter');
2626

2727
$this->assertSame('HTTP', $adapter->getName());
28-
$this->assertSame('http', $adapter->getProtocol());
29-
$this->assertSame('HTTP proxy adapter for routing requests to function containers', $adapter->getDescription());
28+
$this->assertSame(Protocol::HTTP, $adapter->getProtocol());
29+
$this->assertSame('HTTP proxy adapter', $adapter->getDescription());
3030
}
3131

3232
public function test_smtp_adapter_metadata(): void
3333
{
34-
$adapter = new SMTPAdapter($this->resolver);
34+
$adapter = new Adapter($this->resolver, name: 'SMTP', protocol: Protocol::SMTP, description: 'SMTP proxy adapter');
3535

3636
$this->assertSame('SMTP', $adapter->getName());
37-
$this->assertSame('smtp', $adapter->getProtocol());
38-
$this->assertSame('SMTP proxy adapter for email server routing', $adapter->getDescription());
37+
$this->assertSame(Protocol::SMTP, $adapter->getProtocol());
38+
$this->assertSame('SMTP proxy adapter', $adapter->getDescription());
3939
}
4040

4141
public function test_tcp_adapter_metadata(): void
4242
{
4343
$adapter = new TCPAdapter($this->resolver, port: 5432);
4444

4545
$this->assertSame('TCP', $adapter->getName());
46-
$this->assertSame('postgresql', $adapter->getProtocol());
47-
$this->assertSame('TCP proxy adapter for database connections (PostgreSQL, MySQL)', $adapter->getDescription());
46+
$this->assertSame(Protocol::PostgreSQL, $adapter->getProtocol());
47+
$this->assertSame('TCP proxy adapter for database connections (PostgreSQL, MySQL, MongoDB)', $adapter->getDescription());
4848
$this->assertSame(5432, $adapter->port);
4949
}
5050
}

tests/AdapterStatsTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace Utopia\Tests;
44

55
use PHPUnit\Framework\TestCase;
6-
use Utopia\Proxy\Adapter\HTTP\Swoole as HTTPAdapter;
6+
use Utopia\Proxy\Adapter;
7+
use Utopia\Proxy\Protocol;
78
use Utopia\Proxy\Resolver\Exception as ResolverException;
89

910
class AdapterStatsTest extends TestCase
@@ -22,7 +23,7 @@ protected function setUp(): void
2223
public function test_cache_hit_updates_stats(): void
2324
{
2425
$this->resolver->setEndpoint('127.0.0.1:8080');
25-
$adapter = new HTTPAdapter($this->resolver);
26+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
2627
$adapter->setSkipValidation(true);
2728

2829
$start = time();
@@ -38,18 +39,18 @@ public function test_cache_hit_updates_stats(): void
3839

3940
$stats = $adapter->getStats();
4041
$this->assertSame(2, $stats['connections']);
41-
$this->assertSame(1, $stats['cache_hits']);
42-
$this->assertSame(1, $stats['cache_misses']);
43-
$this->assertSame(50.0, $stats['cache_hit_rate']);
44-
$this->assertSame(0, $stats['routing_errors']);
45-
$this->assertSame(1, $stats['routing_table_size']);
46-
$this->assertGreaterThan(0, $stats['routing_table_memory']);
42+
$this->assertSame(1, $stats['cacheHits']);
43+
$this->assertSame(1, $stats['cacheMisses']);
44+
$this->assertSame(50.0, $stats['cacheHitRate']);
45+
$this->assertSame(0, $stats['routingErrors']);
46+
$this->assertSame(1, $stats['routingTableSize']);
47+
$this->assertGreaterThan(0, $stats['routingTableMemory']);
4748
}
4849

4950
public function test_routing_error_increments_stats(): void
5051
{
5152
$this->resolver->setException(new ResolverException('No backend'));
52-
$adapter = new HTTPAdapter($this->resolver);
53+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
5354

5455
try {
5556
$adapter->route('api.example.com');
@@ -59,16 +60,16 @@ public function test_routing_error_increments_stats(): void
5960
}
6061

6162
$stats = $adapter->getStats();
62-
$this->assertSame(1, $stats['routing_errors']);
63-
$this->assertSame(1, $stats['cache_misses']);
64-
$this->assertSame(0, $stats['cache_hits']);
65-
$this->assertSame(0.0, $stats['cache_hit_rate']);
63+
$this->assertSame(1, $stats['routingErrors']);
64+
$this->assertSame(1, $stats['cacheMisses']);
65+
$this->assertSame(0, $stats['cacheHits']);
66+
$this->assertSame(0.0, $stats['cacheHitRate']);
6667
}
6768

6869
public function test_resolver_stats_are_included_in_adapter_stats(): void
6970
{
7071
$this->resolver->setEndpoint('127.0.0.1:8080');
71-
$adapter = new HTTPAdapter($this->resolver);
72+
$adapter = new Adapter($this->resolver, name: 'HTTP', protocol: Protocol::HTTP);
7273
$adapter->setSkipValidation(true);
7374

7475
$adapter->route('api.example.com');

tests/ConnectionResultTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Utopia\Proxy\ConnectionResult;
7+
use Utopia\Proxy\Protocol;
78

89
class ConnectionResultTest extends TestCase
910
{
1011
public function test_connection_result_stores_values(): void
1112
{
1213
$result = new ConnectionResult(
1314
endpoint: '127.0.0.1:8080',
14-
protocol: 'http',
15+
protocol: Protocol::HTTP,
1516
metadata: ['cached' => false]
1617
);
1718

1819
$this->assertSame('127.0.0.1:8080', $result->endpoint);
19-
$this->assertSame('http', $result->protocol);
20+
$this->assertSame(Protocol::HTTP, $result->protocol);
2021
$this->assertSame(['cached' => false], $result->metadata);
2122
}
2223
}

0 commit comments

Comments
 (0)