|
16 | 16 | namespace ReinanHS\Test\Aspect; |
17 | 17 |
|
18 | 18 | use FastRoute\Dispatcher; |
19 | | -use Hyperf\Context\RequestContext; |
| 19 | +use Hyperf\Context\Context; |
20 | 20 | use Hyperf\Contract\ConfigInterface; |
21 | 21 | use Hyperf\Database\Connection; |
22 | 22 | use Hyperf\Di\Aop\ProceedingJoinPoint; |
|
25 | 25 | use Hyperf\HttpServer\Router\Handler; |
26 | 26 | use Mockery; |
27 | 27 | use PHPUnit\Framework\TestCase; |
| 28 | +use Psr\Http\Message\ServerRequestInterface; |
28 | 29 | use Psr\Http\Message\UriInterface; |
29 | 30 | use ReinanHS\SqlCommenterHyperf\Aspect\SqlCommenterAspect; |
30 | 31 | use ReinanHS\SqlCommenterHyperf\Opentelemetry; |
@@ -76,9 +77,9 @@ public function testProcess(): void |
76 | 77 | ->method('getUri') |
77 | 78 | ->willReturn($mockedUri); |
78 | 79 |
|
79 | | - $mockedContext = Mockery::mock('alias:' . RequestContext::class); |
| 80 | + $mockedContext = Mockery::mock('alias:' . Context::class); |
80 | 81 | $mockedContext->shouldReceive('get') |
81 | | - ->with() |
| 82 | + ->with(ServerRequestInterface::class) |
82 | 83 | ->andReturn($mockedRequest); |
83 | 84 |
|
84 | 85 | $mockedOpentelemetry = Mockery::mock('alias:' . Opentelemetry::class); |
@@ -122,4 +123,67 @@ public function testProcess(): void |
122 | 123 | $this->assertStringContainsString("traceparent='00-5bd66ef5095369c7b0d1f8f4bd33716a-c532cb4098ac3dd2-01'", $query); |
123 | 124 | $this->assertTrue($result); |
124 | 125 | } |
| 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 | + } |
125 | 189 | } |
0 commit comments