ComposerとPHPUnitのインストール

PHP使うならComposer使うといいとよく見るので導入してみました。

このエントリーはComposerのインストールとComposerでPHPUnitの導入までのメモです

composerのインストール

公式のインストール方法 https://getcomposer.org/download/

インストールをもっと簡単に

[vagrant@localhost html]$curl -sS https://getcomposer.org/installer | php

パスにあるディレクトリに移動させる

[vagrant@localhost html]$mv composer.phar /usr/local/bin/composer

composerのアップデート

[vagrant@localhost html]$composer self-update

composerのインストールはこれで完了

composer.jsonの作成

とりあえずPHPUnitを指定したのをつくる

{
    "require-dev": {
        "phpunit/phpunit": "5.5.*"
    }
}

composer installでパッケージのインストール

[vagrant@localhost html]$composer install

エラー発生

[vagrant@localhost html]$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpunit/phpunit 5.5.7 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.6 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.5 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.4 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.3 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.2 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.1 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 5.5.0 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - Installation request for phpunit/phpunit 5.5.* -> satisfiable by phpunit/phpunit[5.5.0, 5.5.1, 5.5.2, 5.5.3, 5.5.4, 5.5.5, 5.5.6, 5.5.7].

  To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php.ini
    - /etc/php.d/20-bz2.ini
    - /etc/php.d/20-calendar.ini
    - /etc/php.d/20-ctype.ini
    - /etc/php.d/20-curl.ini
    - /etc/php.d/20-exif.ini
    - /etc/php.d/20-fileinfo.ini
    - /etc/php.d/20-ftp.ini
    - /etc/php.d/20-gettext.ini
    - /etc/php.d/20-iconv.ini
    - /etc/php.d/20-json.ini
    - /etc/php.d/20-phar.ini
    - /etc/php.d/20-sockets.ini
    - /etc/php.d/20-tokenizer.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

DOM Extensionがないようなのでインストールするも失敗する

[vagrant@localhost html]$sudo yum -y install php-xml

Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                                                                                         | 3.6 kB  00:00:00
epel/x86_64/metalink                                                                                                                         | 5.1 kB  00:00:00
epel                                                                                                                                         | 4.3 kB  00:00:00
extras                                                                                                                                       | 3.4 kB  00:00:00
remi-safe                                                                                                                                    | 2.9 kB  00:00:00
updates                                                                                                                                      | 3.4 kB  00:00:00
(1/5): epel/x86_64/updateinfo                                                                                                                | 748 kB  00:00:00
(2/5): extras/7/x86_64/primary_db                                                                                                            | 122 kB  00:00:00
(3/5): updates/7/x86_64/primary_db                                                                                                           | 2.9 MB  00:00:00
(4/5): remi-safe/primary_db                                                                                                                  | 908 kB  00:00:01
(5/5): epel/x86_64/primary_db                                                                                                                | 4.6 MB  00:00:05
Determining fastest mirrors
 * base: ftp.iij.ad.jp
 * epel: ftp.jaist.ac.jp
 * extras: ftp.iij.ad.jp
 * remi-safe: mirror.smartmedia.net.id
 * updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package php-xml.x86_64 0:5.4.16-42.el7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-42.el7 for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.4.16-42.el7.x86_64
--> Running transaction check
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
---> Package php-xml.x86_64 0:5.4.16-42.el7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-42.el7 for package: php-xml-5.4.16-42.el7.x86_64
--> Finished Dependency Resolution
Error: Package: php-xml-5.4.16-42.el7.x86_64 (base)
           Requires: php-common(x86-64) = 5.4.16-42.el7
           Installed: php-common-7.0.15-1.el7.remi.x86_64 (@remi-php70)
               php-common(x86-64) = 7.0.15-1.el7.remi
           Available: php-common-5.4.16-42.el7.x86_64 (base)
               php-common(x86-64) = 5.4.16-42.el7
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

remiでphpをインストールしてたのでremi-php70を指定した方法で再度インストール

[vagrant@localhost html]$sudo yum -y install --enablerepo=remi-php70 php-xml
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.jaist.ac.jp
 * extras: ftp.iij.ad.jp
 * remi-php70: mirror.smartmedia.net.id
 * remi-safe: mirror.smartmedia.net.id
 * updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package php-xml.x86_64 0:7.0.16-1.el7.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================
 Package                             Arch                               Version                                        Repository                              Size
====================================================================================================================================================================
Installing:
 php-xml                             x86_64                             7.0.16-1.el7.remi                              remi-php70                             204 k

Transaction Summary
====================================================================================================================================================================
Install  1 Package

Total download size: 204 k
Installed size: 854 k
Downloading packages:
php-xml-7.0.16-1.el7.remi.x86_64.rpm                                                                                                         | 204 kB  00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : php-xml-7.0.16-1.el7.remi.x86_64                                                                                                                 1/1
  Verifying  : php-xml-7.0.16-1.el7.remi.x86_64                                                                                                                 1/1

Installed:
  php-xml.x86_64 0:7.0.16-1.el7.remi

Complete!

入った

念のため確認

[vagrant@localhost html]$ yum list installed | grep php
php.x86_64                       7.0.16-1.el7.remi              @remi-php70
php-cli.x86_64                   7.0.16-1.el7.remi              @remi-php70
php-common.x86_64                7.0.16-1.el7.remi              @remi-php70
php-json.x86_64                  7.0.16-1.el7.remi              @remi-php70
php-xml.x86_64                   7.0.16-1.el7.remi              @remi-php70

再度composer installするもエラー

[vagrant@localhost ~]$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 25 installs, 0 updates, 0 removals
    Failed to download symfony/yaml from dist: The zip extension and unzip command are both missing, skipping.
The php.ini used by your command-line PHP is: /etc/php.ini
    Now trying to download from source
  - Installing symfony/yaml (v3.2.4) Cloning 9724c68464


  [RuntimeException]
  Failed to clone https://github.com/symfony/yaml.git, git was not found, check that it is installed and in your PATH env.
  sh: git: command not found


install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--] [<packages>]...

zipとunzipがないのかな

[vagrant@localhost html]$ sudo yum -y install zip unzip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.jaist.ac.jp
 * extras: ftp.iij.ad.jp
 * remi-safe: mirror.smartmedia.net.id
 * updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package unzip.x86_64 0:6.0-16.el7 will be installed
---> Package zip.x86_64 0:3.0-11.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================
 Package                               Arch                                   Version                                    Repository                            Size
====================================================================================================================================================================
Installing:
 unzip                                 x86_64                                 6.0-16.el7                                 base                                 169 k
 zip                                   x86_64                                 3.0-11.el7                                 base                                 260 k

Transaction Summary
====================================================================================================================================================================
Install  2 Packages

Total download size: 429 k
Installed size: 1.1 M
Downloading packages:
(1/2): unzip-6.0-16.el7.x86_64.rpm                                                                                                           | 169 kB  00:00:00
(2/2): zip-3.0-11.el7.x86_64.rpm                                                                                                             | 260 kB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                               2.8 MB/s | 429 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : unzip-6.0-16.el7.x86_64                                                                                                                          1/2
  Installing : zip-3.0-11.el7.x86_64                                                                                                                            2/2
  Verifying  : zip-3.0-11.el7.x86_64                                                                                                                            1/2
  Verifying  : unzip-6.0-16.el7.x86_64                                                                                                                          2/2

Installed:
  unzip.x86_64 0:6.0-16.el7                                                         zip.x86_64 0:3.0-11.el7

Complete!

composer installがやっとできた

[vagrant@localhost html]$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 25 installs, 0 updates, 0 removals
  - Installing symfony/yaml (v3.2.4) Downloading: 100%
  - Installing sebastian/version (2.0.1) Downloading: 100%
  - Installing sebastian/resource-operations (1.0.0) Downloading: 100%
  - Installing sebastian/recursion-context (1.0.2) Downloading: 100%
  - Installing sebastian/object-enumerator (1.0.0) Downloading: 100%
  - Installing sebastian/global-state (1.1.1) Downloading: 100%
  - Installing sebastian/exporter (1.2.2) Downloading: 100%
  - Installing sebastian/environment (2.0.0) Downloading: 100%
  - Installing sebastian/diff (1.4.1) Downloading: 100%
  - Installing sebastian/comparator (1.2.4) Downloading: 100%
  - Installing doctrine/instantiator (1.0.5) Downloading: 100%
  - Installing phpunit/php-text-template (1.2.1) Downloading: 100%
  - Installing phpunit/phpunit-mock-objects (3.4.3) Downloading: 100%
  - Installing phpunit/php-timer (1.0.9) Downloading: 100%
  - Installing phpunit/php-file-iterator (1.4.2) Downloading: 100%
  - Installing sebastian/code-unit-reverse-lookup (1.0.0) Downloading: 100%
  - Installing phpunit/php-token-stream (1.4.11) Downloading: 100%
  - Installing phpunit/php-code-coverage (4.0.6) Downloading: 100%
  - Installing webmozart/assert (1.2.0) Downloading: 100%
  - Installing phpdocumentor/reflection-common (1.0) Downloading: 100%
  - Installing phpdocumentor/type-resolver (0.2.1) Downloading: 100%
  - Installing phpdocumentor/reflection-docblock (3.1.1) Downloading: 100%
  - Installing phpspec/prophecy (v1.6.2) Downloading: 100%
  - Installing myclabs/deep-copy (1.6.0) Downloading: 100%
  - Installing phpunit/phpunit (5.5.4) Downloading: 100%
symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.4.0)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files

PHPUnitの確認

vendor内にPHPUnitができてたのでこれをつかうのかな?

[vagrant@localhost html]$ vendor/bin/phpunit --version
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

なんかすごい遠回りしたけど導入できたと思う