PHPUnitで簡単なテストをする2 バグ?

前回PHPUnitの簡単なテストを試してみました。

PHPUnitのdataProvider機能を使ったりしてたら気になる状態になった

テストコード

<?php

require_once ('vendor/autoload.php');

use PHPUnit\Framework\TestCase;

class testHoge extends TestCase
{
    /**
     * @dataProvider testdataProvider
     */
    public function testunittest($a, $expected)
    {
        $this->assertEquals($expected, -1);
    }

    public function testdataProvider()
    {
        return [
            [0, 0],
        ];
    }
}
?>

dataProviderを使ったコードでこれを動かすと

[vagrant@localhost html]$ vendor/bin/phpunit tests/testHoge.php
PHPUnit 5.5.7 by Sebastian Bergmann and contributors.

F.                                                                  2 / 2 (100%)

Time: 241 ms, Memory: 4.00MB

There was 1 failure:

1) testHoge::testunittest with data set #0 (0, 0)
Failed asserting that -1 matches expected 0.

/var/www/html/tests/hogeTest.php:14

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

テストは1つしかやってないはずなのに2つチェックが入ってる

1つ目は失敗してるのであってるのだが2つ目のはどこをチェックしてるんだろ

しかも成功しちゃってるし

んー  なんだろこれ

PHPUnitで簡単なテストをする

前回PHPUnitのインストールまでやったので簡単なサンプルまでを試すメモです

準備

ディレクトリ構成

composerでPHPUnitいれたらvendeorができるので 追加でメイン用とテスト用のphpを作る

www/
├src/
│ └message.php
├tests/
│ └messageTest.php
├vendor/
│ └︙
├composer.json
├composer.lock

message.php

メイン用

<?php

class Message
{
    private $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function get()
    {
        return $this->message;
    }
}
?>

messageTest.php

テスト用

<?php

require_once ('vendor/autoload.php');
require_once (dirname(__FILE__) .'/../src/message.php');

use PHPUnit\Framework\TestCase;

class MessageTest extends TestCase
{
    public function testGet()
    {
        $message = new Message('hello, world');
        $this->assertEquals('hello, world', $message->get());
    }
}
?>

コマンド

testsフォルダを指定して***Test.phpを調べてくれる

$ vendor/bin/phpunit /tests
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

W.                                                                  2 / 2 (100%)

Time: 204 ms, Memory: 4.00MB

There was 1 warning:

1) Warning
No tests found in class "PHPUnit\Framework\TestCase".

WARNINGS!
Tests: 2, Assertions: 1, Warnings: 1.

とりあえずはテストできてるみたいだけどWARNINGSが気になる。
呼び出し方がまちがってる?

原因探るためいろいろ入れたり消したりしてたらWARNINGSが消えて何で解消できたのがか不明。

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.

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

はてぶに無限スクロールをつけるchrome-extension作った

1週間ぶりです

はてなブックマークって昔は新着ページに自動読み込みがあったはずなんですが
いまのタイル形式になってからきえちゃったみたいなんですよね。
タイル形式になってから、Greasemonkeyautopagerizeも効かないし。
もしかして機能はあって当たり前すぎて検索にも引っかからないだけなのか
あったら教えてー

プログラム始めて勉強のついでに作ってみました。

javascriptとか初心者だから調べながらやりました。
もうちょいきれいに書けたらなと思います。

github.com

使い方

  1. get cloneかdownloadする
  2. Chomeの拡張機能のでペロッパーモードにチェックをいれる
  3. 「パッケージ化されていない拡張機能を読み込む」から先程のファイルを選択する

これだけでとりあえずは動くはず。
最低限の機能(次ページ追加)だけなので
アイコンは適当だし(絵かけない)
オプションとかもないです、
読み込み中マークもでません (ちょっと待つと追加される)
あ、リストレイアウトも対応してません。(タイルレイアウトだけ)

最後に
はてなの開発の人、公式でつくってほしいです

とりあえずPHPでHelloWorld

やっと環境ができたので、

プログラムで最初にやるのはHelloWorldの表示させることでしょうか

<?php
echo "HelloWorld";
?>

まあ、表示するだけなら上ので終わりなんですよね


なので練習問題的なのを探してそれをやってみました

<?php
/**
 *
 * ループでHello World!5回表示する
 *
 * http://vipprog.net/wiki/exercise.html#eb2c4338
 *
 */
for ($i=0; $i<5; $i++) {
    echo "HelloWorld!".nl2br("\n");
}

?>

classを使うとこんな感じ?

<?php
/**
 *
 * ループでHello World!5回表示する
 *
 * http://vipprog.net/wiki/exercise.html#eb2c4338
 *
 */
class HelloWorldObj {
    private $count = 0;
    private $text = "HelloWorld!";

    public function getText() {
        return $this->text;
    }

    public function setCount($count) {
        $this->count = $count;
    }

    public function loopEcho() {
        for ($i=0; $i<$this->count; $i++) {
            echo $this->getText().nl2br("\n");
        }
    }
}
$HelloWorld = new HelloWorldObj();
$HelloWorld->setCount(5);
$HelloWorld->loopEcho();
?>

次は順番で行くとfizzbuzzかな

あとnl2brがnewline to breakって意味だってわかった

Vagrantの共有フォルダで403Forbiddenエラーがでる

Vagrantの共有フォルダをhttpdのDocumentRootとして扱う

これならWindows上でファイル編集できるからやりやすそうと思ってVagrantfileを編集してみた

config.vm.synced_folder "../data/var/www/html", "/var/www/html"

これで/html/index.htmlとか作って表示をさせたかった

アクセスしてみると403エラー

アクセスしてみると次の403エラーで表示できなかった

Forbidden
You don't have permission to access /index.html on this server.

表示させるには?

SELinuxを無効にすればとりあえずは表示するようになった

SELinuxの操作

SELinuxの確認

# getenforce
Enforcing
enforcing
SELinux機能,アクセス制限が有効
permissive
SELinuxは警告を出力、アクセス制限は無効
disabled
SELinux機能、アクセス制限がが無効

SELinuxを一時的に有効、無効にする

SELinuxを一時的に無効にする

# setenforce 0

SELinuxを一時的に有効にする

# setenforce 1

上の設定はサーバー再起動すると元に戻る

SELinuxの設定

サーバーを再起動させても設定を保持したい場合は設定ファイルを変更しないといけない

;; 設定ファイルの場所
# vi /etc/selinux/config

でファイルを開いたあとSELINUX=***の箇所を変更する

;; 有効
SELINUX=enforcing
;; 無効
SELINUX=disabled

変更したあとサーバーを再起動させる

ps

SELinuxを有効のまま表示させる方法は結局わからなかった

Vagrant1.9.1でprivate_networkを設定しても有効にならない現象

Vagrantの記事をみると当たり前のようにprivate_networkの設定をやってます。

私の記事では設定は飛ばしてました。

それは、設定ができなかったから・・・

過去記事から延々と探しても探しても

config.vm.network “private_network”, ip: “192.168.33.10” これでOK

としか書いてなくて、どうししたらええんや (ノ`Д´)ノ.:・┻┻)`з゜)・:゙;

このエントリーはprivate_networkの設定までのメモです。

Vagrant1.9.1でprivate_networkを設定しても有効にならない現象

Vagrantfileのprivate_networkを設定しても指定したIPで接続できない

#Vagrantfileの変更箇所
config.vm.network "private_network", ip: "192.168.33.10"

CentOSに接続してIPを確認してもVagrantfileで設定したIPがない

[vagrant@localhost ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:22:5b:53 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 86390sec preferred_lft 86390sec
    inet6 fe80::5054:ff:fe22:5b53/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:05:e3:07 brd ff:ff:ff:ff:ff:ff

次のコマンドでVagrantfileのprivate_networkが有効になる

[vagrant@localhost ~]$systemctl restart network

有効になってるか確認 private_networkで指定したIPが表示されてればつながるはず

[vagrant@localhost ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:22:5b:53 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 86387sec preferred_lft 86387sec
    inet6 fe80::5054:ff:fe22:5b53/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:05:e3:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.10/24 brd 192.168.33.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe05:e307/64 scope link
       valid_lft forever preferred_lft forevre

原因

Vagrant1.9.1でのバグっぽい

https://github.com/mitchellh/vagrant/issues/8096

対応

下のどれかでしのぐ

  • ログインしたあとコマンドでsystemctl restart networkする
  • Vagrantfileに
    config.vm.provision "shell", inline: "systemctl restart network.service", run: "always"
    を追加する
  • 1.9.2がでるまで1.9.0を使う

ですかねー