LoginSignup
0
0

More than 1 year has passed since last update.

assertMatchesRegularExpression()で文字列にスラッシュが入ってる場合の対処

Posted at

はじめに

PHPUNITテストで文字列確認のテストを行う
・その文字列に/が含まれていて正常に動作しない状況である

対策

まず該当文字列がふくまれているかの確認は

assertMatchesRegularExpression()を使用する。

書き方は以下。

$this->assertMatchesRegularExpression('/該当文字列を正規表現で表示/', $result);

該当文字列はデリミタにスラッシュを使用する。

ここで問題はその該当文字列にスラッシュやほかの記号が入る場合は,
エスケープさせるためにpreg_quote()を使用する。

$this->assertMatchesRegularExpression('/' . preg_quote('23行目の列数(CSV:2列/正:3列)が正しくありません', '/') . '/', $result);

これで正常に確認が可能となる。

0
0
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
0
0