php - PHPUnit and Domain Exception Testing -


i have given myself challenge making use of testing code develop figured not practice end developer helps 1 lot in building scalable , extendable code in long run.

i have class defines array of supported doctrine drivers. inside class there method creates instance of annotation driver supported bundle.

protected $supportedannotationdrivers = array(   'doctrine\orm\mapping\driver\xmldriver',   'doctrine\orm\mapping\driver\yamldriver',   'doctrine\orm\mapping\driver\annotationdriver'  ); 

the method creates instance of these drivers can seen below:

 public function getannotationdriver($classdriver, $entitydriver)  {      if(!in_array($classdriver, $this->supportedannotationdrivers)){         throw new \runtimeexception(sprintf(            "%s not supported annotation driver.", $classdriver           ));                    }        if('doctrine\orm\mapping\driver\annotationdriver' === $classdriver){            $annotationdriver = new $classdriver(new annotationreader(), array($entitydriver));            annotationregistry::registerloader('class_exists');          }else{           $annotationdriver = new $classdriver($entitydriver);        }        return $annotationdriver;       } 

before go further have have consulted phpunit manual , stackoverflow, here; here; here , there , insights getting confused time. , none of these links providing me certainty going right way.

below test function inside test class:

public function testunsupportedannotationdriverexception() {      try{          $entitymanagerservice = new entitymanagerservice();           //client requests unsupported driver          $unsupporteddriver              = 'doctrine\orm\mapping\driver\staticphpdriver';          $actualunsupportedexception     = $entitymanagerservice->getannotationdriver(          $unsupporteddriver, __dir__ . '/../../helper'        );       }catch(\runtimeexception $actualunsupportedexception){         return;      }      $this->fail('an expected exception has not been raised!.');   } 

i confused when comes part. in test using exception raised code runtimeexception , test passes. when change client code throw exception leave test fails , throws exception client code. happy know test fails because exception thrown in test gives me hope when user passes unsupported driver.

however, lacking contextualization herein of annotation usage , $this->fail("text/for/failer/) function when testing exceptions.

how best go writing test method? have done how test should behave perhaps me not having grasp concept behind part of phpunit? below output phpunit:

case 1:exceptions same test , client code

luyanda.siko@zact-pc301 mingw64 /d/web/doctrine-bundle $ vendor/bin/phpunit   phpunit 3.7.38 sebastian bergmann.     configuration read d:\web\doctrine-bundle\phpunit.xml     ......     time: 1.49 seconds, memory: 3.00mb    ok (6 tests, 9 assertions)   luyanda.siko@zact-pc301 mingw64 /d/web/doctrine-bundle 

case 2:exceptions not same test , client code

luyanda.siko@zact-pc301 mingw64 /d/web/doctrine-bundle  $ vendor/bin/phpunit    phpunit 3.7.38 sebastian bergmann.     configuration read d:\web\doctrine-bundle\phpunit.xml     ..e...     time: 1.59 seconds, memory: 3.00mb     there 1 error:   1)   phpunitbundle\unit\applicationcontext\service\entitymanagerservicetest::testunsupportedannotationdriverexception 

exception: doctrine\orm\mapping\driver\staticphpdriver not supported annotation driver.

 d:\web\doctrine-bundle\lib\doctrinebundle\applicationcontext\service\entitymanagerservice.php:33  d:\web\doctrine-bundle\lib\phpunitbundle\unit\applicationcontext\service\entitymanagerservicetest.php:68   failures!  tests: 6, assertions: 9, errors: 1. 

i want know best way know doing here , if doing right - if not best way it. perpahs trying not worthe believe no single line of code should seen worthless , unworthy of testing.

in general, code provided valid , work should. describe expects code , test code meet expectations.

in case expecting exception type \runtimeexception thrown , if \exception - there error

another question how phpunit tell you. in case shows message

1)   phpunitbundle\unit\applicationcontext\service\entitymanagerservicetest::testunsupportedannotationdriverexception exception: doctrine\orm\mapping\driver\staticphpdriver not supported annotation driver. 

it not obviouse. can make expectations more explicit if descibe them setexpectedexception:

/**  * test fail if client requests unsupported driver  */ public function testunsupportedannotationdriverexception() {     $entitymanagerservice = new entitymanagerservice();      $this->setexpectedexception(\runtimeexception::class);     // $this->setexpectedexception('runtimeexception'); // php < 5.5      $unsupporteddriver = 'doctrine\orm\mapping\driver\staticphpdriver';     $actualunsupportedexception = $entitymanagerservice->getannotationdriver(         $unsupporteddriver, __dir__ . '/../../helper'     ); } 

or using proper annotation @expectedexception:

/**  * test fail if client requests unsupported driver  *  * @expectedexception \runtimeexception  */ public function testunsupportedannotationdriverexception() {     $entitymanagerservice = new entitymanagerservice();      $unsupporteddriver = 'doctrine\orm\mapping\driver\staticphpdriver';     $actualunsupportedexception = $entitymanagerservice->getannotationdriver(         $unsupporteddriver, __dir__ . '/../../helper'     ); } 

now if change throw \runtimeexception throw \exception in tested method, can note phpunit's message this

failed asserting exception of type "exception" matches expected exception "\runtimeexception". message was: "doctrine\orm\mapping\driver\staticphpdriver not supported annotation driver."  

so see more explicit wrong


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -