11
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP: テストでglob関数にfalseを返させるテクニック

Posted at

PHPのglob関数falseを返す場合がある。

PHP__glob_-_Manual.png

PHPUnitなどでglob関数を含むコードをテストしているとき、このケースを満たすためにfalseがどうしても欲しい場合がある。しかし、よっぽどのことがない限りfalseに出会うことはない。手元の環境で実行してみた限りでは、マッチするファイルが見つからなくてもfalseが返ることはなかった。

PHPのソースコードを読んで、唯一手軽にfalseを返させる方法は、極端に長いパターンを与えることだとわかった。

var_dump(glob(str_repeat('.', 1024)));
Warning: glob(): Pattern exceeds the maximum allowed length of 1024 characters in /../test.php on line 3
bool(false)

つまり、テストで意図的にfalseを返させるには、globに長い文字列を渡すとよい。

11
2
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
11
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?