LoginSignup
3
2

More than 5 years have passed since last update.

PHPUnit+CodeIgniter ユニットテストの備忘録<随時更新>

Posted at

株式会社オズビジョン@terra_yuccoです。

現在、レガシーコードにテストコードを網羅していこうの活動を、エンジニアチームとしてやっています。とはいえ、テストコードだけをひたすら書く活動をするわけではなく、新規に開発を行うときはテストコード必須、可能なら修正時に参照した箇所をテストコード付きでcosme、といった感じで進めています。
利用しているフレームワークはCodeIgniter2.0.3がメインです。
(バージョンを上げられないのも、リスクがあって踏み切れないというところ…)

この記事は私的な備忘録なので、随時更新していきます。

update_batchがnoticeレベルのエラーを出す

エラー内容

[CIUnit] PHP Error: Notice - Array to string conversion File Path: database/DB_active_rec.php (line: 1414)
[CIUnit] PHP Error: Notice - Array to string conversion File Path: database/DB_active_rec.php (line: 1414)

該当コード

$database->update_batch($table, $update_data, 'id');

/**
 * var_dump($update_data); の結果
 *
 * array(1) {
 *   [0] =>
 *   array(3) {
 *     'id' =>
 *     string(7) "1234567"
 *     'register_date' =>
 *     string(19) "2017-03-08 11:10:00"
 *     'update_date' =>
 *     string(19) "2017-03-08 11:10:00"
 *   }
 * }
 */

原因は古いCodeIgniterのバグ

自分のコードが悪いのかと思いましたが、どうもCodeIgniterのバグである模様。
ユーザガイドを見ると呼び出し方はあっていますが、以下のStackoverflowに回答がありました。2.1.4では修正されているようです。
バージョンの違いがあるため行数は少しずれていますが、試しに手元で同様の修正をしたところ、無事にNoticeは消えました。
※とはいえフレームワークを変更することになるので、事象の確認ができたところで元に戻しています。

Just stumbled upon the exact same problem here. Fortunately, I'm using the same CI version. :)

It's true that the answer from M_A_K helps remove the "Notice", but I don't think that is the right solution for the problem. So I decided to take a look at line 1407 in DB_active_rec.php and I believe that this is nothing but a minor bug in CI 2.1.2.

Here's my fix. I simply changed the original code:

$not[] = $k.'-'.$v;
into this:

$not[] = $k2.'-'.$v2;
Voila! The "Notice" doesn't appear anymore. :)

We can clearly see that line 1407 was not meant to use $k and $v because line 1407 is inside a foreach loop iterating through $v as $k2 and $v2.

この記事について

最初にも書いた通り、他にもいくつもいろいろ踏み抜くと思うので、随時更新していきます。

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