mano/autotest-bundle

Symfony bundle for automatic routes testing.

Installs: 248

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 1

Forks: 1

Open Issues: 0

Type:symfony-bundle

v1.1.2 2022-02-17 13:00 UTC

This package is auto-updated.

Last update: 2024-04-17 18:22:04 UTC


README

Symfony bundle for automatic routes testing - check the response code of all static routes defined in the framework. It supports both PHPUnit and Codeception testing. The module lists all the available routes with GET method, add default values and filter just those that does not contain wildcards. The routes that were not matched can be added manually though - see config. Then the response is checked just for the successful code.

In order to access authorised routes, set admin email in the config (or any user that has super privilege). There must be 'email' property on the user entity (or at least getter). Some routes might need to be excluded though (ex. '/logout','/login') as they are redirected.

Installation

composer require mano/autotest-bundle --dev
  • enable the bundle in config/test/bundles.php by pasting new array item
[   
    ...
    Mano\AutotestBundle\AutotestBundle::class => ['dev' => true, 'test' => true],
]

Config

create file config/packages/test/autotest.yaml and override the defaults if necessary.

# Default configuration for extension with alias: "autotest"
autotest:

    # Paths that will be excluded from the test. Exclusion is made before resolving paths therefore the regex may include curly braces (if the route to be excluded contains them).
    exclude: []
    
    # Paths that will be included into the test as they could not be resolved automatically. Array of object in format name:{nameOfRoute}, path:{manuallyAddedPath}'
    include: []

    # Custom resolver to be used - must implement PathResolverInterface
    resolver: null

    # Email of admin that can access all the routes - if authorisation needed.
    admin_email: null

    # User repository to be used - must have email property defined.
    user_repository: App\Repository\UserRepository

exclude paths

Some routes might need to be excluded though (ex. '/logout','/login') as they are redirected. It is advisable to declare allowed methods in the route annotation, so that you do not need to exclude POST only routes here. Regex can be used here to match multiple routes at once.

exclude: 
   - '/logout'
   - '/login'
   - '/foo/{name}' # if the route had default name (and therefore resolved)
   - '/foo/.*' # pattern will be wrapped by ~^regex$~i

include paths

All the routes that could not be automatically resolved (contain wildcard that can not be filled from defaults) can be listed here to be included in the test.

The full list of unresolved paths is outputted at the beginning of the test. A route listed in included paths is removed from the output.

include:
  - name: route_name
    path: '/foo/bar'
]

Usage

Cache needs to be cleared after modifications in the project before running the test.

PHPUnit

Add entry to phpunit.xml(.dist)

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>tests</directory> -->
        <directory>vendor/mano/autotest-bundle/src/Test/</directory>
    </testsuite> 
</testsuites>

run

php bin/phpunit

Alternatively you can skip editing phpunit.xml and just extend any test.

class someTest extends \Mano\AutotestBundle\Test\PhpUnitWebTest

If you come across the exception "User repository 'App\Repository\UserRepository' not found" and you are sure it exists, register the service as public.

# config/services.yaml
services:
    ...
    App\Repository\UserRepository:
      public: true

Codeception

  • add the module to a suite (functional.suite.yml)
- \Mano\AutotestBundle\Test\CodeceptionAutotestModule
  • add module method autotest to any cest
class fooCest
{
    public function testStaticPages(FunctionalTester $I)
    {
        $I->autotest();
    }
}

Codeception is stricter with login - different authenticators (api keys) will not work out of the box.

Extension

The used path resolver is simple - it just adds defaults to the path and takes only GET method that does not contain wildcards (after completing defaults).

Custom resolver can be used and referenced from config. The resolver must implement Mano\AutotestBundle\PathResolverInterface.