Skip to content

Commit df36e24

Browse files
committed
fix: performs the correction for the RequestContext return
1 parent ec8dc54 commit df36e24

3 files changed

Lines changed: 90 additions & 19 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ php:
66
docker exec -it hyperf-sqlcommenter-app bash
77
check:
88
tools/php-cs-fixer fix
9-
tools/psalm --output-format=console --show-info=true
9+
tools/psalm --output-format=console --show-info=true --no-cache
1010
install:
1111
phive install
1212
composer install

src/Aspect/SqlCommenterAspect.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
namespace ReinanHS\SqlCommenterHyperf\Aspect;
1717

18-
use Hyperf\Context\RequestContext;
18+
use Hyperf\Context\Context;
1919
use Hyperf\Contract\ConfigInterface;
2020
use Hyperf\Database\Connection;
2121
use Hyperf\Di\Aop\AbstractAspect;
2222
use Hyperf\Di\Aop\ProceedingJoinPoint;
2323
use Hyperf\HttpServer\Router\Dispatched;
24+
use Psr\Http\Message\ServerRequestInterface;
2425
use ReinanHS\SqlCommenterHyperf\Opentelemetry;
2526
use ReinanHS\SqlCommenterHyperf\SwitchManager;
2627
use ReinanHS\SqlCommenterHyperf\Utils;
@@ -68,24 +69,30 @@ private function appendSqlComments(string $query, string $dbDriver): string
6869
$comments['db_driver'] = $dbDriver;
6970
}
7071

71-
$request = RequestContext::get();
72-
if ($this->switchManager->isEnable('route')) {
73-
$comments['route'] = $request->getUri()->getPath();
74-
}
72+
/**
73+
* @psalm-suppress InvalidArgument
74+
* @var null|ServerRequestInterface $request
75+
*/
76+
$request = Context::get(ServerRequestInterface::class);
77+
if ($request instanceof ServerRequestInterface) {
78+
if ($this->switchManager->isEnable('route')) {
79+
$comments['route'] = $request->getUri()->getPath();
80+
}
7581

76-
if ($this->switchManager->isEnable('controller') || $this->switchManager->isEnable('action')) {
77-
/** @var null|Dispatched $dispatched */
78-
$dispatched = $request->getAttribute(Dispatched::class);
82+
if ($this->switchManager->isEnable('controller') || $this->switchManager->isEnable('action')) {
83+
/** @var null|Dispatched $dispatched */
84+
$dispatched = $request->getAttribute(Dispatched::class);
7985

80-
if ($dispatched && $dispatched->isFound()) {
81-
$parts = Utils::extractCallback($dispatched->handler?->callback);
86+
if ($dispatched && $dispatched->isFound()) {
87+
$parts = Utils::extractCallback($dispatched->handler?->callback);
8288

83-
if ($this->switchManager->isEnable('controller')) {
84-
$comments['controller'] = (string) $parts[0];
85-
}
89+
if ($this->switchManager->isEnable('controller')) {
90+
$comments['controller'] = (string) $parts[0];
91+
}
8692

87-
if ($this->switchManager->isEnable('action')) {
88-
$comments['action'] = (string) $parts[1];
93+
if ($this->switchManager->isEnable('action')) {
94+
$comments['action'] = (string) $parts[1];
95+
}
8996
}
9097
}
9198
}

tests/Aspect/SqlCommenterAspectTest.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace ReinanHS\Test\Aspect;
1717

1818
use FastRoute\Dispatcher;
19-
use Hyperf\Context\RequestContext;
19+
use Hyperf\Context\Context;
2020
use Hyperf\Contract\ConfigInterface;
2121
use Hyperf\Database\Connection;
2222
use Hyperf\Di\Aop\ProceedingJoinPoint;
@@ -25,6 +25,7 @@
2525
use Hyperf\HttpServer\Router\Handler;
2626
use Mockery;
2727
use PHPUnit\Framework\TestCase;
28+
use Psr\Http\Message\ServerRequestInterface;
2829
use Psr\Http\Message\UriInterface;
2930
use ReinanHS\SqlCommenterHyperf\Aspect\SqlCommenterAspect;
3031
use ReinanHS\SqlCommenterHyperf\Opentelemetry;
@@ -76,9 +77,9 @@ public function testProcess(): void
7677
->method('getUri')
7778
->willReturn($mockedUri);
7879

79-
$mockedContext = Mockery::mock('alias:' . RequestContext::class);
80+
$mockedContext = Mockery::mock('alias:' . Context::class);
8081
$mockedContext->shouldReceive('get')
81-
->with()
82+
->with(ServerRequestInterface::class)
8283
->andReturn($mockedRequest);
8384

8485
$mockedOpentelemetry = Mockery::mock('alias:' . Opentelemetry::class);
@@ -122,4 +123,67 @@ public function testProcess(): void
122123
$this->assertStringContainsString("traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01'", $query);
123124
$this->assertTrue($result);
124125
}
126+
127+
public function testProcessNoRequest(): void
128+
{
129+
$mockedConfig = $this->createMock(ConfigInterface::class);
130+
$mockedConfig->expects($this->exactly(2))
131+
->method('get')
132+
->willReturnOnConsecutiveCalls(true, 'TestApp');
133+
134+
$mockedSwitchManager = $this->createMock(SwitchManager::class);
135+
$mockedSwitchManager->expects($this->exactly(4))
136+
->method('isEnable')
137+
->willReturn(true);
138+
139+
$mockedProceedingJoinPoint = $this->getMockBuilder(ProceedingJoinPoint::class)
140+
->disableOriginalConstructor()
141+
->getMock();
142+
143+
$mockedContext = Mockery::mock('alias:' . Context::class);
144+
$mockedContext->shouldReceive('get')
145+
->with(ServerRequestInterface::class)
146+
->andReturn(null);
147+
148+
$mockedOpentelemetry = Mockery::mock('alias:' . Opentelemetry::class);
149+
$mockedOpentelemetry->shouldReceive('getOpentelemetryValues')
150+
->andReturn([
151+
'traceparent' => '00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01',
152+
]);
153+
154+
$mockedProceedingJoinPoint->arguments = [
155+
'keys' => [
156+
'query' => 'SELECT CURRENT_TIMESTAMP()',
157+
],
158+
];
159+
160+
$mockedConnection = $this->createMock(Connection::class);
161+
$mockedConnection->expects($this->once())
162+
->method('getDriverName')
163+
->willReturn('mysql');
164+
165+
$mockedProceedingJoinPoint->expects($this->once())
166+
->method('getInstance')
167+
->willReturn($mockedConnection);
168+
169+
$mockedProceedingJoinPoint->expects($this->once())
170+
->method('process')
171+
->willReturn(true);
172+
173+
$aspect = new SqlCommenterAspect($mockedConfig, $mockedSwitchManager);
174+
$result = $aspect->process($mockedProceedingJoinPoint);
175+
176+
$query = $mockedProceedingJoinPoint->arguments['keys']['query'];
177+
178+
$this->assertStringContainsString('SELECT CURRENT_TIMESTAMP()', $query);
179+
$this->assertStringContainsString("application='TestApp'", $query);
180+
$this->assertStringContainsString("framework='hyperf'", $query);
181+
$this->assertStringContainsString("application='TestApp'", $query);
182+
$this->assertStringContainsString("db_driver='mysql'", $query);
183+
$this->assertStringNotContainsString("route='%%2Fv1%%2Fadmin%%2Findex'", $query);
184+
$this->assertStringNotContainsString("controller='IndexController'", $query);
185+
$this->assertStringNotContainsString("action='index'", $query);
186+
$this->assertStringContainsString("traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01'", $query);
187+
$this->assertTrue($result);
188+
}
125189
}

0 commit comments

Comments
 (0)