shopsys / http-smoke-testing
HTTP smoke test case for testing all configured routes in your Symfony project
Requires
- php: ^8.3
- doctrine/annotations: ^1.10.4
- phpunit/phpunit: ^11.2.1
- symfony/dependency-injection: ^5.4
- symfony/framework-bundle: ^5.4
- symfony/http-foundation: ^5.4
- symfony/routing: ^5.4
- 16.0.x-dev
- 15.0.x-dev
- v15.0.0
- 14.0.x-dev
- v14.0.1
- v14.0.0
- 13.0.x-dev
- v13.0.0
- 12.1.x-dev
- 12.0.x-dev
- v12.0.0
- v11.1.0
- dev-master / 11.0.x-dev
- v11.0.0
- 10.0.x-dev
- v10.0.5
- v10.0.4
- v10.0.3
- v10.0.2
- v10.0.1
- v10.0.0
- 9.1.x-dev
- v9.1.3
- v9.1.2
- v9.1.1
- v9.1.0
- 9.0.x-dev
- v9.0.4
- v9.0.3
- v9.0.2
- v9.0.1
- v9.0.0
- 8.1.x-dev
- v8.1.2
- v8.1.1
- v8.1.0
- 8.0.x-dev
- v8.0.0
- 7.3.x-dev
- v7.3.7
- v7.3.6
- v7.3.5
- v7.3.4
- v7.3.3
- v7.3.2
- v7.3.1
- v7.3.0
- 7.2.x-dev
- v7.2.2
- v7.2.1
- v7.2.0
- 7.1.x-dev
- v7.1.1
- v7.1.0
- 7.0.x-dev
- v7.0.1
- v7.0.0
- v7.0.0-beta6
- v7.0.0-beta5
- v7.0.0-beta4
- v7.0.0-beta3
- v7.0.0-beta2
- v7.0.0-beta1
- v7.0.0-alpha6
- v7.0.0-alpha5
- v7.0.0-alpha4
- v7.0.0-alpha3
- v7.0.0-alpha2
- v7.0.0-alpha1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-alpha
- dev-rv-stocks
- dev-tl-convertim-backend
- dev-tl-fix-nonexisting-class-framework
- dev-pt-yaml-standards-vol-2
- dev-rc-12-0-0
- dev-tl-fix-failing-12-0-builds
- dev-mg-js-translations-fix
- dev-mg-split-repo
- dev-rc-11-1-0
- dev-mg-deployment
- dev-mg-project-cold-fusion
- dev-mg-remove-manifests
- dev-tl-fix-project-base
- dev-rc-11-0-0
- dev-rc-10-0-5
- dev-rk-fw-uprage-from-10-to-11-part-3
- dev-rc-v9-1-3
- dev-rc-10-0-4
- dev-rc-10-0-3
- dev-rc-10-0-2
- dev-rc-10-0-1
- dev-mg-fw-933-remove-rootdir
- dev-mg-release-lock
- dev-mg-remove-twig-extension
- dev-rk-fw-1019-api-query-name-in-symfony-profiler
- dev-rk-fw-uprage-from-10-to-11-part-1
- dev-rk-fw-uprage-from-10-to-11-part-2
- dev-rc-10-0-0
- dev-mg-upgrade-php8
- dev-mg-remove-be-api-integration
- dev-rv-fw-837-doctrine-upgrade
- dev-rv-fw-847-migrations-tests
- dev-mg-php-versions
- dev-rv-fw-810-fix-actions
- dev-mg-improve-entity-extension
- dev-rc-v9-1-2
- dev-mg-allow-dependency
- dev-mg-fix-phpstan-packages
- dev-mg-force-proxy-manager-version
- dev-mg-update-node
- dev-origin/tl-phpstan-all-packages
- dev-tl-release-9-1-1
- dev-ds-coding-standards-allow-symfony-5-higher
- dev-tl-remove-package-builder
- dev-tl-github-actions-for-packages
- dev-rc-v7-3-7
- dev-ds-fix-ecs
- dev-mg-update-helios-elfinder
- dev-tp-allow-coding-standards-for-symfony5
- dev-rc-v9-1-0
- dev-rc-v9-0-4
- dev-mg-php74-only
- dev-tl-fix-docker-limits
- dev-rc-v9-0-3
- dev-ds-more-coding-standards
- dev-ds-ecs-path-fix
- dev-rc-v9-0-2
- dev-ds-eof-new-line
- dev-ds-phpstan-lvl-5
- dev-tl-fix-phpstan
- dev-rc-v9-0-1
- dev-ds-fixed-framework-standaards
- dev-tl-add-psr-14
- dev-rc-v8-1-2
- dev-rc-v9-0-0
- dev-tl-fix-current-customer-user
- dev-tp-js-fixes
- dev-vitek-rostislav-patch-3
- dev-tg-grunt-webpack
- dev-tp-symfony4
- dev-rv-eur-on-first-domain
- dev-tp-update-composer
- dev-ds-easier-elastic-extensebility
- dev-rc-v8-1-1
- dev-rc-v8-1-0
- dev-tp-npm-link
- dev-mg-weird-flex-but-ok
- dev-ds-fix-project-base-dependency
- dev-ds-fix-sed-macos
- dev-mg-graphql-categories
- dev-tl-rv-datafixtures-refactoring
- dev-ds-kubernetes-simplify
- dev-ds-kubernetes-simplify-test
- dev-rv-extended-classes-fixer
- dev-rv-phpstan-lvl4
- dev-ds-test-install-script-automatically
- dev-rc-8-0-0
- dev-ss-api-import-products
- dev-do-version-lock
- dev-rc-v7-3-3
- dev-rc-v7-3-4
- dev-rc-v7-3-5
- dev-rc-v7-3-6
- dev-ss-api-separated-from-default-project-base
- dev-do-elastic-structure-deploy
- dev-mg-fix-k8s-https
- dev-ph-price-calculation
- dev-ph-redis-clients
- dev-rc-v7-3-0
- dev-tl-fix-tests-bc-break
- dev-tl-removed-tests-bc-break
- dev-tl-united-non-bc-break-service-injections
- dev-ss-tl-api-products
- dev-tl-end-support-php-71
- dev-bb-flysystem-volume-driver
- dev-bb-initcontainer-owner-fix
- dev-bb-multidomain-sitemap
- dev-do-mg-mail-attachments
- dev-do-phpstan-upgrade
- dev-ph-phing-test
- dev-rv-read-model-bck
- dev-jg-redis-cache
- dev-mg-elastic-filtering
- dev-bb-ecs-hotfix
- dev-do-fixtures-project-base
- dev-jg-form-ordering
- dev-mg-multiple-cron
- dev-rv-better-php-doc-parser-config
- dev-rv-upgrade-ecs
- dev-mc-cdn-bucket
- dev-sspooky13-pt-yaml-standards
- dev-do-product-export-fix
- dev-mc-ph-google-cloud-bundle
This package is auto-updated.
Last update: 2024-11-15 10:04:48 UTC
README
This package enables you to do simple HTTP smoke testing of your Symfony application.
Basically, 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.
While this is not a very sophisticated check, it can answer the essential question "does it run?". It prevents you from triggering 500 Server Error on some seemingly unrelated page when you are doing changes in shared code. Moreover, after initial configuration it is almost maintenance-free as it checks any new routes automatically.
This repository is maintained by shopsys/shopsys monorepo, information about changes is in monorepo CHANGELOG.md.
Installation
Add the package to require-dev
in your application:
composer require --dev shopsys/http-smoke-testing
This package internally uses PHPUnit to run the tests.
That means that you need to setup your phpunit.xml
properly.
Fortunately, Symfony comes with example configuration.
Renaming the phpunit.xml.dist
in your project root (or app/phpunit.xml.dist
on Symfony 2) should be sufficient.
Note: If you did not find the file in your project check out the example in Symfony Standard Edition.
Usage
Create new PHPUnit test extending \Shopsys\HttpSmokeTesting\HttpSmokeTestCase
class and implement customizeRouteConfigs
method.
You can run your new test by:
php vendor/bin/phpunit tests/AppBundle/Smoke/SmokeTest.php
(or php bin/phpunit -c app/phpunit.xml src/AppBundle/Tests/Smoke/SmokeTest.php
on Symfony 2)
Warning: This package checks all routes by making real requests. It is important not to execute it on production data. You may unknowingly delete or modify your data or real requests on 3rd party services. Even if you implement some way of protecting the application from side-effect (eg. database transaction wrapping) you should never execute tests on production data.
Example test class
namespace Tests\AppBundle\Smoke; use Shopsys\HttpSmokeTesting\Auth\BasicHttpAuth; use Shopsys\HttpSmokeTesting\HttpSmokeTestCase; use Shopsys\HttpSmokeTesting\RouteConfig; use Shopsys\HttpSmokeTesting\RouteConfigCustomizer; use Shopsys\HttpSmokeTesting\RouteInfo; use Symfony\Component\HttpFoundation\Request; class SmokeTest extends HttpSmokeTestCase { /** * @param \Shopsys\HttpSmokeTesting\RouteConfigCustomizer $routeConfigCustomizer */ protected function customizeRouteConfigs(RouteConfigCustomizer $routeConfigCustomizer) { $routeConfigCustomizer ->customize(function (RouteConfig $config, RouteInfo $info) { // This function will be called on every RouteConfig provided by RouterAdapter if ($info->getRouteName()[0] === '_') { // You can use RouteConfig to change expected behavior or skip testing particular routes $config->skipRoute('Route name is prefixed with "_" meaning internal route.'); } }) ->customizeByRouteName('acme_demo_secured_hello', function (RouteConfig $config, RouteInfo $info) { // You can customize RouteConfig to use authentication for secured routes $config->changeDefaultRequestDataSet('Log in as "user".') ->setAuth(new BasicHttpAuth('user', 'userpass')); }); } /** * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\Response */ protected function handleRequest(Request $request) { $entityManager = self::$kernel->getContainer()->get('doctrine.orm.entity_manager'); // Enclose request handling in rolled-back database transaction to prevent side-effects $entityManager->beginTransaction(); $response = parent::handleRequest($request); $entityManager->rollback(); return $response; } }
Documentation
By 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.
To change this behavior you must implement method customizeRouteConfigs(RouteConfigCustomizer $routeConfigCustomizer)
in your test.
RouteConfigCustomizer
provides two methods for customizing individual route requests:
customize
accepts callbackfunction (RouteConfig $config, RouteInfo $info) {...}
as the only argument. This is called with eachRouteConfig
along withRouteInfo
collected from your router.
This method is useful when you want to define general rules for multiple routes (eg. skip all routes with name starting with underscore).customizeByRouteName
accepts a single route name or an array of route names as the first argument and same callback ascustomize
as the second argument. This is called with eachRouteConfig
along withRouteInfo
with matching route name. If matching route config is not found aRouteNameNotFoundException
is thrown.
This method is useful when you want to define rules for specific routes (eg. logging in to some secured route).
In your customizing callback you can call three methods on RouteConfig
to change the tested behavior:
skipRoute
can be called to skip this route during test.changeDefaultRequestDataSet
is the main method for configuring routes. It returnsRequestDataSet
object offering the setters needed to change the actual behavior:setExpectedStatusCode
changes the expected response HTTP status code that will be asserted.setAuth
changes the authentication method for the route. (UseNoAuth
for anonymous access,BasicHttpAuth
for logging in via basic http headers or implement your own method usingAuthInterface
.)setParameter
specifies value of a route parameter by name.addCallDuringTestExecution
adds a callbackfunction (RequestDataSet $requestDataSet, ContainerInterface $container) { ... }
to be called before test execution.
(Useful for code that needs to access the same instance of container as the test method, eg. adding CSRF token as a route parameter)
addExtraRequestDataSet
can be used to test more requests on the same route (eg. test a secured route as both logged in and anonymous user). ReturnsRequestDataSet
that you can use the same way as the result fromchangeDefaultRequestDataSet
. All configured options will extend the values from default request data set (even when you change the defaultRequestDataSet
after you add the extraRequestDataSet
).
Note: All three methods of RouteConfigCustomizer
accept string $debugNote
as an argument.
It is useful for describing the reasons of your configuration change because it may help you with debugging when the test fails.
Additionally you can override these methods in your implementation of HttpSmokeTestCase
to further change the test behavior:
setUp
to change the way your kernel is booted (eg. boot it with different options).getRouterAdapter
to change the object responsible for collecting routes from your application and generating urls.createRequest
if you have specific needs about the wayRequest
is created fromRequestDataSet
.handleRequest
to customize handlingRequest
in your application (eg. you can wrap it in database transaction to roll it back into original state).
Annotations
To make smoke test configuration a little easier, you can use the annotations:
DataSet
Used for setting expected status code based on provided paramteters.
@DataSet(statusCode=404, parameters={
@Parameter(name="name", value="Batman")
})
- arguments:
parameters
(optional)statusCode
(optional, default =200
)
Parameter
Parameter defines value for specified parameter.
@Parameter(name="name", value="Batman")
- arguments:
name
(required)value
(required)
Skipped
Mark test as skipped
@Skipped()
You can add them directly to your controller methods. See the example in Shopsys\HttpSmokeTesting\Test\TestController
.
Note: You should avoid using annotations with configuring via changeDefaultRequestDataSet()
on same route. It may result in unexpected behavior.
Troubleshooting
Tests do not fail on non-existing route
PHPUnit by default does not fail on warnings. Setting failOnWarning="true"
in phpunit.xml
fixes this problem.
Contributing
Thank you for your contributions to Shopsys HTTP Smoke Testing package. Together we are making Shopsys Platform better.
This repository is READ-ONLY. If you want to report issues and/or send pull requests, please use the main Shopsys repository.
Please, check our Contribution Guide before contributing.
Support
What to do when you are in troubles or need some help? The best way is to join our Slack.
If you want to report issues, please use the main Shopsys repository.