CIUnit: Unit testing for CodeIgniter(2)の続き

CodeIgnigter


CIUnit: Unit testing for CodeIgniter(2)
の続きです。
(3)ではありませn・・・・・(汗)
phpunitの再インストールで動き出したと思われたCIUnitですが、
正常な動作ではありませんでした。

$ cd controllers/

$ phpunit testWelcome.php 
PHPUnit 3.3.16 by Sebastian Bergmann.

..

Time: 0 seconds

OK (2 tests, 2 assertions)

$ phpunit ControllersAllTests.php 
PHPUnit 3.3.16 by Sebastian Bergmann.

..

Time: 0 seconds

OK (2 tests, 2 assertions)

ここまでは正常です。

controllreのテストまで正常に動きましたが、
全テストではエラーが発生します

$ cd ../
phpunit AllTests.php 
PHPUnit 3.3.16 by Sebastian Bergmann.

.............<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  CIUnit::include_once(/var/www/html/works/system/application/controllers/Welcome.php): failed to open stream: No such file or directory</p>
<p>Filename: tests/CIUnit.php</p>
<p>Line Number: 124</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  CIUnit::include_once(): Failed opening '/var/www/html/works/system/application/controllers/Welcome.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php')</p>
<p>Filename: tests/CIUnit.php</p>
<p>Line Number: 124</p>

</div>PHP Fatal error:  Class 'Welcome' not found in /var/www/html/works/system/application/tests/CIUnit.php on line 130
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
PHP   3. PHPUnit_TextUI_TestRunner->doRun() /usr/share/pear/PHPUnit/TextUI/Command.php:128
PHP   4. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/TextUI/TestRunner.php:324
PHP   5. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
PHP   6. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
PHP   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/pear/PHPUnit/Framework/TestSuite.php:658
PHP   8. PHPUnit_Framework_TestCase->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:677
PHP   9. PHPUnit_Framework_TestResult->run() /usr/share/pear/PHPUnit/Framework/TestCase.php:375
PHP  10. PHPUnit_Framework_TestCase->runBare() /usr/share/pear/PHPUnit/Framework/TestResult.php:607
PHP  11. PHPUnit_Framework_TestCase->runTest() /usr/share/pear/PHPUnit/Framework/TestCase.php:404
PHP  12. ReflectionMethod->invoke() /usr/share/pear/PHPUnit/Framework/TestCase.php:489
PHP  13. testCIUnit->testDifferentControllers() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:0
PHP  14. set_controller() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:147
PHP  15. CIUnit::set_controller() /var/www/html/works/system/application/tests/CIUnit.php:195

Fatal error: Class 'Welcome' not found in /var/www/html/works/system/application/tests/CIUnit.php on line 130

Call Stack:
    0.0005	83784   1. {main}() /usr/bin/phpunit:0
    0.1485    5663072   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
    0.3327    9503688   3. PHPUnit_TextUI_TestRunner->doRun() /usr/share/pear/PHPUnit/TextUI/Command.php:128
    0.3333    9506304   4. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/TextUI/TestRunner.php:324
    0.3342    9506512   5. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
    0.4183    9597480   6. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
    0.4809   10191464   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/pear/PHPUnit/Framework/TestSuite.php:658
    0.4809   10191464   8. PHPUnit_Framework_TestCase->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:677
    0.4809   10191464   9. PHPUnit_Framework_TestResult->run() /usr/share/pear/PHPUnit/Framework/TestCase.php:375
    0.4811   10191464  10. PHPUnit_Framework_TestCase->runBare() /usr/share/pear/PHPUnit/Framework/TestResult.php:607
    0.4825   10201528  11. PHPUnit_Framework_TestCase->runTest() /usr/share/pear/PHPUnit/Framework/TestCase.php:404
    0.4826   10201528  12. ReflectionMethod->invoke() /usr/share/pear/PHPUnit/Framework/TestCase.php:489
    0.4826   10201528  13. testCIUnit->testDifferentControllers() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:0
    0.4827   10201528  14. set_controller() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:147
    0.4827   10201528  15. CIUnit::set_controller() /var/www/html/works/system/application/tests/CIUnit.php:195

$ cd controllers/
$phpunit testWelcome.php 
PHPUnit 3.3.16 by Sebastian Bergmann.

..

Time: 0 seconds

OK (2 tests, 2 assertions)
[root@localhost controllers]# phpunit ControllersAllTests.php 
PHPUnit 3.3.16 by Sebastian Bergmann.

..

Time: 0 seconds

OK (2 tests, 2 assertions)
$  cd ../
$ phpunit AllTests.php 
PHPUnit 3.3.16 by Sebastian Bergmann.

.............<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  CIUnit::include_once(/var/www/html/works/system/application/controllers/Welcome.php): failed to open stream: No such file or directory</p>
<p>Filename: tests/CIUnit.php</p>
<p>Line Number: 124</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  CIUnit::include_once(): Failed opening '/var/www/html/works/system/application/controllers/Welcome.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php')</p>
<p>Filename: tests/CIUnit.php</p>
<p>Line Number: 124</p>

</div>PHP Fatal error:  Class 'Welcome' not found in /var/www/html/works/system/application/tests/CIUnit.php on line 130
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
PHP   3. PHPUnit_TextUI_TestRunner->doRun() /usr/share/pear/PHPUnit/TextUI/Command.php:128
PHP   4. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/TextUI/TestRunner.php:324
PHP   5. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
PHP   6. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
PHP   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/pear/PHPUnit/Framework/TestSuite.php:658
PHP   8. PHPUnit_Framework_TestCase->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:677
PHP   9. PHPUnit_Framework_TestResult->run() /usr/share/pear/PHPUnit/Framework/TestCase.php:375
PHP  10. PHPUnit_Framework_TestCase->runBare() /usr/share/pear/PHPUnit/Framework/TestResult.php:607
PHP  11. PHPUnit_Framework_TestCase->runTest() /usr/share/pear/PHPUnit/Framework/TestCase.php:404
PHP  12. ReflectionMethod->invoke() /usr/share/pear/PHPUnit/Framework/TestCase.php:489
PHP  13. testCIUnit->testDifferentControllers() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:0
PHP  14. set_controller() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:147
PHP  15. CIUnit::set_controller() /var/www/html/works/system/application/tests/CIUnit.php:195

Fatal error: Class 'Welcome' not found in /var/www/html/works/system/application/tests/CIUnit.php on line 130

Call Stack:
    0.0006	83784   1. {main}() /usr/bin/phpunit:0
    0.1494    5663072   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:52
    0.3345    9503688   3. PHPUnit_TextUI_TestRunner->doRun() /usr/share/pear/PHPUnit/TextUI/Command.php:128
    0.3351    9506304   4. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/TextUI/TestRunner.php:324
    0.3360    9506512   5. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
    0.4190    9597480   6. PHPUnit_Framework_TestSuite->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:621
    0.4814   10191464   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/pear/PHPUnit/Framework/TestSuite.php:658
    0.4814   10191464   8. PHPUnit_Framework_TestCase->run() /usr/share/pear/PHPUnit/Framework/TestSuite.php:677
    0.4814   10191464   9. PHPUnit_Framework_TestResult->run() /usr/share/pear/PHPUnit/Framework/TestCase.php:375
    0.4816   10191464  10. PHPUnit_Framework_TestCase->runBare() /usr/share/pear/PHPUnit/Framework/TestResult.php:607
    0.4831   10201528  11. PHPUnit_Framework_TestCase->runTest() /usr/share/pear/PHPUnit/Framework/TestCase.php:404
    0.4831   10201528  12. ReflectionMethod->invoke() /usr/share/pear/PHPUnit/Framework/TestCase.php:489
    0.4831   10201528  13. testCIUnit->testDifferentControllers() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:0
    0.4832   10201528  14. set_controller() /var/www/html/works/system/application/tests/ciunit/testCIUnit.php:147
    0.4832   10201528  15. CIUnit::set_controller() /var/www/html/works/system/application/tests/CIUnit.php:195

なぜかAllTTestの場合、controllerのwelcomeを呼び出しているところが、Welcomeを呼び出そうとして失敗をしています。

CIUnit.phpの124行目

include_once(APPPATH . ‘controllers/’ . $controller . EXT);
の部分の$controller がwelcomeからWelcomeに変化しています。それによってファイルが読み込めずのエラーのようです。

無理やりな感じになおしてみました。

CIUnit.phpの124行目
include_once(APPPATH . ‘controllers/’ . $controller . EXT);
大文字でファイルがなかったら、小文字にしてファイル読み込んでよね的。

if(is_file(APPPATH . 'controllers/' . $controller . EXT)){
		    include_once(APPPATH . 'controllers/' . $controller . EXT);
		}else{
		    $controller2 = strtolower($controller);
		    include_once(APPPATH . 'controllers/' . $controller2 . EXT);
		}

やっとやっと、テストができるところまで到着(TAT)

ここまでの感想
phpunitの再インストールの記事を自力で探せませんでした。
基礎力がおそろしく足りないことを自覚しました。
「何がわからないか?」がわからないと解答が目の前にあってもそれが解答と理解できないのだなぁ・・・・

CIUnitへの挑戦はこんな流れでがんばってみる予定です

  • CIUnitを設置してみる
  • CodeIgniterでテストができるようにする ←今ココ
  • 実際にテストを書いてみる
  • テストの効果を実感してみる
  • CIUnitのコードを読んでみる
  • MyNETS2(CodeIgniter 1.7.0改)で使えないか模索してみる

    「CIUnit: Unit testing for CodeIgniter(2)の続き」への2件のフィードバック

    1. if(is_file(APPPATH . ‘controllers/’ . $controller . EXT)){
      include_once(APPPATH . ‘controllers/’ . $controller . EXT);
      }else{
      $controller2 = strtolower($controller);
      include_once(APPPATH . ‘controllers/’ . $controller2 . EXT);
      }

      ここ、判定せずすべてstrtolower($controller)ではだめなんでしょうか??

    2. コーディングルールではファイル名は小文字でということで、私も小文字でファイル名をつけるので、実際の処理は全部小文字に変えちゃっても全く問題ないです。

      元々出来た処理ができなくなるのはだめかなと思ったのでここでは判定をいれてみました。
      (ファイル名に大文字が入っていた場合には読み込めずにエラーになります。)

    コメントは停止中です。