如何将参数传递给自定义作曲家脚本

How to pass arguments to custom composer script

在我的 Laravel 项目中,我想 运行 phpunit 用于单个文件,例如:

$ composer run-script test tests/Unit/Services/OrderServiceTest.php

这是我的作曲家设置:

"scripts": {
    "test": [
        "@testenv",
        "@phpunit"
    ],
    "testenv": [
        "php artisan config:cache --env=testing",
        "php artisan config:clear"
    ],
    "phpunit": [
        "php ./vendor/phpunit/phpunit/phpunit --"
    ]
}

但是,我运行遇到了这个错误:

www@287dd7480e22:/var/www$ composer run-script test tests/Unit/Services/OrderServiceTest.php

php artisan config:cache --env=testing 'tests/Unit/Services/OrderServiceTest.php'

Too many arguments, expected arguments "command".

通知:

根据this comment in composer repository

There is no way to pass arguments only to one of the sub-scripts if you do a script group and call multiple things..

有一个技巧可以让您忽略子脚本的参数:

"scripts": {
    "testenv": [
        "php artisan config:cache --env=testing || exit $?",
        ...
    ],
    ...
}

这里发生的是你的参数(比方说 OrderServiceTest.php)附加到整个命令行,但是 shell 命令 exit 忽略退出代码后的任何进一步参数.对于退出代码,我们设置 $? 这是最后一个 运行 命令的退出代码,以便不改变整体结果。