mano / autotest-bundle
Symfony 扩展包,用于自动测试路由。
Requires
- php: >=7.2
- symfony/browser-kit: ^5.1
- symfony/framework-bundle: ^5.1
- symfony/phpunit-bridge: ^5.1
Requires (Dev)
README
Symfony 扩展包,用于自动测试路由 - 检查框架中定义的所有静态路由的响应代码。它支持 PHPUnit 和 Codeception 测试。该模块列出了所有可用的 GET 方法路由,添加默认值并筛选出不包含通配符的路由。未匹配的路由可以通过手动添加 - 查看 配置。然后仅检查成功的响应代码。
为了访问受保护的路由,请在配置中设置管理员邮箱(或任何具有超级权限的用户)。用户实体上必须有 'email' 属性(或至少有 getter)。一些路由可能需要排除(例如 '/logout'、'/login'),因为它们会被重定向。
安装
- 使用 Composer 安装此扩展包
composer require mano/autotest-bundle --dev
- 在 config/test/bundles.php 中启用扩展包,通过粘贴新的数组项
[ ... Mano\AutotestBundle\AutotestBundle::class => ['dev' => true, 'test' => true], ]
配置
创建文件 config/packages/test/autotest.yaml,并在必要时覆盖默认值。
# 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
排除路径
一些路由可能需要排除(例如 '/logout'、'/login'),因为它们会被重定向。建议在路由注释中声明允许的方法,这样您就不需要在这里排除只包含 POST 的路由。可以使用正则表达式一次匹配多个路由。
exclude: - '/logout' - '/login' - '/foo/{name}' # if the route had default name (and therefore resolved) - '/foo/.*' # pattern will be wrapped by ~^regex$~i
包含路径
所有无法自动解析的路由(包含无法从默认值中填充的通配符)都可以在这里列出,以便在测试中包含。
未解析路径的完整列表将在测试开始时输出。包含在路径中的路由将被从输出中移除。
include: - name: route_name path: '/foo/bar' ]
使用方法
在运行测试之前,需要在项目修改后清除缓存。
PHPUnit
将条目添加到 phpunit.xml(.dist)
<testsuites> <testsuite name="Project Test Suite"> <directory>tests</directory> --> <directory>vendor/mano/autotest-bundle/src/Test/</directory> </testsuite> </testsuites>
运行
php bin/phpunit
或者您可以选择不编辑 phpunit.xml,只需扩展任何测试。
class someTest extends \Mano\AutotestBundle\Test\PhpUnitWebTest
如果您遇到 "用户存储库 'App\Repository\UserRepository' 未找到" 的异常,并且您确信它存在,请将服务注册为公共的。
# config/services.yaml services: ... App\Repository\UserRepository: public: true
Codeception
- 将模块添加到套件中(functional.suite.yml)
- \Mano\AutotestBundle\Test\CodeceptionAutotestModule
- 将模块方法 autotest 添加到任何 cest
class fooCest { public function testStaticPages(FunctionalTester $I) { $I->autotest(); } }
Codeception 对登录的要求更严格 - 默认情况下,不同的身份验证器(API 密钥)可能无法正常工作。
扩展
所使用的路径解析器很简单 - 它只是向路径添加默认值,并仅获取不包含通配符的 GET 方法(在完成默认值后)。
可以使用自定义解析器,并从配置中引用它。解析器必须实现 Mano\AutotestBundle\PathResolverInterface。