composerでサンプルライブラリの作成
便利とは思いつつ環境で抵抗がある
なんか便利とは思いつつうまく使えていないので調査
まず準備
ディレクトリを作成してcomposerをダウンロード
[root@localhost ~]# mkdir HelloWorld [root@localhost ~]# cd HelloWorld/ [root@localhost HelloWorld]# curl -sS https://getcomposer.org/installer | php #!/usr/bin/env php All settings correct for using Composer Downloading... Composer successfully installed to: /root/HelloWorld/composer.phar Use it: php composer.phar
初期化
[root@localhost HelloWorld]# php composer.phar init Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [root/hello-world]: mshige1979/helloworld Description []: sample Author [m_shige1979 <m_shige1979@r2.dion.ne.jp>]: shigeharu matsumoto <m_shige1979@r2.dion.ne.jp> Minimum Stability []: dev License []: MIT Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to define your dev dependencies (require-dev) interactively [yes]? no { "name": "mshige1979/helloworld", "description": "sample", "license": "MIT", "authors": [ { "name": "shigeharu matsumoto", "email": "m_shige1979@r2.dion.ne.jp" } ], "minimum-stability": "dev", "require": { } } Do you confirm generation [yes]? [root@localhost HelloWorld]#
composer.jsonが作成されていることを確認
[root@localhost HelloWorld]# ll 合計 988 -rw-r--r-- 1 root root 284 3月 6 08:07 2014 composer.json -rwxr-xr-x 1 root root 1004313 3月 6 07:56 2014 composer.phar [root@localhost HelloWorld]#
ライブラリを作成
ディレクトリを作成(src/名前空間名/ライブラリ名で作成)
[root@localhost HelloWorld]# mkdir -p src/Mshige1979/HelloWorld [root@localhost HelloWorld]# tree src src + Mshige1979 + HelloWorld 2 directories, 0 files [root@localhost HelloWorld]#
src/Mshige1979/HelloWorld/Say.php(クラス名と同じ名前のクラスファイルで作成)
<?php // 名前空間を定義 namespace Mshige1979\HelloWorld; // クラスを定義 class Say{ // テストメッセージ public static function hello(){ return "hello"; } // 文字列数を返却 public static function len($str){ return 10; } }
テスト
テスト用ディレクトリを作成
[root@localhost HelloWorld]# mkdir -p tests [root@localhost HelloWorld]#
composer.jsonのファイルを修正(phpunitなどを指定)
{ "name": "mshige1979/helloworld", "description": "sample", "license": "MIT", "authors": [ { "name": "shigeharu matsumoto", "email": "m_shige1979@r2.dion.ne.jp" } ], "minimum-stability": "dev", "require": { }, "require-dev": { "phpunit/phpunit": "3.7.*" }, "autoload": { "psr-0": { "Mshige1979\\HelloWorld": "src/" } } }
composerの開発版の部分をインストールする
[root@localhost HelloWorld]# php composer.phar install --dev Loading composer repositories with package information Installing dependencies (including require-dev) - Installing phpunit/php-timer (1.0.5) Downloading: 100% - Installing phpunit/php-text-template (1.1.4) Downloading: 100% - Installing phpunit/php-file-iterator (1.3.4) Downloading: 100% - Installing symfony/yaml (dev-master 6194137) Cloning 6194137af5f12fbcfc0cb21e269f47542b8beb47 - Installing phpunit/phpunit-mock-objects (1.2.x-dev c39c451) Cloning c39c4511c3b007539eb170c32cbc2af49a07351a - Installing phpunit/php-token-stream (dev-master ad4e1e2) Cloning ad4e1e23ae01b483c16f600ff1bebec184588e32 - Installing phpunit/php-code-coverage (1.2.13) Downloading: 100% - Installing phpunit/phpunit (3.7.x-dev 4dbc3ab) Cloning 4dbc3ab5de25a9ffd07796519f9f7f4aaddc37e0 phpunit/phpunit suggests installing phpunit/php-invoker (~1.1) Writing lock file Generating autoload files [root@localhost HelloWorld]#
テスト用のディレクトリを作成
[root@localhost HelloWorld]# mkdir -p tests/Mshige1979/HelloWorld [root@localhost HelloWorld]#
テスト用の設定ファイルを作成する(tests/phpunit.xml)
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="../vendor/autoload.php" cacheTokens="true" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" verbose="false"> <filter> <whitelist addUncoveredFilesFromWhitelist="false"> <directory>../src/Mshige1979/</directory> </whitelist> </filter> <testsuite name="Mshige1979\HelloWorld Test Suite"> <directory>.</directory> </testsuite> </phpunit>
テストコード(tests/Mshige1979/HelloWorld/SayTest.php)
<?php // 名前空間定義 namespace Mshige1979\HelloWorld\Tests; // ライブラリを指定 use Mshige1979\HelloWorld\Say; // テスト用クラス class SayTest extends \PHPUnit_Framework_TestCase{ // ================================================== // 機能毎にテストメソッドを記載 // ================================================== // helloメソッドのテスト public function testHello(){ $data = Say::hello(); $this->assertEquals('hello', $data); } // lenメソッドのテスト public function testLen(){ $data = Say::len("1234567890"); $this->assertEquals(10, $data); } }
テスト実行
[root@localhost HelloWorld]# cd tests [root@localhost tests]# ../vendor/bin/phpunit PHPUnit 3.7.32-3-g4dbc3ab by Sebastian Bergmann. Configuration read from /root/HelloWorld/tests/phpunit.xml .. Time: 224 ms, Memory: 3.00Mb OK (2 tests, 2 assertions) [root@localhost tests]#
git
git初期化
[root@localhost HelloWorld]# git init Initialized empty Git repository in /root/HelloWorld/.git/ [root@localhost HelloWorld]#
.gitignore(管理対象外のファイルを設定)
vendor/ composer.lock composer.phar
追加して確認
[root@localhost HelloWorld]# git add . [root@localhost HelloWorld]# git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: .gitignore new file: composer.json new file: src/Mshige1979/HelloWorld/Say.php new file: tests/Mshige1979/HelloWorld/SayTest.php new file: tests/phpunit.xml [root@localhost HelloWorld]#
コミット
[root@localhost HelloWorld]# git commit -m "initial commit" [master (root-commit) 47e9edd] initial commit 5 files changed, 92 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Mshige1979/HelloWorld/Say.php create mode 100644 tests/Mshige1979/HelloWorld/SayTest.php create mode 100644 tests/phpunit.xml [root@localhost HelloWorld]#
githubで公開
リモート設定
[root@localhost HelloWorld]# git remote add origin git@github.com:mshige1979/HelloWorld.git [root@localhost HelloWorld]# git fetch [root@localhost HelloWorld]# git pull git@github.com:mshige1979/HelloWorld.git
プッシュ
[root@localhost HelloWorld]# git push origin master
一部修正してタグ付け
composer.json修正
{ "name": "mshige1979/helloworld", "description": "sample", "license": "MIT", "type": "library", "homepage": "https://github.com/mshige1979/HelloWorld", "authors": [ { "name": "shigeharu matsumoto", "email": "m_shige1979@r2.dion.ne.jp" } ], "minimum-stability": "dev", "require": { }, "require-dev": { "phpunit/phpunit": "3.7.*" }, "autoload": { "psr-0": { "Mshige1979\\HelloWorld": "src/" } } }
ライセンス
よしなに作成してくれ…
コミット
[root@localhost HelloWorld]# git commit -m "settings update" [root@localhost HelloWorld]#
タグ付けしてプッシュ
[root@localhost HelloWorld]# git tag 1.0.0 [root@localhost HelloWorld]# git push origin master [root@localhost HelloWorld]# git push origin 1.0.0
タグ作成
まとめ
やることがいろいろあるので慣れるまで時間がかかりそう。
Packagistに公開するのは後でやってみる。
テストのやり方もこれで慣らしていく。テスト方法の勉強にもちょうど良さそう。