因為minimum-stability: dev而安裝到PHPUnit 6.0的雷

因為Laravel 4.2預設沒有安裝PHPUnitMockery,所以想自己用composer require安裝,但沒想到卻安裝到正在開發中的PHPUnit 6.0。重點是PHPUnit 6.0只支援PHP 7,因為Homestead 0.2.7裝的是PHP 5.6.10,而導致PHPUnit安裝失敗,而這一切的原因竟然因為是…

問題


我想在一個前人所留下的Laravel 4.2專案安裝PHPUnit,在專案目錄下了:

1
composer require phpunit/phpunit --dev

沒想到竟出現以下錯誤:

1
Using version ^6.0@dev for phpunit/phpunit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for phpunit/phpunit ^6.0@dev -> satisfiable by phpunit/phpunit[6.0.x-dev].
    - phpunit/phpunit 6.0.x-dev requires php >=7.0 -> your PHP version (5.6.10-1+deb.sury.org~trusty+1) or "config.platform.php" value does not satisfy that requirement.


Installation failed, reverting ./composer.json to its original content.

意思是即將安裝>=6.0<7.0@dev版本,即開發中的版本,重點是PHPUnit 6.0只支援PHP 7.0以上,而目前Homestead 0.2.7PHP 5.6.10,所以無法安裝PHPUnit。

討論


為什麼composer會去抓PHPUnit 6.0呢?

/composer.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*",
"barryvdh/laravel-debugbar": "1.8.*",
"fzaninotto/faker": "1.5.*@dev",
"dollar/generators": "dev-master",
"makzumi/calendar": "dev-master",
"aws/aws-sdk-php-laravel": "1.*",
"doctrine/dbal": "~2.3",
"caouecs/laravel4-lang": "~1.0",
"ramsey/array_column": "dev-master",
"barryvdh/laravel-ide-helper": "~1.11",
"maatwebsite/excel": "~1.3.0",
"phpoffice/phpexcel": "1.8.0",
"Chumper/Zipper": "0.5.x"
},
"require-dev": {
"xethron/migrations-generator": "dev-master",
"way/generators": "~2.0",
"laravel/homestead": "dev-master",
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-update-cmd":[
"php artisan clear-compiled",
"php artisan ide-helper:generate",
"php artisan optimize"

]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev"
}

63行

1
"minimum-stability": "dev"

因為宣告為dev,所以composer才去抓正在開發中PHPUnit 6.0了。

解決方法


1
"minimum-stability": "stable"

改成stable之後,再composer require phpunit/phpunit --dev 就一切正常了,會自動抓目前最新的穩定版本:PHPUnit 5.0.3

Laravel 4.2適合安裝PHPUnit 5.x嗎?

Laravel 4.2的要求是PHP 5.4,由於PHPUnit 5.x只支援PHP 5.6PHP 7,雖然在Homestead 0.2.7可以安裝PHPUnit 5.x,不過可能無法在測試主機的PHP 5.4跑testing,所以建議只安裝PHPUnit 4.x即可。

1
composer require phpunit/phpunit:~4.0 --dev

Conclusion


  • PHPUnit在Oct.02,2015宣布重要改版,PHPUnit 5.x將支援PHP 5.6PHP 7PHPUnit 6.x將只支援PHP 7,而PHP 5.3PHP 5.4PHP 5.5將只能使用PHPUnit 4.x
  • Package建議不要安裝dev版本,除非有特別原因。
  • 若專案由多人開發,建議安裝package之前檢查一下minimum-stability的設定,避免裝到dev版。