ランサーズ(Lancers)エンジニアブログ > PHP > CakePHP > CakePHP2→CakePHP4へのジャンプアップ

CakePHP2→CakePHP4へのジャンプアップ

kanazawa|2020年03月31日
CakePHP

SREチーム、QAチームの金澤です。

昨年より、CakePHP2→CakePHP3の段階的バージョンアップに取り組んできましたが、
バージョンアップ対象をCakePHP3からCakePHP4に変更しました。

CakePHP2→CakePHP4にターゲットを変更する

CakePHP3へのバージョンアップを開始したのは、2019/8です。
※バージョンアップ開始時のブログはこちら

この時はまだCakePHP4は正式にリリースされていませんでしたが、
CakePHP2→CakePHP4へのジャンプアップできるか調査したところ、
PHPUnitのバージョンが合わず、見送っていました。

CakePHP2で利用できるPHPUnitの最新バージョンはPHPUnit5.7です。
CakePHP3では、PHPUnit5.7が共存できたのですが、
CakePHP4で利用できるPHPUnitの最低バージョンはPHPUnit8.5になります。

PHPUnitの共存

しかしながら、CakePHP同様、PHPUnitも共存できるのではないかと考えました。

それぞれ、以下のディレクトリに配置し、共存します。

CakePHP2:PHPUnit5.7 (cake28/Vendor/phpunit)
CakePHP3:PHPUnit6,0 (vendor/phpunit)

具体的には、以下の手順になります。

・PHPUnit5.7をvendor/phpunit からcake28/Vendor/phpunitに移動
・composer管理からgit管理にする
・composer管理のvendor/phpunit をPHPUnit6.0にバージョンアップ
・CakePHP3が対応する最上位バージョン
・cake28/Vendor/phpunit/phpunit でcomposer install
・※cake28/Vendor/phpunit/phpunit/.gitignoreからvendorを外しておく

$ php composer.phar install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 25 installs, 0 updates, 0 removals
  - Installing sebastian/diff (1.4.3): Loading from cache
  - Installing sebastian/resource-operations (1.0.0): Loading from cache
  - Installing sebastian/version (2.0.1): Loading from cache
  - Installing sebastian/environment (2.0.0): Loading from cache
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
  - Installing phpunit/php-text-template (1.2.1): Loading from cache
  - Installing phpunit/php-token-stream (1.4.12): Downloading (100%)
  - Installing phpunit/php-file-iterator (1.4.5): Loading from cache
  - Installing phpunit/php-code-coverage (4.0.8): Loading from cache
  - Installing phpunit/php-timer (1.0.9): Loading from cache
  - Installing sebastian/recursion-context (2.0.0): Loading from cache
  - Installing sebastian/exporter (2.0.0): Loading from cache
  - Installing doctrine/instantiator (1.0.5): Downloading (100%)
  - Installing phpunit/phpunit-mock-objects (3.4.4): Loading from cache
  - Installing sebastian/comparator (1.2.4): Loading from cache
  - Installing symfony/polyfill-ctype (v1.13.1): Loading from cache
  - Installing webmozart/assert (1.6.0): Loading from cache
  - Installing phpdocumentor/reflection-common (1.0.1): Downloading (100%)
  - Installing phpdocumentor/type-resolver (0.4.0): Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (3.3.2): Downloading (100%)
  - Installing phpspec/prophecy (v1.10.2): Loading from cache
  - Installing symfony/yaml (v3.4.37): Downloading (100%)
  - Installing sebastian/global-state (1.1.1): Loading from cache
  - Installing sebastian/object-enumerator (2.0.1): Loading from cache
  - Installing myclabs/deep-copy (1.7.0): Downloading (100%)
symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command)
sebastian/global-state suggests installing ext-uopz (*)
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating optimized autoload files

cake28/Vendor/phpunit/phpunit に移動したPHPUnit5.7を参照するように設定します。

cake28/Config/bootstrap.php

define('__PHPUNIT_PHAR__', '');
require ROOT . '/vendor/autoload.php';
require_once APP . 'Vendor/phpunit/phpunit/vendor/autoload.php';

PHPUnitの実行については、それぞれ以下のようにコマンドを実行します。
CakePHP4のphpunit.xmlは新規にtestsディレクトリに配置してphpunit実行時に指定するようにしています。

CakePHP2のUT実行

./cake28/Console/cake l_test app Lib/Function/CryptTest.php

CakePHP4のUT実行

./bin/phpunit --bootstrap config/bootstrap.php --configuration=tests/phpunit.xml tests/TestCase/Lib/Functions/CryptTest.php

CakePHP4へのバージョンアップ

composerの各ライブラリを最新にし、CakePHP4に対応させます。

CakePHP4へのバージョンアップ手順についてはの詳細は、年末のブログで紹介しています。

CakePHP4にバージョンアップできれば、PHPUnitも8.5以上にバージョンアップにされるはずです。

今後のCakePHPバージョンアップ計画

CakePHP2→CakePHP3に段階的にバージョンアップ

現在のランサーズのリポジトリには

・ランサーズ本体
・管理画面
・バッチ

が全て含まれていますが、そのうち、

・管理画面
・バッチ

CakePHP4で新規構築して移行し、lancers本体のソースを徐々に削っています。

この新規構築で得たCakePHP4のノウハウをlancers本体のリポジトリにも反映していく予定です。