LoginSignup
10
8

More than 5 years have passed since last update.

PHP5.2の開発環境にPHPUnit3.4をいれる

Last updated at Posted at 2017-02-02

制約多すぎて挫けそうだ。

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 余談はここから ↓-------------------

PHPに限らず何かしら開発をするときに、
テストの実行環境がないと厳しい。
PHPにはPHPUnitという単体テストツールが古くから存在するが、
紆余曲折あって古い環境にインストールするのが非常に難しくなっている。

PHPUnitの最新バージョンはPHPUnit5.7(2017/2/1時点) 。
PHP7をサポートする強力なもの。

ところが、手持ちの開発環境はPHPは5.2.17 (2017/2/1時点) 。
PHPUnit3.7すら動かず3.6以前のどれかということになる。

当然root権限はなく、PHARモジュールすらない。
PEARを使えばと思われるかもだが、
PHPUnitがPEARから離脱してしまっていて、
リポジトリ自体が存在しない。
そんな状況でインストールするには?

(ーー;) ベタにやるしかない・・・よね。

実はPHPUnitのGitHubにインストール方法が書いてある。
https://github.com/sebastianbergmann/phpunit/tree/3.5#using-phpunit-from-a-git-checkout

ただ、試したところ3.6も3.5もインストールできなかった。
(間接的に使ってるモジュールのパスが変わってる)

そこでPHPUnit3.4を使ってインストールを行った。

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 本題はここから ↓-------------------

PHPUnitのインストール

インストール先をphpunitというディレクトリとしている

必要なモジュールをGitHubからダウンロード

mkdir phpunit && cd phpunit
git clone git://github.com/sebastianbergmann/phpunit.git
git clone git://github.com/sebastianbergmann/dbunit.git
git clone git://github.com/sebastianbergmann/php-file-iterator.git
git clone git://github.com/sebastianbergmann/php-text-template.git
git clone git://github.com/sebastianbergmann/php-code-coverage.git
git clone git://github.com/sebastianbergmann/php-token-stream.git
git clone git://github.com/sebastianbergmann/php-timer.git
git clone git://github.com/sebastianbergmann/phpunit-mock-objects.git
git clone git://github.com/sebastianbergmann/phpunit-selenium.git

各ブランチを変更

cd phpunit && git checkout 3.4 && cd ..
cd dbunit && git checkout 1.0 && cd ..
cd php-file-iterator && git checkout 1.2 && cd ..
cd php-code-coverage && git checkout 1.0 && cd ..
cd php-token-stream && git checkout 1.0 && cd ..
cd phpunit-mock-objects && git checkout 1.0 && cd ..
cd phpunit-selenium && git checkout 1.0 && cd ..

実行用のシェルを作成

phpunit.sh
#!/bin/sh

php -d include_path='.:./phpunit/:./dbunit/:./php-code-coverage/:./php-file-iterator/:./php-invoker/:./php-text-template/:./php-timer:./php-token-stream:./phpunit-mock-objects/:./phpunit-selenium/:./phpunit-story/:/usr/local/lib/php' ./phpunit/phpunit.php $*

テスト実行

unittest.php
<?php
require_once 'PHPUnit/Framework.php';

/**
 * Test class for Hello.
 * Generated by PHPUnit on 2010-03-29 at 03:14:15.
 */
class HelloTest extends PHPUnit_Framework_TestCase
{
    protected function setUp(){
    }

   /**
     * @dataProvider abcProvider
     */
    public function testAbc($actual, $expected){
        $this->assertEquals($actual, $expected);
    }

    public function abcProvider(){
         return array(
             array(1,0),
         );
    }

}

実行

$ chmod +x phpunit.sh
$ ./phpunit.sh test.php
PHPUnit @package_version@ by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 2.50Mb

There was 1 failure:

1) HelloTest::testAbc with data set #0 (1, 0)
Failed asserting that 0 matches expected 1.

/home/dozo/tmp/unittest.php:16

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

ちゃんと動作していて、
dataproviderも使えている。

ヽ(^ω^)ノ ヤッタネ


ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪

------------------- ↓ 後書はここから ↓-------------------

インストール時のエラー

上記ではPHPUnit3.4をインストールしたが、
3.5や3.6ももちろん試した。
その前にPEARインストール、PEARローカルインストール、composerインストールも試したわけだが、
どれもこれもうまくいかなかった。

(・_・;) 詰んだな・・・

ハックすれば使うこともできたかもだが、
そこまでやらんでええかとやめたわけだが、
どんなエラーが出たのかは残しておこう。

composerインストールのエラー

$ wget https://getcomposer.org/download/1.3.2/composer.phar
$ php composer.phar

Fatal error: Class 'Phar' not found in /home/dozo/composer.phar on line 23

(゚Д゚;) あ・・・PHARモジュール入ってない。。。

$ pecl download https://pecl.php.net/get/phar-2.0.0.tgz
$ tar xvfz phar-2.0.0.tgz
$ cd phar-2.0.0
$ phpize
$ ./configure
$ make
・・・
/usr/include/php/ext/spl/spl_iterators.h:27 から include されたファイル中,
                 /usr/include/php/ext/spl/spl_array.h:26 から,
                 /home/dozo/phar-2.0.0/phar_internal.h:59 から,
                 /home/dozo/phar-2.0.0/util.c:23 から:
/usr/include/php/ext/pcre/php_pcre.h:29:18: error: pcre.h: そのようなファイルやディレクトリはありません
In file included from /usr/include/php/ext/spl/spl_iterators.h:27,
                 from /usr/include/php/ext/spl/spl_array.h:26,
                 from /home/dozo/phar-2.0.0/phar_internal.h:59,
                 from /home/dozo/phar-2.0.0/util.c:23:

あかん、ライブラリ足らんっぽい。。。
断念。

PEARインストールのエラー

$ pear config-create /home/dozo/pear /home/dozo/.pearrc
$ pear -c ./.pearrc install
$ pear -c ./.pearrc install PHPUnit
WARNING: "pear/PHPUnit" is deprecated in favor of "channel://pear.phpunit.de/PHPUnit"
Did not download dependencies: pear/PHP_Compat, use --alldeps or --onlyreqdeps to download automatically
pear/PHPUnit can optionally use package "pear/PHP_Compat"
downloading PHPUnit-1.3.2.tgz ...
Starting to download PHPUnit-1.3.2.tgz (20,913 bytes)
........done: 20,913 bytes
install ok: channel://pear.php.net/PHPUnit-1.3.2

(゚Д゚)!? 1.3.2が入ってもうた。
流石に古すぎる。

$ pear -c ./.pearrc install pear.phpunit.de/PHPUnit
Attempting to discover channel "pear.phpunit.de"...
downloading phpunit.de ...
Starting to download phpunit.de (5,378 bytes)
.....done: 5,378 bytes
unknown channel "pear.phpunit.de" in "pear.phpunit.de/PHPUnit"
invalid package name/package file "pear.phpunit.de/PHPUnit"
install failed

チャンネルがもうない・・・ですよねぇ。
断念。

PHPUnit3.6のエラー

$ ./phpunit.sh ../test.php
Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in /home/dozo/phpunit/phpunit/PHPUnit/Autoload.php on line 45

Fatal error: require_once(): Failed opening required 'File/Iterator/Autoload.php' (include_path='.:./phpunit/:./dbunit/:./php-code-coverage/:./php-file-iterator/:./php-invoker/:./php-text-template/:./php-timer:./php-token-stream:./phpunit-mock-objects/:./phpunit-selenium/:./phpunit-story/:/usr/local/lib/php') in /home/dozo/phpunit/phpunit/PHPUnit/Autoload.php on line 45

Autoload.phpがないか・・・。

PHPUnit3.5のエラー

$ ./phpunit.sh ../test.php

Notice: Please no longer include "PHPUnit/Framework.php". in /home/dozo/phpunit/phpunit/PHPUnit/Framework.php on line 50

Warning: require_once(Text/Template.php): failed to open stream: No such file or directory in /home/dozo/phpunit/phpunit/PHPUnit/Framework/TestCase.php on line 46

Fatal error: require_once(): Failed opening required 'Text/Template.php' (include_path='/home/dozo/phpunit/phpunit:.:./phpunit/:./dbunit/:./php-code-coverage/:./php-file-iterator/:./php-invoker/:./php-text-template/:./php-timer:./php-token-stream:./phpunit-mock-objects/:./phpunit-selenium/:./phpunit-story/:/usr/local/lib/php') in /home/dozo/phpunit/phpunit/PHPUnit/Framework/TestCase.php on line 46

こっちもファイルが見つからないとのこと。
ファイル構成がいろいろ変ったみたいやね。

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8