Skip to content

Commit

Permalink
Add an env var to disable the deprecation message
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Apr 10, 2024
1 parent 59e759c commit 392ba8e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.0.1

* Add the `SMOKE_TESTING_ROUTES_METHODS` environment variable to disable the deprecation message

## v1.0.0

* Project seems quite stable, let's roll for v1.0.0
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ To be able to customize your HTTP testing, for example when you have lots of rou

If you implement these methods (that are defined as empty by default in the class), you must implement them at least as `protected`, and non-static.

#### Recommendations

As of Symfony best practices, all routes should be configured with HTTP methods (GET, POST, etc.).

If your routes are not configured this way, the `RoutesExtractor` (used by the `SmokeTestStaticRoutes` class) will trigger a `E_USER_DEPRECATED` error.

If you do not want to trigger deprecations, you can customize the contents of the `SMOKE_TESTING_ROUTES_METHODS` environment variable this way:

##### Customizing the `SMOKE_TESTING_ROUTES_METHODS` environment variable

| Values | Effect |
|--------|--------|
| `no`, `false`, `disabled`, `0` | Disables the behavior |
| A constant starting with `E_USER_` | The `trigger_error` function will trigger an error based on the constant name you specify |
| Env var not set, or any non-empty value | Triggers a `E_USER_DEPRECATED` error. |


### 🔬 Smoke test routes **manually**

Instead of (or conjointedly to) checking all routes of your app, you can run a list of URLs of your choice and have control on all request parameters and test assertions/expectations.
Expand Down
2 changes: 2 additions & 0 deletions fixture-app/config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ framework:
php_errors:
log: true

http_method_override: false

when@test:
framework:
test: true
Expand Down
11 changes: 10 additions & 1 deletion src/RoutesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ public static function extractRoutesFromRouter(RouterInterface $router): \Genera

$methods = $route->getMethods();
if (!$methods) {
trigger_error(sprintf("Route %s has no configured HTTP methods. It is recommended that you set at least one HTTP method for your route in its configuration.", $routeName), E_USER_DEPRECATED);
$errorMode = $_SERVER['SMOKE_TESTING_ROUTES_METHODS'] ?? $_ENV['SMOKE_TESTING_ROUTES_METHODS'] ?? getenv('SMOKE_TESTING_ROUTES_METHODS') ?: 'true';

if (!empty($errorMode) && 'false' !== $errorMode && 'no' !== $errorMode && '0' !== $errorMode && 'disabled' !== $errorMode) {
$errorType = E_USER_DEPRECATED;
if (str_starts_with($errorMode, 'E_USER_')) {
$errorType = constant($errorMode);
}
$message = sprintf('Route "%s" has no configured HTTP methods. It is recommended that you set at least one HTTP method for your route in its configuration.', $routeName);
trigger_error($message, $errorType);
}

$methods[] = 'GET';
}
Expand Down

0 comments on commit 392ba8e

Please sign in to comment.