• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mock models with PHPUnit

I have found one resolution that is described after my question. I was wondering if you could point me in the direction on where I can find some reading material regarding unit testing, I am interested in:
- Use of fake classes in PHPUnit for testing?
- Working with a dummy database to test application ( Live application )?
- Creation of dummy data for unit testing using arrays?


I have a library with a range of functions, some of the functions send data to the model to perform CRUD operations.

At the present moment I am interested in creating a dummy set of data and a mock of those CRUD operations, so rather than going into DB i would get data from a dummy set of data.

I have found some examples online on how to do Mock of the function but i didn't have much luck getting Mock to work, my original function still communicates to the DB.

Below is example of the code that I have in my PHPUnit test class
PHP Code:
public function testSaveExistingSupplier()
       $suppliers = array(
           array('id' => '1''name' => 'O2')
       // Create a stub.
       require_once(APPPATH '/models/suppliers_model.php');
       $stub $this->getMockBuilder('suppliers_model')

       // Configure the stub.
       $this->assertEquals($result$suppliers[0]['id'], "Supplier exists");

saveSupplier functions checks in DB if supplier with supplied param exists (in our case "O2") by calling getSupplierByText method in suppliers_model. If supplier exists than we return int other ways we create new supplier and return int.

So, I have tried to create a mock function that changes the return value for method getSupplierByText


Current Resolution:
I have also tried implementation below, taken from the PHPUnit 3.7 DOC:
PHP Code:
$suppliers = array('id' => '1''name' => 'O2');
// Create a stub for the SomeClass class.
$stub $this->getMock('suppliers_model'); 

// Configure the stub.

$result $stub->saveSupplier("O2");
$this->assertEquals($result$suppliers[0]['id'], "Existing supplier exists"); 

The code above code works, I had to create a new function in my library that dealt with a call to the model; below is my original function:
PHP Code:
public function saveSupplier($supplier)
// find if this supplier exists
$supplier_info $this->ci->suppliers_model->getSupplierByText($supplier);

$supplier_info != FALSE)
// supplier already exists
return $supplier_info[0]['id'];

// create new supplier
$options = array();
$options['text'] = $supplier;

$supplier_id $this->ci->suppliers_model->createEntry($options);


On the 4th line I have amended the code to "$supplier_info = $this->getSupplierByText($supplier);" and created a new function that deals with a call:
PHP Code:
public function getSupplierByText($supplier)

Now, because I am calling a specific function within a library that I am testing , I was able to mask "getSupplierByText" function and provide a desired return parameter.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.