tomkyle/find-run-test 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tomkyle/find-run-test

Composer 安装命令:

composer require tomkyle/find-run-test

包简介

Find and run the PHPUnit test for a single changed PHP class file, most useful when watching the filesystem

README 文档

README

Composer Version PHP version GitHub Actions Workflow Status Packagist License

About

FRT automatically executes the associated PHPUnit test for a changed source code file.

Comparison with spatie/phpunit-watcher: spatie/phpunit-watcher re-executes the entire PHPUnit test suite once a source code file has changed. The executed tests can be restricted to specific tests using the --filter option. This means that regardless of which file is changed or saved, only the specified unit tests are executed.

$ vendor/bin/phpunit-watcher watch
$ vendor/bin/phpunit-watcher watch --filter=certain_test

tomkyle/find-run-test takes a different approach: After saving a file, only the PHPUnit test associated with the modified file is executed.

COMPARISON tomkyle/frt spatie/phpunit-watcher
Watch process via chokidar in package.json phpunit-watcher command
Configuration CLI options Config file and CLI options
Customization basic better
Chaining with phpstan, phpcs or rector via built-in shell && operators none
Open shells needed One Two
Tests run Those associated with changed file All or all specified tests

Installation

Install FRT with Composer:

$ composer require --dev tomkyle/find-run-test

How it works

The vendor/bin/frt executable expects the PHP file of which the source code has changed as argument. It will then look for any PHPUnit test file which suits the class name of the changed file and run the associated test. For example:

  1. You update file src/MyClass.php and pass that file as argument to FRT.

    $ vendor/bin/frt src/MyClass.php
  2. FRT will look for matching PHPUnit tests inside the project’s tests directory.

  3. If there is a test file MyClassTest.php, FRT will hand it over to PHPUnit, like so:

    $ vendor/bin/phpunit --filter=MyClassTest.php

Automation

Our goal is to automate testing as much as possible, and so do we with running unit tests. In the Node-based app developing world, Chokidar is one of the go-to file watching libraries, most often used for frontend building with webpack, gulp, workbox, and such. We will utilize it for FRT.

Install Chokidar with NPM:

$ npm install --dev chokidar-cli

Create a watch task inside the project root’s package.json, for example:

{
  "name": "my-app",
  "devDependencies": {
    "chokidar-cli": "^3.0.0"
  },
  "scripts": {
    "watch": "chokidar \"src/**/*.php\" -c \"./vendor/bin/frt {path}\"",
  }
}

Back to our project root: Start the watch process with npm run watch. Chokidar will now report any PHP source code change and hand over the changed file to FRT, which in turn will call PHPUnit:

$ npm run watch
# Output will be like ...

Watching "src/**/*.php" ..

Assuming we just edited src/SomeNamespace/MyClass.php, we get this:

change:src/SomeNamespace/MyClass.php
PHPUnit 11.3.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.10
Configuration: /Users/john_doe/vendor-project/phpunit.dist.xml

....  4 / 4 (100%)

Time: 00:00.016, Memory: 10.00 MB

My Class (tests\Unit\MyClassTest)
 ✔ Construct
 ✔ Get styles
 ✔ Get colors
 ✔ Get throws exception for invalid color

OK (4 tests, 5 assertions)

In case no associated PHPUnit test can be found, FRT will report this:

No test available: src/SomeInterface.php

Chain with other test tools

Install the PHP development tools using Composer:

$ composer require --dev phpstan/phpstan
$ composer require --dev rector/rector
$ compsoer require --dev friendsofphp/php-cs-fixer
# If not already
$ composer require --dev phpunit/phpunit

Add the PHP tools as NPM scripts to package.json – see the full example in examples/package.json

{
  "name": "my-app",
  "devDependencies": {
    "chokidar-cli": "^3.0.0",
    "npm-run-all": "^4.1.5"
  },
  "scripts": {

    "watch": "npm-run-all -p watch:*",
    "watch:src": "chokidar \"src/**/*.php\" -c \"./vendor/bin/frt {path} && npm run phpstan {path} && npm run rector {path}\"",
    "watch:tests": "chokidar \"tests/**/*.php\" -c \"npm run phpunit:short {path}\"",

    "phpstan": "./vendor/bin/phpstan --no-progress analyse",

    "phpcs": "npm run phpcs:apply -- --dry-run",
    "phpcs:apply": "./vendor/bin/php-cs-fixer fix --verbose --diff",

    "rector": "npm run rector:apply -- --dry-run",
    "rector:apply": "./vendor/bin/rector process",

    "phpunit": "./vendor/bin/phpunit --testdox",
    "phpunit:short": "npm run phpunit -- --no-coverage"
  }
}

Start watching:

$ npm run watch
# Output will be like ...

Watching "src/**/*.php" ..

Development

Clone repo and install requirements

$ git clone git@github.com:tomkyle/frt.git
$ composer install
$ pnpm install

Watch source and run various tests

This will watch changes inside the src/ and tests/ directories and run a series of tests:

  1. Find and run the according unit test with PHPUnit.
  2. Find possible bugs and documentation isses using phpstan.
  3. Analyse code style and give hints on newer syntax using Rector.
$ npm run watch

Run all tests

$ npm run phpunit

tomkyle/find-run-test 适用场景与选型建议

tomkyle/find-run-test 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.96k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「filesystem」 「phpunit」 「unit testing」 「watcher」 「test runner」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tomkyle/find-run-test 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tomkyle/find-run-test 我们能提供哪些服务?
定制开发 / 二次开发

基于 tomkyle/find-run-test 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-10