php - How to unit test a data extraction method -


i have created method interacts framework, pulling out order items have completed status.

however, how can unit test method ensure behaves correctly...

class {     public function extractdata()     {         // extract data framework         $datacollection = frameworkx->getdatacollection('sales/orders');         $datacollection->filter('state', 'complete');          return $extracteddata;     } }   classatest {     public function test_extracted_data_contains_only_items_with_complete_status {         $sut  = new classa();         $sut->extractdata();          // assertion here?     }  } 

you can iterate through collection , assert every item in 'complete' state.

however you're doing here called integration test.

you're not testing method (unit), test how framework behave (get's data storage) , how filter method works.

if want have unit-test have @ article. should create stub framewokx->getdatacollection() method.

how test methods third party dependencies

if frameworkx final can declare interface:

interface idatasource {     public function getdatacollection($path); } 

and new class:

class datasource implements idatasource    {       public function getdatacollection($path)       {          //no need test method, it's wrapper          return  frameworkx->getdatacollection('sales/orders');       }     } 

in class create a constructor:

public function __construct(idatasource $datasource)  {    $this->datasource= $datasource;  } 

and change extractdata method use it.

public function extractdata() {     // extract data framework     $datacollection = $datasource->getdatacollection('sales/orders');     $datacollection->filter('state', 'complete');      return $extracteddata; } 

let's assume you're using phpunit.

 public function test_extracted_data_contains_only_items_with_complete_status ()  {     // create stub datasource class.     $stub = $this->getmockbuilder('datasource')                  ->getmock();      // configure stub.     $stub->method('getdatacollection')          ->willreturn(preconfigured_collection_with_all_statuses_here);      $sut  = new classa($stub); //create new constructor , pass stub     $sut->extractdata();     // , after verify method filtered out non complete statuses  } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -