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 がないですね..ファイルがないのでエラーになってたようです
追加して解決しました

phpで引数を取ってみる

phpファイルを実行する際に引数が渡せます

$ php -f sample.php arg1 arg2

と言った形で、arg1、arg2が引数に渡ります
phpで取得するには

<?php

if ($argc > 1) {
    $one = $argv[1];
}
if ($argc > 2) {
    $two = $argv[2];
}

といった形で取れます
$argcに引数の数、$argvに引数が入ります
$argv[0]には実行ファイルのフルパスが入ってました

Cakephp 2.7でFlash->errorが呼ばれたかテストを書く

Cakephp 2.7のコントローラーのテストで、Flashコンポーネントへエラーメッセージがセットされるか確認するとき

$this->controller->Flash->expects($this->once())
    ->method('error')->with($this->equalTo('error occured'));

とするとエラーになります

Expectation failed for method name is equal to <string:error> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

呼ばれてないとかって言われます
これは、FlashComponentを見ると分かりますが、errorメソッドはなく、__callを使って呼び出されているからでした

PHP: オーバーロード __call – Manual

errorメソッドは用意されてなく、用意されてないメソッドが呼ばれた時には、__callが呼ばれる仕組みを利用したものでした
いい案が思いつかなかったのですが、テストをクリアするために

$this->controller->Flash->expects($this->once())
    ->method('__call')->with($this->equalTo('error'));

としてクリアさせました
__callが呼ばれて、errorが引数に渡るので、これでグリーンになりました
メッセージがテスト出来てませんが、まずはこれでいいかと思います