|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\graphql\Kernel\Framework; |
| 4 | + |
| 5 | +use Drupal\Core\Logger\RfcLoggerTrait; |
| 6 | +use Drupal\Tests\graphql\Kernel\GraphQLTestBase; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test error logging. |
| 11 | + * |
| 12 | + * @group graphql |
| 13 | + */ |
| 14 | +class LoggerTest extends GraphQLTestBase implements LoggerInterface { |
| 15 | + |
| 16 | + use RfcLoggerTrait; |
| 17 | + |
| 18 | + /** |
| 19 | + * Loggers calls. |
| 20 | + * |
| 21 | + * @var array |
| 22 | + */ |
| 23 | + protected $loggerCalls = []; |
| 24 | + |
| 25 | + /** |
| 26 | + * {@inheritdoc} |
| 27 | + */ |
| 28 | + protected function setUp(): void { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $schema = <<<GQL |
| 32 | + schema { |
| 33 | + query: Query |
| 34 | + } |
| 35 | + type Query { |
| 36 | + resolvesToNull: String! |
| 37 | + throwsException: String! |
| 38 | + takesIntArgument(id: Int!): String |
| 39 | + } |
| 40 | +GQL; |
| 41 | + |
| 42 | + $this->setUpSchema($schema); |
| 43 | + |
| 44 | + $this->mockResolver('Query', 'resolvesToNull', NULL); |
| 45 | + $this->mockResolver('Query', 'throwsException', function () { |
| 46 | + throw new \Exception('BOOM!'); |
| 47 | + }); |
| 48 | + $this->mockResolver('Query', 'takesIntArgument'); |
| 49 | + |
| 50 | + $this->container->get('logger.factory')->addLogger($this); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + public function log($level, $message, array $context = []) { |
| 57 | + $this->loggerCalls[] = [ |
| 58 | + 'level' => $level, |
| 59 | + 'message' => $message, |
| 60 | + 'context' => $context, |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test if invariant violation errors are logged. |
| 66 | + */ |
| 67 | + public function testInvariantViolationError(): void { |
| 68 | + $result = $this->query('query { resolvesToNull }'); |
| 69 | + $this->assertSame(200, $result->getStatusCode()); |
| 70 | + // Client should not see the actual error. |
| 71 | + $this->assertSame([ |
| 72 | + 'errors' => [ |
| 73 | + [ |
| 74 | + 'message' => 'Internal server error', |
| 75 | + 'extensions' => [ |
| 76 | + 'category' => 'internal', |
| 77 | + ], |
| 78 | + 'locations' => [ |
| 79 | + [ |
| 80 | + 'line' => 1, |
| 81 | + 'column' => 9, |
| 82 | + ], |
| 83 | + ], |
| 84 | + 'path' => [ |
| 85 | + 'resolvesToNull', |
| 86 | + ], |
| 87 | + ], |
| 88 | + ], |
| 89 | + ], json_decode($result->getContent(), TRUE)); |
| 90 | + // The error should be logged. |
| 91 | + $this->assertCount(1, $this->loggerCalls); |
| 92 | + $loggerCall = reset($this->loggerCalls); |
| 93 | + $details = json_decode($loggerCall['context']['details'], TRUE); |
| 94 | + $this->assertSame($details['$operation']['query'], 'query { resolvesToNull }'); |
| 95 | + $this->assertSame($details['$operation']['variables'], []); |
| 96 | + $this->assertCount(1, $details['$result->errors']); |
| 97 | + $this->assertSame( |
| 98 | + $details['$result->errors'][0]['message'], |
| 99 | + 'Cannot return null for non-nullable field "Query.resolvesToNull".' |
| 100 | + ); |
| 101 | + $this->assertStringContainsString( |
| 102 | + 'For error #0: GraphQL\Error\InvariantViolation: Cannot return null for non-nullable field "Query.resolvesToNull".', |
| 103 | + $loggerCall['context']['previous'] |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test if exceptions thrown from resolvers are logged. |
| 109 | + */ |
| 110 | + public function testException(): void { |
| 111 | + $result = $this->query('query { throwsException }'); |
| 112 | + $this->assertSame(200, $result->getStatusCode()); |
| 113 | + // Client should not see the actual error. |
| 114 | + $this->assertSame([ |
| 115 | + 'errors' => [ |
| 116 | + [ |
| 117 | + 'message' => 'Internal server error', |
| 118 | + 'extensions' => [ |
| 119 | + 'category' => 'internal', |
| 120 | + ], |
| 121 | + 'locations' => [ |
| 122 | + [ |
| 123 | + 'line' => 1, |
| 124 | + 'column' => 9, |
| 125 | + ], |
| 126 | + ], |
| 127 | + 'path' => [ |
| 128 | + 'throwsException', |
| 129 | + ], |
| 130 | + ], |
| 131 | + ], |
| 132 | + ], json_decode($result->getContent(), TRUE)); |
| 133 | + // The error should be logged. |
| 134 | + $this->assertCount(1, $this->loggerCalls); |
| 135 | + $loggerCall = reset($this->loggerCalls); |
| 136 | + $details = json_decode($loggerCall['context']['details'], TRUE); |
| 137 | + $this->assertSame($details['$operation']['query'], 'query { throwsException }'); |
| 138 | + $this->assertSame($details['$operation']['variables'], []); |
| 139 | + $this->assertCount(1, $details['$result->errors']); |
| 140 | + $this->assertSame($details['$result->errors'][0]['message'], 'BOOM!'); |
| 141 | + $this->assertStringContainsString( |
| 142 | + 'For error #0: Exception: BOOM!', |
| 143 | + $loggerCall['context']['previous'] |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test if client error are not logged. |
| 149 | + */ |
| 150 | + public function testClientError(): void { |
| 151 | + $result = $this->query('query { takesIntArgument(id: "boom") }'); |
| 152 | + $this->assertSame(200, $result->getStatusCode()); |
| 153 | + // The error should be reported back to client. |
| 154 | + $this->assertSame([ |
| 155 | + 'errors' => [ |
| 156 | + 0 => [ |
| 157 | + 'message' => 'Field "takesIntArgument" argument "id" requires type Int!, found "boom".', |
| 158 | + 'extensions' => [ |
| 159 | + 'category' => 'graphql', |
| 160 | + ], |
| 161 | + 'locations' => [ |
| 162 | + 0 => [ |
| 163 | + 'line' => 1, |
| 164 | + 'column' => 30, |
| 165 | + ], |
| 166 | + ], |
| 167 | + ], |
| 168 | + ], |
| 169 | + ], json_decode($result->getContent(), TRUE)); |
| 170 | + // The error should not be logged. |
| 171 | + $this->assertCount(0, $this->loggerCalls); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments