今更ですが必要になったので。
環境
$ php --version
PHP 7.2.10-0ubuntu1 (cli) (built: Sep 13 2018 13:38:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu1, Copyright (c) 1999-2018, by Zend Technologies
$ composer --version
Composer version 1.8.0 2018-12-03 10:31:16
なおOSはUbuntuからフォークされたPop!_OSというOSです。
$ cat /etc/os-release
NAME="Pop!_OS"
VERSION="18.10"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Pop!_OS 18.10"
VERSION_ID="18.10"
HOME_URL="https://system76.com/pop"
SUPPORT_URL="http://support.system76.com"
BUG_REPORT_URL="https://github.com/pop-os/pop/issues"
PRIVACY_POLICY_URL="https://system76.com/privacy"
VERSION_CODENAME=cosmic
UBUNTU_CODENAME=cosmic
CakePHP2.xのインストール
簡単に言えばcomposer.json
を作成してcomposer install
すればローカルにcakephpがインストールされて使えるようになります。
以下はcomposer.jsonです。
{
"name": "otameshi",
"repositories": [
{
"type": "pear",
"url": "http://pear.cakephp.org"
}
],
"require": {
"cakephp/cakephp": ">=2.4.9"
},
"config": {
"vendor-dir": "Vendor/"
}
}
しかしこれでcomposer install
すると以下のエラーが出てしまいます。
$ composer install
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
PHP Fatal error: Uncaught Error: Call to undefined function Composer\Repository\Pear\simplexml_load_string() in phar:///usr/local/bin/composer/src/Composer/Repository/Pear/BaseChannelReader.php:74
ググったらxml周りのライブラリが足らないらしいのでインストールします。
$ sudo apt install php7.2-xml
これを入れたらcomposer install
が通りました。
$ composer install
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
PEAR repository from http://pear.cakephp.org could not be loaded. Your configuration does not allow connections to http://pear.cakephp.org/channel.xml. Seehttps://getcomposer.org/doc/06-config.md#secure-http for details.
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing cakephp/cakephp (2.10.14): Downloading (100%)
cakephp/cakephp suggests installing ext-mcrypt (You need to install ext-openssl or ext-mcrypt to use AES-256 encryption)
Writing lock file
Generating autoload files
$ la
total 20KT --
drwxrwxr-x 3 asmsuechan asmsuechan 4.0K 1月 17 10:04 ./
drwxr-xr-x 5 asmsuechan asmsuechan 4.0K 1月 16 14:11 ../
drwxrwxr-x 5 asmsuechan asmsuechan 4.0K 1月 17 10:04 Vendor/
-rw-rw-r-- 1 asmsuechan asmsuechan 265 1月 16 13:37 composer.json
-rw-rw-r-- 1 asmsuechan asmsuechan 2.3K 1月 17 10:04 composer.lock
bakeコマンド
./Vendor/bin/cake bake
を実行するとファイルをよしなに作成してくれます。
la myapp
total 64K
drwxr-xr-x 14 asmsuechan asmsuechan 4.0K 1月 17 10:26 ./
drwxrwxr-x 4 asmsuechan asmsuechan 4.0K 1月 17 10:26 ../
-rwxr-xr-x 1 asmsuechan asmsuechan 116 12月 20 10:45 .htaccess*
drwxr-xr-x 3 asmsuechan asmsuechan 4.0K 1月 17 10:27 Config/
drwxr-xr-x 4 asmsuechan asmsuechan 4.0K 1月 17 10:26 Console/
drwxr-xr-x 3 asmsuechan asmsuechan 4.0K 1月 17 10:26 Controller/
drwxr-xr-x 2 asmsuechan asmsuechan 4.0K 1月 17 10:26 Lib/
drwxr-xr-x 3 asmsuechan asmsuechan 4.0K 1月 17 10:26 Locale/
drwxr-xr-x 4 asmsuechan asmsuechan 4.0K 1月 17 10:26 Model/
drwxr-xr-x 2 asmsuechan asmsuechan 4.0K 1月 17 10:26 Plugin/
drwxr-xr-x 4 asmsuechan asmsuechan 4.0K 1月 17 10:26 Test/
drwxr-xr-x 2 asmsuechan asmsuechan 4.0K 1月 17 10:26 Vendor/
drwxr-xr-x 9 asmsuechan asmsuechan 4.0K 1月 17 10:26 View/
-rwxr-xr-x 1 asmsuechan asmsuechan 194 12月 20 10:45 index.php*
drwxrwxrwx 6 asmsuechan asmsuechan 4.0K 1月 17 10:26 tmp/
drwxr-xr-x 6 asmsuechan asmsuechan 4.0K 1月 17 10:26 webroot/
よくできました。
PHPUnitのインストール
./Console/cake test
を実行するとPHPUnitをインストールしろって言われます。ご丁寧にバージョン指定付きで。
Error: Please install PHPUnit framework v3.7 (http://www.phpunit.de)
# 0 /home/asmsuechan/src/asmsuechan/otameshi/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(218): TestShell->initialize()
# 1 /home/asmsuechan/src/asmsuechan/otameshi/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(66): ShellDispatcher->dispatch()
# 2 /home/asmsuechan/src/asmsuechan/otameshi/myapp/Console/cake.php(48): ShellDispatcher::run(Array)
# 3 {main}
composer require
コマンドでインストールします。
$ composer require --dev phpunit/phpunit ^3.7
Initializing PEAR repository http://pear.cakephp.org
PEAR repository from http://pear.cakephp.org could not be loaded. Your configuration does not allow connections to http://pear.cakephp.org/channel.xml. Seehttps://getcomposer.org/doc/06-config.md#secure-http for details.
./composer.json has been updated
Loading composer repositories with package information
Initializing PEAR repository http://pear.cakephp.org
PEAR repository from http://pear.cakephp.org could not be loaded. Your configuration does not allow connections to http://pear.cakephp.org/channel.xml. Seehttps://getcomposer.org/doc/06-config.md#secure-http for details.
Updating dependencies (including require-dev)
Package operations: 9 installs, 0 updates, 0 removals
- Installing symfony/polyfill-ctype (v1.10.0): Downloading (100%)
- Installing symfony/yaml (v2.8.49): Downloading (100%)
- Installing phpunit/php-text-template (1.2.1): Downloading (100%)
- Installing phpunit/phpunit-mock-objects (1.2.3): Downloading (100%)
- Installing phpunit/php-timer (1.0.9): Downloading (100%)
- Installing phpunit/php-file-iterator (1.4.5): Downloading (100%)
- Installing phpunit/php-token-stream (1.2.2): Downloading (100%)
- Installing phpunit/php-code-coverage (1.2.18): Downloading (100%)
- Installing phpunit/phpunit (3.7.38): Downloading (100%)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.0.5)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files
テストの実行
phpunitをインストールしたので実際に簡単なコンポーネントとそのテストを書いてみます。
// myapp/Controller/Component/CalculateComponent.php
<?php
class CalculateComponent {
function add ($x, $y) {
return $x + $y;
}
}
// myapp/Test/Case/Controller/Component/CalculateComponentTest.php
<?php
App::uses('CalculateComponent', 'Controller/Component');
class CalculateComponentTest extends CakeTestCase {
private $CalculateComponent = null;
public function setUp() {
$this->CalculateComponent = new CalculateComponent;
parent::setUp();
}
function testAdd() {
$result = $this->CalculateComponent->add(1,2);
$this->assertTrue($result == 3);
}
}
実際にテストを動かしてみます。
$ ./Console/cake test app
Welcome to CakePHP v2.10.14 Console
---------------------------------------------------------------
App : myapp
Path: /home/asmsuechan/src/asmsuechan/otameshi/myapp/
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
App Test Cases:
[1] AllTests
[2] Controller/Component/CalculateComponent
What test case would you like to run?
[q] > 2
PHPUnit 3.7.38 by Sebastian Bergmann.
The Xdebug extension is not loaded. No code coverage will be generated.
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from /home/asmsuechan/src/asmsuechan/otameshi/myapp/phpunit.xml
.
Time: 1 second, Memory: 6.00MB
OK (1 test, 1 assertion)
サーバーの起動
cake server
でサーバーが立ち上がります。
$ sudo ./Console/cake server
[sudo] password for asmsuechan:
Welcome to CakePHP v2.10.14 Console
---------------------------------------------------------------
App : myapp
Path: /home/asmsuechan/src/asmsuechan/otameshi/myapp/
DocumentRoot: /home/asmsuechan/src/asmsuechan/otameshi/myapp/webroot
---------------------------------------------------------------
built-in server is running in http://localhost/
[Thu Jan 17 11:36:26 2019] ::1:46716 [200]: /css/cake.generic.css
[Thu Jan 17 11:36:26 2019] ::1:46718 [200]: /img/cake.power.gif
[Thu Jan 17 11:36:26 2019] ::1:46720 [200]: /img/cake.icon.png
localohostにアクセスするとこんな画面が見えます。
まとめ
古い環境を構築するの大変。Dockerでサクッと出来るパワーが必要。