Xdebug impact on Composerの対策をしてみた

composerを起動するたびに
Xdebug impact on Composer
の警告が出て、実行が遅いので対策してみました

環境は
Ubuntu 14.04
php 7.0

composer実行時にxdebugを読み込まないようにするため、.bashrcに書込み

$ echo ‘function composer() { COMPOSER=”$(which composer)” || { echo “Could not find composer in path” >&2 ; return 1 ; } && sudo phpdismod -s cli xdebug ; $COMPOSER “$@” ; STATUS=$? ; sudo phpenmod -s cli xdebug ; return $STATUS ; }’ >> ~/.bashrc
. ~/.bashrc

参考: Troubleshooting - Composer

参考サイトをちょっと環境に合わせて変えてます
これで実行が早くなりました

cakephp 3を試してみる

php 7入のUbuntuで、cakephp 3を試してみました
Quick Start Guide

php のバージョンをチェック

$ php -v
PHP 7.0.7-4+deb.sury.org~trusty+1 (cli) ( NTS )

さっそくいれてみる

$ composer create-project –prefer-dist cakephp/app bookmarker

エラー..

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - cakephp/cakephp 3.2.9 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.8 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.7 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.6 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.5 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.4 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.3 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.11 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.10 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - cakephp/cakephp 3.2.0 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - Installation request for cakephp/cakephp ~3.2 -> satisfiable by cakephp/cakephp[3.2.0, 3.2.1, 3.2.10, 3.2.11, 3.2.2, 3.2.3, 3.2.4, 3.2.5, 3.2.6, 3.2.7, 3.2.8, 3.2.9].

  To enable extensions, verify that they are enabled in those .ini files:
    - /etc/php/7.0/cli/php.ini
    - /etc/php/7.0/cli/conf.d/10-opcache.ini
    - /etc/php/7.0/cli/conf.d/10-pdo.ini
    - /etc/php/7.0/cli/conf.d/20-apcu.ini
    - /etc/php/7.0/cli/conf.d/20-apcu_bc.ini
    - /etc/php/7.0/cli/conf.d/20-calendar.ini
    - /etc/php/7.0/cli/conf.d/20-ctype.ini
    - /etc/php/7.0/cli/conf.d/20-exif.ini
    - /etc/php/7.0/cli/conf.d/20-fileinfo.ini
    - /etc/php/7.0/cli/conf.d/20-ftp.ini
    - /etc/php/7.0/cli/conf.d/20-gettext.ini
    - /etc/php/7.0/cli/conf.d/20-iconv.ini
    - /etc/php/7.0/cli/conf.d/20-json.ini
    - /etc/php/7.0/cli/conf.d/20-newrelic.ini
    - /etc/php/7.0/cli/conf.d/20-phar.ini
    - /etc/php/7.0/cli/conf.d/20-posix.ini
    - /etc/php/7.0/cli/conf.d/20-readline.ini
    - /etc/php/7.0/cli/conf.d/20-shmop.ini
    - /etc/php/7.0/cli/conf.d/20-sockets.ini
    - /etc/php/7.0/cli/conf.d/20-sysvmsg.ini
    - /etc/php/7.0/cli/conf.d/20-sysvsem.ini
    - /etc/php/7.0/cli/conf.d/20-sysvshm.ini
    - /etc/php/7.0/cli/conf.d/20-tokenizer.ini
    - /etc/php/7.0/cli/conf.d/20-xdebug.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

PHP extension intl がないようなので入れます

$ sudo apt-get update
$ sudo apt-get php7.0-intl

念の為もう一度いれておきます

$ rm -rf bookmarker/
$ composer create-project –prefer-dist cakephp/app bookmarker

最後のほうのログがこんな感じ

cakephp/app suggests installing phpunit/phpunit (Allows automated tests to be run without system-wide install.)
cakephp/app suggests installing cakephp/cakephp-codesniffer (Allows to check the code against the coding standards used in CakePHP.)
symfony/console suggests installing symfony/event-dispatcher ()
symfony/console suggests installing symfony/process ()
symfony/var-dumper suggests installing ext-symfony_debug ()
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
cakephp/debug_kit suggests installing ext-sqlite (DebugKit needs to store panel data in a database. SQLite is simple and easy to use.)
Writing lock file
Generating autoload files
> Cake\Composer\Installer\PluginInstaller::postAutoloadDump
> App\Console\Installer::postInstall
Created `config/app.php` file
Set Folder Permissions ? (Default to Y) [Y,n]? Y
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp/cache
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp/cache/models
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp/cache/persistent
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp/cache/views
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp/sessions
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp/tests
Permissions set on /home/user/ws/cakephp3/bookmarker/tmp
Permissions set on /home/user/ws/cakephp3/bookmarker/logs
Updated Security.salt value in config/app.php

これでインストールできました
試しに起動

$ bin/cake server -H sample.com -p port

sample.com と port はご自由に
指定しない場合
localhost:8765で立ち上がるようです

起動した画面
https://gyazo.com/86a9ea1a19572c0b82068cd6720dad74

mysqlとつなぎます

ログイン

$ mysql -u user -ppassword

userとpasswordはご自身のもので

データベース作成
mysql> create database cake_bookmarks character set utf8;

データベース指定して
mysql> connect cake_bookmarks;

Quick Start Guide
のsqlを流します

テーブルまでできあがり

cakephpにデータベースの設定を書きます

$ vim config/app.php

で Datasources のところの設定を書き換えます
これでつながりました

ここまででベースができあがり

Nginxでphpを使うと403エラー

Nginx + php_fpm環境を設定してて問題なかったのに

http://(サーバードメイン)/sample
にアクセスするとエラーが..

2016/05/14 16:37:55 [error] 7942#0: *621 directory index of "/usr/share/nginx/html/sample/" is forbidden

http://(サーバードメイン)/sample/index.php

だと問題ない..ちょっと考えたらわかるのにはまりました..
nginxの設定ファイルを確認

server {                                                                           
    listen       80;                                                                                                                                                                                         
    server_name  sample.co.jp;                                       
    root   /usr/share/nginx/html;                                               

    access_log  /var/log/nginx/base-access.log  main;                           
    error_log  /var/log/nginx/base-error.log warn;                              

    location / {                                                                
        index  index.html index.htm;                                            
    }                                                                           

    #error_page  404              /404.html;                                    

    # redirect server error pages to the static page /50x.html                  
    #                                                                           
    error_page   500 502 503 504  /50x.html;                                    
    location = /50x.html {                                                      
        root   /usr/share/nginx/html;                                           
    }                                                                           

    # pass the PHP scripts to FastCGI server                                    
    #                                                                           
    location ~ \.php$ {                                                         
        fastcgi_pass   unix:/var/run/php5-fpm.sock;                             
        fastcgi_index  index.php;                                               
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        
        include        fastcgi_params;                                          
    }                                                                           
} 

location /
の下に index.php がないですね..ファイルがないのでエラーになってたようです
追加して解決しました

phpunitでprivateメソッドのテストをする

phpunitでclassのprivate methodをテストしたいときはPHP: ReflectionMethod – Manualを使うとできます

SampleクラスのisValidメソッドへ引数を渡してテストするなら

$class = new Sample();
$arr = ['arg' => 100];
$method = new ReflectionMethod(get_class($class), 'isValid');
$method->setAccessible(true);
$result = $method->invoke($this->csv, $arr);
$this->assertTrue($result);

といった形です