|
15 | 15 | use Studio\OpenApiContractTesting\OpenApiSpecLoader; |
16 | 16 | use Studio\OpenApiContractTesting\Tests\Helpers\CreatesTestResponse; |
17 | 17 |
|
| 18 | +use function array_filter; |
| 19 | +use function count; |
| 20 | +use function explode; |
18 | 21 | use function json_encode; |
| 22 | +use function str_starts_with; |
| 23 | +use function trim; |
19 | 24 |
|
20 | 25 | // Load namespace-level config() mock before the trait resolves the function call. |
21 | 26 | require_once __DIR__ . '/../Helpers/LaravelConfigMock.php'; |
@@ -100,4 +105,89 @@ public function configured_default_spec_validates_successfully(): void |
100 | 105 | '/v1/pets', |
101 | 106 | ); |
102 | 107 | } |
| 108 | + |
| 109 | + // ======================================== |
| 110 | + // max_errors config tests |
| 111 | + // ======================================== |
| 112 | + |
| 113 | + #[Test] |
| 114 | + public function max_errors_config_limits_reported_errors(): void |
| 115 | + { |
| 116 | + $GLOBALS['__openapi_testing_config']['openapi-contract-testing.default_spec'] = 'petstore-3.0'; |
| 117 | + $GLOBALS['__openapi_testing_config']['openapi-contract-testing.max_errors'] = 1; |
| 118 | + |
| 119 | + $body = (string) json_encode( |
| 120 | + ['data' => [['id' => 'bad', 'name' => 123], ['id' => 'bad', 'name' => 456]]], |
| 121 | + JSON_THROW_ON_ERROR, |
| 122 | + ); |
| 123 | + $response = $this->makeTestResponse($body, 200); |
| 124 | + |
| 125 | + try { |
| 126 | + $this->assertResponseMatchesOpenApiSchema( |
| 127 | + $response, |
| 128 | + HttpMethod::GET, |
| 129 | + '/v1/pets', |
| 130 | + ); |
| 131 | + $this->fail('Expected AssertionFailedError was not thrown.'); |
| 132 | + } catch (AssertionFailedError $e) { |
| 133 | + // With max_errors=1, the error message should contain exactly one schema error line. |
| 134 | + // The error format is "[path] message", so count lines starting with "[". |
| 135 | + $lines = explode("\n", $e->getMessage()); |
| 136 | + $errorLines = array_filter($lines, static fn(string $line) => str_starts_with(trim($line), '[')); |
| 137 | + $this->assertCount(1, $errorLines); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + #[Test] |
| 142 | + public function string_numeric_max_errors_config_is_cast_to_int(): void |
| 143 | + { |
| 144 | + $GLOBALS['__openapi_testing_config']['openapi-contract-testing.default_spec'] = 'petstore-3.0'; |
| 145 | + $GLOBALS['__openapi_testing_config']['openapi-contract-testing.max_errors'] = '1'; |
| 146 | + |
| 147 | + $body = (string) json_encode( |
| 148 | + ['data' => [['id' => 'bad', 'name' => 123], ['id' => 'bad', 'name' => 456]]], |
| 149 | + JSON_THROW_ON_ERROR, |
| 150 | + ); |
| 151 | + $response = $this->makeTestResponse($body, 200); |
| 152 | + |
| 153 | + try { |
| 154 | + $this->assertResponseMatchesOpenApiSchema( |
| 155 | + $response, |
| 156 | + HttpMethod::GET, |
| 157 | + '/v1/pets', |
| 158 | + ); |
| 159 | + $this->fail('Expected AssertionFailedError was not thrown.'); |
| 160 | + } catch (AssertionFailedError $e) { |
| 161 | + $lines = explode("\n", $e->getMessage()); |
| 162 | + $errorLines = array_filter($lines, static fn(string $line) => str_starts_with(trim($line), '[')); |
| 163 | + $this->assertCount(1, $errorLines); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + #[Test] |
| 168 | + public function non_numeric_max_errors_config_falls_back_to_default(): void |
| 169 | + { |
| 170 | + $GLOBALS['__openapi_testing_config']['openapi-contract-testing.default_spec'] = 'petstore-3.0'; |
| 171 | + $GLOBALS['__openapi_testing_config']['openapi-contract-testing.max_errors'] = 'not-a-number'; |
| 172 | + |
| 173 | + $body = (string) json_encode( |
| 174 | + ['data' => [['id' => 'bad', 'name' => 123], ['id' => 'bad', 'name' => 456]]], |
| 175 | + JSON_THROW_ON_ERROR, |
| 176 | + ); |
| 177 | + $response = $this->makeTestResponse($body, 200); |
| 178 | + |
| 179 | + try { |
| 180 | + $this->assertResponseMatchesOpenApiSchema( |
| 181 | + $response, |
| 182 | + HttpMethod::GET, |
| 183 | + '/v1/pets', |
| 184 | + ); |
| 185 | + $this->fail('Expected AssertionFailedError was not thrown.'); |
| 186 | + } catch (AssertionFailedError $e) { |
| 187 | + // Falls back to default of 20, so multiple errors should be reported |
| 188 | + $lines = explode("\n", $e->getMessage()); |
| 189 | + $errorLines = array_filter($lines, static fn(string $line) => str_starts_with(trim($line), '[')); |
| 190 | + $this->assertGreaterThan(1, count($errorLines)); |
| 191 | + } |
| 192 | + } |
103 | 193 | } |
0 commit comments