how to test models using php unit testing framework?

Download How to test models using php unit testing framework?

Post on 26-Jan-2017




3 download

Embed Size (px)


  • How to test models using PHPUnit testing framework?Satej Kumar SahuMindfire Solutions

  • ContentsBasic intro to PHPUnit and conceptsUnit test conceptsHow to think about test cases?Intro to MVC structureRole of Model layer Testing models using PHPUnitTesting models for database integration using DBUnit extension of PHPUnitUnit testing models using MocksConclusion

  • Overall session coverage and take aways from it.

  • Basic intro to PHPUnit and conceptsPHPUnit is a programmer-oriented testing framework for PHP.A clear precise documentation: Download latest phpunit.phar file- Give it execute permission: chmod +x phpunit.phar- Move it to /usr/local/bin/phpunit file: sudo mv phpunit.phar /usr/local/bin/phpunit

  • Unit test concepts3 As of unit testing:- Assemble- Action- AssertSingle responsibility principle that is a class dedicated for a single independent modular functionality and each function in it handling a single responsibility.Dependency Injection.Leads to independent decoupled and unit testable modular code.

  • How to think about test cases?Suppose we want to write unit test cases for a class.So we will be writing test cases for each function provided they are unit testable that is the code handles a single responsibility.Does it suffice to say that a single test case will cover a single function?No, you can say that a function should have at least one test case. But may have more depending on any branching logic in the function. Each test case should handle every possible logic in the functions.So if there is an if .. else .., in that case you will have to write two test cases to cover the function testing.

  • Intro to MVC structure

  • Role of Model layerModels contain your business logic of your application and controllers should be as thin as possible responsible for only routing, interacting with models and fetching view page updated with model data.Basically two functionalities of model layer:- Data interaction- Business logic, data structure modification etc.It can be only either business logic or database interaction or a combination of both.

  • Testing modelsUnit testing is solely concerned with testing your business logic and not with database testing, email sending, api testing since unit tests (and unit testable modular code) should be isolated from all external dependencies.If you are ignoring this basic rule, you are no longer unit testing.CASE: So what if our function in our model sends an email, is it possible to unit test this so as to ensure that the proper method is called.Yes, the answer is MOCKS.So do we not test the email sending functionality to ensure our models are doing what they are supposed to do. YES, we can to verify the functionality is working. But this would not be unit testing but integration testing since we will be testing our method with email sending functionality.

  • Techniques to test models using PHPUnitDBUnit extension for database integration testingMocks to unit test models

  • Getting bored with FACTS :(We are done with the boring FACTS stuff and now into some practical coding :)

  • Testing models for database integration using DBUnit extension of PHPUnitKnown state of data for each test caseData fixturesTypes of data fixturesgetConnection()getDataSet()

  • Unit testing models using MocksMocking means emulating or imitate something or some behaviour.
  • ConclusionWe need to cover models with both unit testing using Mocks and integration testing using DBUnit extension.Unit test is a must to ensure that all unit modules are working as expected.Sometimes you may not want to run all your integration tests because all the database interactions, email sending will take time and you may not want to wait for all of them to execute and would want to just ensure unit testability by running unit tests.

  • Any?