{"id":15014481,"url":"https://github.com/shopsys/http-smoke-testing","last_synced_at":"2025-04-12T15:37:00.620Z","repository":{"id":49268845,"uuid":"92179274","full_name":"shopsys/http-smoke-testing","owner":"shopsys","description":"[READ-ONLY] Simple HTTP smoke testing for your Symfony application","archived":false,"fork":false,"pushed_at":"2025-03-13T10:02:44.000Z","size":194,"stargazers_count":65,"open_issues_count":0,"forks_count":10,"subscribers_count":9,"default_branch":"17.0","last_synced_at":"2025-04-03T15:11:14.022Z","etag":null,"topics":["phpunit","routing","smoke-test","symfony","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shopsys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-23T13:57:14.000Z","updated_at":"2025-03-13T10:02:49.000Z","dependencies_parsed_at":"2023-09-23T10:13:05.053Z","dependency_job_id":"273bba19-21fa-45ba-95ea-5c7d6f9a7874","html_url":"https://github.com/shopsys/http-smoke-testing","commit_stats":null,"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopsys%2Fhttp-smoke-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopsys%2Fhttp-smoke-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopsys%2Fhttp-smoke-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shopsys%2Fhttp-smoke-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shopsys","download_url":"https://codeload.github.com/shopsys/http-smoke-testing/tar.gz/refs/heads/17.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248590454,"owners_count":21129822,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["phpunit","routing","smoke-test","symfony","testing"],"created_at":"2024-09-24T19:45:40.963Z","updated_at":"2025-04-12T15:37:00.562Z","avatar_url":"https://github.com/shopsys.png","language":"PHP","readme":"# Shopsys HTTP Smoke Testing\n\n[![Downloads](https://img.shields.io/packagist/dt/shopsys/http-smoke-testing.svg)](https://packagist.org/packages/shopsys/http-smoke-testing)\n\nThis package enables you to do simple HTTP smoke testing of your Symfony application.\n\nBasically, it generates a HTTP request for every page (controller action) provided by the application router and then asserts that the returned HTTP response code is correct.\n\nWhile this is not a very sophisticated check, it can answer the essential question _\"does it run?\"_.\nIt prevents you from triggering _500 Server Error_ on some seemingly unrelated page when you are doing changes in shared code.\nMoreover, after initial configuration it is almost maintenance-free as it checks any new routes automatically.\n\nThis repository is maintained by [shopsys/shopsys] monorepo, information about changes is in [monorepo CHANGELOG.md](https://github.com/shopsys/shopsys/blob/master/CHANGELOG.md).\n\n## Installation\n\nAdd the package to `require-dev` in your application:\n\n```\ncomposer require --dev shopsys/http-smoke-testing\n```\n\nThis package internally uses [PHPUnit](https://phpunit.de/) to run the tests.\nThat means that you need to setup your `phpunit.xml` properly.\nFortunately, Symfony comes with example configuration.\nRenaming the `phpunit.xml.dist` in your project root (or `app/phpunit.xml.dist` on Symfony 2) should be sufficient.\n\n_Note: If you did not find the file in your project check out the example in [Symfony Standard Edition](https://github.com/symfony/symfony-standard)._\n\n## Usage\n\nCreate [new PHPUnit test](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html) extending [`\\Shopsys\\HttpSmokeTesting\\HttpSmokeTestCase`](./src/HttpSmokeTestCase.php) class and implement `customizeRouteConfigs` method.\n\nYou can run your new test by:\n\n```\nphp vendor/bin/phpunit tests/AppBundle/Smoke/SmokeTest.php\n```\n\n(or `php bin/phpunit -c app/phpunit.xml src/AppBundle/Tests/Smoke/SmokeTest.php` on Symfony 2)\n\n**Warning: This package checks all routes by making real requests.**\n**It is important not to execute it on production data.**\n**You may unknowingly delete or modify your data or real requests on 3rd party services.**\nEven if you implement some way of protecting the application from side-effect (eg. database transaction wrapping) you should never execute tests on production data.\n\n### Example test class\n\n```php\nnamespace Tests\\AppBundle\\Smoke;\n\nuse Shopsys\\HttpSmokeTesting\\Auth\\BasicHttpAuth;\nuse Shopsys\\HttpSmokeTesting\\HttpSmokeTestCase;\nuse Shopsys\\HttpSmokeTesting\\RouteConfig;\nuse Shopsys\\HttpSmokeTesting\\RouteConfigCustomizer;\nuse Shopsys\\HttpSmokeTesting\\RouteInfo;\nuse Symfony\\Component\\HttpFoundation\\Request;\n\nclass SmokeTest extends HttpSmokeTestCase {\n    /**\n     * @param \\Shopsys\\HttpSmokeTesting\\RouteConfigCustomizer $routeConfigCustomizer\n     */\n    protected function customizeRouteConfigs(RouteConfigCustomizer $routeConfigCustomizer)\n    {\n        $routeConfigCustomizer\n            -\u003ecustomize(function (RouteConfig $config, RouteInfo $info) {\n                // This function will be called on every RouteConfig provided by RouterAdapter\n                if ($info-\u003egetRouteName()[0] === '_') {\n                    // You can use RouteConfig to change expected behavior or skip testing particular routes\n                    $config-\u003eskipRoute('Route name is prefixed with \"_\" meaning internal route.');\n                }\n            })\n            -\u003ecustomizeByRouteName('acme_demo_secured_hello', function (RouteConfig $config, RouteInfo $info) {\n                // You can customize RouteConfig to use authentication for secured routes\n                $config-\u003echangeDefaultRequestDataSet('Log in as \"user\".')\n                    -\u003esetAuth(new BasicHttpAuth('user', 'userpass'));\n            });\n    }\n\n    /**\n     * @param \\Symfony\\Component\\HttpFoundation\\Request $request\n     * @return \\Symfony\\Component\\HttpFoundation\\Response\n     */\n    protected function handleRequest(Request $request)\n    {\n        $entityManager = self::$kernel-\u003egetContainer()-\u003eget('doctrine.orm.entity_manager');\n\n        // Enclose request handling in rolled-back database transaction to prevent side-effects\n        $entityManager-\u003ebeginTransaction();\n        $response = parent::handleRequest($request);\n        $entityManager-\u003erollback();\n\n        return $response;\n    }\n}\n```\n\n## Documentation\n\nBy default the test makes request to every route without using any authentication or providing any parameters and expects the response to have HTTP status code _200 OK_.\n\nTo change this behavior you must implement method `customizeRouteConfigs(RouteConfigCustomizer $routeConfigCustomizer)` in your test.\n\n[`RouteConfigCustomizer`](./src/RouteConfigCustomizer.php) provides two methods for customizing individual route requests:\n\n- `customize` accepts callback `function (RouteConfig $config, RouteInfo $info) {...}` as the only argument.\n  This is called with each [`RouteConfig`](./src/RouteConfig.php) along with [`RouteInfo`](./src/RouteInfo.php) collected from your router.  \n  This method is useful when you want to define general rules for multiple routes (eg. skip all routes with name starting with underscore).\n- `customizeByRouteName` accepts a single route name or an array of route names as the first argument and same callback as `customize` as the second argument.\n  This is called with each [`RouteConfig`](./src/RouteConfig.php) along with [`RouteInfo`](./src/RouteInfo.php) with matching route name.\n  If matching route config is not found a [`RouteNameNotFoundException`](./src/Exception/RouteNameNotFoundException.php) is thrown.  \n  This method is useful when you want to define rules for specific routes (eg. logging in to some secured route).\n\nIn your customizing callback you can call three methods on [`RouteConfig`](./src/RouteConfig.php) to change the tested behavior:\n\n- `skipRoute` can be called to skip this route during test.\n- `changeDefaultRequestDataSet` is the main method for configuring routes.\n  It returns [`RequestDataSet`](./src/RequestDataSet.php) object offering the setters needed to change the actual behavior:\n    - `setExpectedStatusCode` changes the expected response HTTP status code that will be asserted.\n    - `setAuth` changes the authentication method for the route.\n      (Use [`NoAuth`](./src/Auth/NoAuth.php) for anonymous access, [`BasicHttpAuth`](./src/Auth/BasicHttpAuth.php) for logging in via basic http headers\n      or implement your own method using [`AuthInterface`](./src/Auth/AuthInterface.php).)\n    - `setParameter` specifies value of a route parameter by name.\n    - `addCallDuringTestExecution` adds a callback `function (RequestDataSet $requestDataSet, ContainerInterface $container) { ... }` to be called before test execution.  \n      (Useful for code that needs to access the same instance of container as the test method, eg. adding CSRF token as a route parameter)\n- `addExtraRequestDataSet` can be used to test more requests on the same route (eg. test a secured route as both logged in and anonymous user).\n  Returns [`RequestDataSet`](./src/RequestDataSet.php) that you can use the same way as the result from `changeDefaultRequestDataSet`.\n  All configured options will extend the values from default request data set (even when you change the default [`RequestDataSet`](./src/RequestDataSet.php) after you add the extra [`RequestDataSet`](./src/RequestDataSet.php)).\n\n_Note: All three methods of [`RouteConfigCustomizer`](./src/RouteConfigCustomizer.php) accept `string $debugNote` as an argument._\n_It is useful for describing the reasons of your configuration change because it may help you with debugging when the test fails._\n\nAdditionally you can override these methods in your implementation of [`HttpSmokeTestCase`](./src/HttpSmokeTestCase.php) to further change the test behavior:\n\n- `setUp` to change the way your kernel is booted (eg. boot it with different options).\n- `getRouterAdapter` to change the object responsible for collecting routes from your application and generating urls.\n- `createRequest` if you have specific needs about the way `Request` is created from [`RequestDataSet`](./src/RequestDataSet.php).\n- `handleRequest` to customize handling `Request` in your application (eg. you can wrap it in database transaction to roll it back into original state).\n\n### Annotations\n\nTo make smoke test configuration a little easier, you can use the annotations:\n\n#### DataSet\n\nUsed for setting expected status code based on provided paramteters.\n\n```\n@DataSet(statusCode=404, parameters={\n    @Parameter(name=\"name\", value=\"Batman\")\n})\n```\n\n- arguments:\n    - `parameters` _(optional)_\n    - `statusCode` _(optional, default = `200`)_\n\n#### Parameter\n\nParameter defines value for specified parameter.\n\n```\n@Parameter(name=\"name\", value=\"Batman\")\n```\n\n- arguments:\n    - `name` _(required)_\n    - `value` _(required)_\n\n#### Skipped\n\nMark test as skipped\n\n```\n@Skipped()\n```\n\nYou can add them directly to your controller methods. See the example in [`Shopsys\\HttpSmokeTesting\\Test\\TestController`](./src/Test/TestController.php).\n\n_Note: You should avoid using annotations with configuring via `changeDefaultRequestDataSet()` on same route. It may result in unexpected behavior._\n\n## Troubleshooting\n\n### Tests do not fail on non-existing route\n\nPHPUnit by default does not fail on warnings. Setting `failOnWarning=\"true\"` in `phpunit.xml` fixes this problem.\n\n## Contributing\n\nThank you for your contributions to Shopsys HTTP Smoke Testing package.\nTogether we are making Shopsys Platform better.\n\nThis repository is READ-ONLY.\nIf you want to [report issues](https://github.com/shopsys/shopsys/issues/new) and/or send [pull requests](https://github.com/shopsys/shopsys/compare),\nplease use the main [Shopsys repository](https://github.com/shopsys/shopsys).\n\nPlease, check our [Contribution Guide](https://github.com/shopsys/shopsys/blob/master/CONTRIBUTING.md) before contributing.\n\n## Support\n\nWhat to do when you are in troubles or need some help?\nThe best way is to join our [Slack](https://join.slack.com/t/shopsysframework/shared_invite/zt-11wx9au4g-e5pXei73UJydHRQ7nVApAQ).\n\nIf you want to [report issues](https://github.com/shopsys/shopsys/issues/new), please use the main [Shopsys repository](https://github.com/shopsys/shopsys).\n\n[shopsys/shopsys]: (https://github.com/shopsys/shopsys)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopsys%2Fhttp-smoke-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshopsys%2Fhttp-smoke-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopsys%2Fhttp-smoke-testing/lists"}