LoginSignup
23
22

More than 5 years have passed since last update.

Composerを使ってPHPUnitを使えるようにしSeleniumも一緒に動かす。

Posted at

今までは他の人がVirtualBoxのCentOSにセットしてくれたPHPUnitを使っていたのだが、自分のPC(MacBookPro)にPHPUnitを入れる必要が出てきた!

Homebrew・Gitインストール

PHPUnitを入れるために必要なHomebrewを入れる。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew update

brew install git

git --versionでキチンと動作すればOK!

Composerダウンロード

PHPUnitを入れるならコレを使わないと!ということでComposerダウンロード。
curl -sS https://getcomposer.org/installer | php
それをパスが通っているトコに移動!
sudo -s mv composer.phar /usr/bin/composer

PHPUnitインストール

Composerを使ってPHPUnitをインストールする。
composer global require "phpunit/phpunit=4.3.*"
phpunitコマンドを使うためにパスを通す。~/.bash_profileにも記載しておこう。
export PATH=$PATH:~/.composer/vendor/bin/
パスが通っているかどうかを確認。
phpunit --version

PHPUnitにSeleniumパッケージ追加

私はPHPUnit-seleniumを動かすので、それを動かせるようにしないといけない。
composer.jsonのrequire"phpunit/phpunit-selenium": ">=1.2"の行を追加する。パスは~/.composer/composer.json

composer.json
{
    "require": {
        "phpunit/phpunit": "4.3.*"
         ,
        "phpunit/phpunit-selenium": ">=1.2"
    }
}

書き換えたらComposerを更新する。
composer update

試しにテストプログラムを動かしてみて動いたらOK。
phpunit test.php

テストプログラム参考。ローカルでSeleniumサーバを起動してみよう!

test.php
<?php
class Example extends PHPUnit_Extensions_Selenium2TestCase
{
    public function setUp()
    {
          $this->setBrowser('firefox');
          $this->setHost('localhost');
          $this->setPort(4444);
          $this->setBrowserUrl('http://www.yahoo.co.jp/');
    }

    public function testExample()
    {
           $this->url("http://www.yahoo.co.jp/");
    }
}  
?>
23
22
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
23
22