23
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

Organization

composerを使ってみた時のメモ

pearの後継版のパッケージ管理の機能とかなんとかどっかに書いてあった。

使ってみた感想は便利!!

環境

  • centos6.3(ゲストOS)
  • macosx10.8(ホストOS)

事前準備に必要なもの

  • php5.3以上

参考にしたサイト

composerをインストールする

サクッとワンライナー

curl -s https://getcomposer.org/installer | php && mv -v composer.phar /usr/local/bin/composer

自分は/usr/local/binに置きました

動作確認

$composer -help


   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 98ada572ec52875dfa70e0cb9851dd5f8a34f006

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v Increase verbosity of messages.
  --version        -V Display this application version.
  --ansi              Force ANSI output.
  --no-ansi           Disable ANSI output.
  --no-interaction -n Do not ask any interactive question.
  --profile           Display timing and memory usage information
  --working-dir    -d If specified, use the given directory as working directory.

Available commands:
  about            Short information about Composer
  config           Set config options
  create-project   Create new project from a package into given directory.
  depends          Shows which packages depend on the given package
  dump-autoload    Dumps the autoloader
  dumpautoload     Dumps the autoloader
  help             Displays help for a command
  init             Creates a basic composer.json file in current directory.
  install          Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  list             Lists commands
  require          Adds required packages to your composer.json and installs them
  search           Search for packages
  self-update      Updates composer.phar to the latest version.
  selfupdate       Updates composer.phar to the latest version.
  show             Show information about packages
  status           Show a list of locally modified packages
  update           Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate         Validates a composer.json

composerの設定ファイルを書く

ディレクトリ構成について

composerコマンドはcomposerコマンド実行時のカレントの設定ファイルを読みに行くのがデフォルトです。
設定ファイルもバージョン管理化に置きたいのでファイル構成はこんな感じで

├── composer.json
├── composer.lock
├── phpunit.bootstrap.php
├── phpunit.xml.dist
├── tests #テスト本体
│   └── ExampleTest.php
├── vendor #composerのインストールしたパッケージの置き場
└── www #ドキュメントルート
    ├── index.html
    └── phpinfo.php

設定ファイルを新規で追加

phpunit本家からcomposerを対応しているモジュール郡をグバっと入れてます。

composer.json
{
    "require": {
        "phpunit/phpunit": "3.7.*",
        "mockery/mockery": "0.7.*",
        "phpunit/dbunit": ">=1.2",
        "phpunit/phpunit-selenium": ">=1.2",
        "phpunit/phpunit-story": "*"
    },
    "config": {
        "bin-dir": "/usr/local/bin/"
    }
}

composer install

シンプル

composer install

phpunitの設定を書く

ComposerでPHPUnit一式+Mockeryをインストールを真似ました。

phpunit.xml.dist

phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    bootstrap="phpunit.bootstrap.php"
    processIsolation="false"
    verbose="true"
    strict="false"
    colors="true">
    <testsuites>
        <testsuite name="PHPUnit">
            <!-- テストケースがあるディレクトリを列挙する -->
            <directory>tests</directory>
            <!-- <directory>MyProject2</directory> -->
            <!-- <directory>MyProject3</directory> -->
            <!-- <directory>MyProject4</directory> -->
        </testsuite>
    </testsuites>

    <logging>
        <!-- コードカバレッジ率 -->
        <log
            type="coverage-html"
            target="metrics/coverage"
            charset="UTF-8"
            yui="true"
            highlight="false"
            lowUpperBound="35"
            highLowerBound="70" />
        <!-- <log type="coverage-text" target="php://stdout" lowUpperBound="35" highLowerBound="70" /> -->
        <!-- 上行をアンコメントアウトするとカバレッジ率がプロンプトにも表示される -->
    </logging>

    <filter>
        <!-- コードカバレッジ率を計測する対象の指定 -->
        <whitelist>
            <directory suffix=".php">src</directory>
            <!-- <file>/path/to/file</file> -->
            <exclude>
                <file>public/index.php</file>
            </exclude>
        </whitelist>
        <!-- コードカバレッジ測定対象から除外 -->
        <blacklist>
            <directory suffix=".php" group="PHPUNIT">vendor</directory>
        </blacklist>
    </filter>
    <listeners>
        <!-- mockery統合のため必要 -->
        <listener class="\Mockery\Adapter\Phpunit\TestListener" file="Vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php" />
    </listeners>
</phpunit>

phpunit.bootstrap.php

phpunit.bootstrap.php
<?php

// For composer
require_once 'vendor/autoload.php';

tests/ExampleTest.php

tests/ExampleTest.php
<?php

class ExampleTest extends PHPUnit_Framework_TestCase
{
        public function testSomething()
        {
                $this->assertTrue(true);
        }
}

phpunit実行

実行

$phpunit


PHPUnit 3.7.14 by Sebastian Bergmann.

Configuration read from /home/web/lo.test.org/phpunit.xml.dist

.

Time: 9 seconds, Memory: 5.25Mb

OK (1 test, 1 assertion)

以上。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
23
Help us understand the problem. What are the problem?