![]() |
Mock models with PHPUnit - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Mock models with PHPUnit (/showthread.php?tid=1577) |
Mock models with PHPUnit - Spiker - 03-23-2015 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? Question: 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() 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'); 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) 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. |