thesgroup / magento2-testing-framework
Magento 2模块的测试框架
Requires
- php: ~8.1.0||~8.2.0
- laminas/laminas-code: ^4.5
- magento/framework: 103.0.*
- magento/magento-coding-standard: *
- magento/module-webapi: 100.4.*
- phpmd/phpmd: ~2.12.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- rregeer/phpunit-coverage-check: *
- sebastian/phpcpd: ^6.0
- dev-master
- 1.1.33
- 1.1.32
- 1.1.31
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.26
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-release/1.1
- dev-feature/cli
- dev-release/1.0
This package is auto-updated.
Last update: 2024-09-26 00:35:40 UTC
README
单模块测试的Magento 2静态/单元测试框架。它可以用于管道或验证Magento 2模块。
安装
在您的Magento 2项目上运行以下命令
composer require --dev thesgroup/magento2-testing-framework
管道示例
假设$MAGENTO_USER和$MAGENTO_PASS设置为管道变量。
基本用法
在大多数情况下,您可以使用基本测试套件运行PHP代码、JavaScript、PHPUnit测试。
PHP代码
有一个选项可以运行所有PHP测试,使用一个命令。它运行以下测试
- PHP编码标准验证
- 代码完整性测试
- HTML静态代码分析
- Less静态代码分析
- GraphQL静态代码分析
您可以使用以下命令运行所有测试
vendor/bin/phpcs --config-set installed_paths vendor/magento/magento-coding-standard,vendor/phpcompatibility/php-compatibility/PHPCompatibility vendor/bin/run-all-tests
当您需要删除测试文件时,可以使用清理命令
vendor/bin/cleanup
PHPUnit
运行单元测试并检查代码覆盖率阈值。
vendor/bin/phpunit-tests
执行后生成以下报告
- JUnit日志 test-reports/junit.xml
- Html覆盖率报告 test-coverage-html/
- Clover覆盖率报告 clover.xml
将代码覆盖率阈值设置为80%(默认值为70%)
vendor/bin/phpunit-tests 80
JavaScript
运行ESLint以确保JavaScript代码的质量
vendor/bin/js-tests
本地修复ESLint
npm install eslint --save-dev npx eslint -c vendor/thesgroup/magento2-testing-framework/static/js/eslint/.eslintrc --ignore-pattern=vendor/** --no-error-on-unmatched-pattern .
高级用法
PHP编码标准验证
这些测试套件包括:PHPCS、PHPMD、PHPCPD、PHPStan测试和严格类型声明。您可以使用以下命令单独运行它
vendor/bin/phpcs --config-set installed_paths vendor/magento/magento-coding-standard,vendor/phpcompatibility/php-compatibility/PHPCompatibility
vendor/bin/phpunit --testsuite="PHP Coding Standard Verification" -c vendor/thesgroup/magento2-testing-framework/static/integrity/phpunit.xml
代码完整性测试
您可以使用以下命令单独运行它
vendor/bin/phpunit --testsuite="Code Integrity Tests" -c vendor/thesgroup/magento2-testing-framework/static/integrity/phpunit.xml
HTML静态代码分析
您可以使用以下命令单独运行它
vendor/bin/phpunit --testsuite="HTML Static Code Analysis" -c vendor/thesgroup/magento2-testing-framework/static/integrity/phpunit.xml
Less静态代码分析
您可以使用以下命令单独运行它
vendor/bin/phpunit --testsuite="Less Static Code Analysis" -c vendor/thesgroup/magento2-testing-framework/static/integrity/phpunit.xml
GraphQL静态代码分析
您可以使用以下命令单独运行它
vendor/bin/phpunit --testsuite="GraphQL Static Code Analysis" -c vendor/thesgroup/magento2-testing-framework/static/integrity/phpunit.xml
PHP代码美化器(PHPCBF)
要尽可能自动修复尽可能多的嗅探违规,请使用phpcbf命令
vendor/bin/phpcs --config-set installed_paths vendor/magento/magento-coding-standard/,vendor/phpcompatibility/php-compatibility/PHPCompatibility
vendor/bin/phpcbf --standard=Magento2 .
忽略文件和文件夹
可以在模块文件夹中通过添加新行将特定文件/文件夹添加到黑名单
PHPCS
{module_dir}/Test/_files/phpcs/ignorelist/*.txt
PHPMD
{module_dir}/Test/_files/phpmd/ignorelist/*.txt
PHPCPD
{module_dir}/Test/_files/phpcpd/blacklist/*.txt
严格类型声明
{module_dir}/Test/_files/blacklist/strict_type.txt
PHPStan
{module_dir}/Test/_files/phpstan/blacklist/*.txt
关于测试的信息
Magento特定规则
通用动作
控制器(实现ActionInterface的类)必须实现标记HttpActionInterface以通过方法限制传入请求。
CookieAndSessionMisuse
会话和cookie必须在直接负责HTML展示的类中使用,因为Web API不依赖于cookie和会话。如果您需要获取当前用户,请使用Magento\Authorization\Model\UserContextInterface
FinalImplementation
在Magento中禁止使用final关键字,因为这会降低可扩展性和可定制性。final类和方法与插件和代理不兼容。
代码完整性测试
上述命令将执行以下测试
- 编译器测试。检查DI定义的编译和代码生成
- 测试块名称存在
- 测试布局声明和块元素的使用
- 测试布局文件的格式
- 测试布局声明和块元素的使用
- 测试主题布局更新中的handles的声明
- 通过各种断言测试管理区域中的ACL
- 找到adminhtml/system.xml文件并验证它们。
- 找到fieldset.xml文件并验证它们。
- 检查从\Magento\Framework\Api\ExtensibleDataInterface继承的接口。
- 找到webapi xml文件并验证它们。
- 找到widget.xml文件并验证它们。
- 扫描源代码以查找对类别的引用并查看它们是否确实存在。
- 测试强制composer.json文件的有效性和任何其他Magento组件中的约定。
- 根据声明性模式验证模块之间的依赖信息。
- 观察者实现。 (请注意:当前实现不支持虚拟类型)
- 扫描源代码,寻找不正确或未声明的模块依赖。
- 检查 Magento 模块结构是否存在循环依赖。
- 验证所有支付方式是否声明在适当的模块中。
- 测试以查找过时的安装/升级模式/数据脚本。
- 布局中过时节点的覆盖率。
- phtml 模板文件的静态测试。
- Less静态代码分析
- GraphQL静态代码分析
- 扫描源代码,检测到过时的 __() 方法的调用。
- 扫描源代码,检测到 __() 函数或短语对象的调用,分析带有参数的占位符,查看它们是否不相等。
- 将检查短语是否为空。
- xsi:noNamespaceSchemaLocation 验证。
- XML DOM 验证。
为模块做贡献
请随意分支并为此模块做出贡献,创建一个 pull request,这样我们就可以将您的更改合并到 master 分支。
致谢
感谢贡献者