macでphpunitが動かない

macでphpunitをインストールして実行すると、こんなエラーが出た。

PHP Warning:  require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in /usr/lib/php/PHPUnit/Autoload.php on line 45
PHP Fatal error:  require_once(): Failed opening required 'File/Iterator/Autoload.php' (include_path='.:') in /usr/lib/php/PHPUnit/Autoload.php on line 45

/private/etc/php.iniに、次の一文を入れることで解決

include_path = ".:/php/includes:/usr/lib/php"

 

mac + mysql + Symfony2 + doctrineでエラー

mac + mysql + Symfony2でdoctrine関係のコマンドを実行すると、次のエラーが出ることがあります。

Could not create database for connection named `test_project`
SQLSTATE[HY000] [2002] No such file or directory

これは、mysql起動時のsocket情報を保存するファイルが通常とは違うところに出力されるのが原因です。解決するためには、app/config/config.ymlに"unix_socket"の設定を追加します。
 
doctrine:
  dbal:
       driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        unix_socket: /tmp/mysql.sock
 
unix_socketの値は、mysqlにログインして"status"コマンドで確認できます。
 
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.5.27, for osx10.6 (i386) using readline 5.1
 
Connection id:6
Current database:
Current user:root@localhost
SSL:Not in use
Current pager:stdout
Using outfile:''
Using delimiter:;
Server version:5.5.27-log MySQL Community Server (GPL)
Protocol version:10
Connection:Localhost via UNIX socket
Server characterset:latin1
Db     characterset:latin1
Client characterset:utf8
Conn.  characterset:utf8
UNIX socket:/tmp/mysql.sock
Uptime:10 hours 57 min 18 sec
 
Threads: 1  Questions: 24  Slow queries: 0  Opens: 34  Flush tables: 1  Open tables: 27  Queries per second avg: 0.000
--------------
 

mac + Symfony2でapp/cacheとapp/logsに権限を追加する

Symfony2のマニュアルに沿ってインストールを行っている時に"app/cache"と"app/logs"に書き込み権限を与える方法で、マニュアルに書かれている以下の文章を入力するとエラーが発生することがある。

>sudo chmod +a "www-data allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
chmod: Unable to translate 'www-data' to a UID/GID

これは、"www-data"というのはapache2を起動しているユーザーなのだが、これがmacだと異なるからだ。このユーザー名は次のコマンドで知ることができる。

>ps -ef | grep httpd
  70   512    40   0   0:00.51 ??         0:02.80 /usr/sbin/httpd -D FOREGROUND
>grep 70 /etc/passwd
_www:*:70:70:World Wide Web Server:/Library/WebServer:/usr/bin/false 

この場合、"_www"がユーザー名となる。先の分のwww-dataをこれで置き換えればOKだ。

>sudo chmod +a "_www allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs

macでphp.iniの編集

macでphp.iniを編集する。

>php -i | grep ini

として表示される中に、次の文字を探す

Loaded Configuration File => /private/etc/php.ini

これがmacで使われるphp.ini。
管理者権限がいるので、sudoコマンドを使って編集する。
編集が終わったら、apacheを再起動しないと編集結果が反映されないので注意すること。

sudo /usr/sbin/apachectl restart 

設計書なき開発の進め方 #5

※タイトルをちょっと短くしました。

しばらくモデリングについて書いてみようと思う。

ECサイトでは、ビジネスは次のように進むことが一般的だ。

  1. 利用者が注文をする
  2. 利用者が入金をする(クレジット決済など)
  3. サービス提供者が出荷をする

これをモデル化すると、次のようなモデルが考えられる。

f:id:nakaken0629:20120918003421j:plain

どちらをとるか? ここでは入金と出荷が並列に注文と関係するモデルを取りたい。なぜかと言えば、今回のケースでは確かに入金をしないと出荷ができないのでもう一方のほうが理にかなっているかもしれない。しかし一般的には出荷は必ずしも入金を必要としない。入金と出荷が前後する可能性も無いとはいえない。しかし注文が無いのに一方的な出荷はあり得ない。なので後々のことを考えれば注文を中心に捉え、入金や出荷を並列に注文に属する情報として捉える。入金と出荷の関係はUML上では注文の制約としてコメントで記載する。実装では注文クラスで定義するメソッドで実現することになるだろう。

設計書なき開発の進め方(Mac, MySQL, Symfony) #5

 

まずはsymfonymysqlを使うようにするための設定。

./symfony configure:database "mysql:host=localhost;dbname=virtuosoec"

 

これで、config/databases.ymlファイルが以下のように編集される。

このファイルはおそらく、手動でメンテするのではなく上記コマンドで変更するものと思われます。

[config/databases.yml]
all:
 doctrine:
   class: sfDoctrineDatabase
   param:
     dsn: 'mysql:host=localhost;dbname=virtuosoec'
    username: root
     password: null

 

次からsymfonyコマンドでソースコードを自動生成していきます。そのときソースコードには、チームや部署の作業者の名前が入ります。propertiesiniを使って名前を入れます

config/properties.ini

 

スキーマの設定。このファイルを元にSQLやモデルファイルが作成されます。とりあえずは必要最低限、仕様に出てきた項目を用意します。

[config/doctrine/schema.yml]

Item:
 columns:
   id: { type: integer, notnull: true, primary: true, autoincrement: true }
   name: { type: string(255), notnull: true }
   description: { type: string, notnull: true }
   unit_price: { type: decimal, notnull: true }

 

./symfony doctrine:build-mode./symfony doctrine:build-sql

./symfony doctrine:insert-sql

 

nakagaki-kenji-no-macbook-pro:virtuosoec kenji$ ./symfony doctrine:insert-sql>> doctrine  creating tables

PHP Warning:  PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/kenji/virtuosoec/lib/vendor/symfony-1.4.18/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php on line 470

 

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/kenji/virtuosoec/lib/vendor/symfony-1.4.18/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php on line 470

 

...実際にデータベースに接続しようとした時に、この警告が発生します。調べてみると、"UNIX Socket"との値が警告メッセージの中の値と異なっています。

 

nakagaki-kenji-no-macbook-pro:virtuosoec kenji$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.5.27 MySQL Community Server (GPL)

 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> status

--------------

mysql  Ver 14.14 Distrib 5.5.27, for osx10.6 (i386) using readline 5.1

 

Connection id:7

Current database:

Current user:kenji@localhost

SSL:Not in use

Current pager:stdout

Using outfile:''

Using delimiter:;

Server version:5.5.27 MySQL Community Server (GPL)

Protocol version:10

Connection:Localhost via UNIX socket

Server characterset:latin1

Db     characterset:latin1

Client characterset:utf8

Conn.  characterset:utf8

UNIX socket:/tmp/mysql.sock

Uptime:31 min 1 sec

 

Threads: 1  Questions: 36  Slow queries: 0  Opens: 35  Flush tables: 1  Open tables: 27  Queries per second avg: 0.019

--------------

 

mysql

 

設定ファイルに合うように、configure:databaseコマンドを修正してもう一度作成します。

./symfony configure:database "mysql:host=localhost;dbname=virtuosoec;unix_socket=/tmp/mysql.sock"

 

テストデータを用意します。

[data/fixtures/fixtures.xml]

Item:

  B001DCQI82:

    name: Beethoven: The Symphonies [Box Set, CD, Import, From UK]

    description: Herbert von Karajan

    unit_price: 2468

  B004HGQXB8:

    name: Complete Beethoven Edition [Box Set, CD, Import, From US]

    description: 交響曲は、他にも安いのがいくらでもあるので...(以下省略)

    unit_price: 8480

    

./symfony doctrine:data-load

 

 

今日はここまで

 

設計書なき開発の進め方(Mac, MySQL, Symfony) #4

[TODO]

symfonyコマンドでソースを自動生成すると、コメント欄に"author: Your name here"と表示される。これはconfig/properties.iniを書き換えることで対応できるのだが、このファイルは一般的にはソース管理対象下のファイルとして扱われるため、個人個人でファイルを書き換えることができない。どうすればいいか?