how to test models using php unit testing framework?

16
How to test models using PHPUnit testing framework? Satej Kumar Sahu Mindfire Solutions

Upload: satejsahu

Post on 26-Jan-2017

151 views

Category:

Software


3 download

TRANSCRIPT

Page 1: How to test models using php unit testing framework?

How to test models using PHPUnit testing

framework?Satej Kumar SahuMindfire Solutions

Page 2: How to test models using php unit testing framework?

Contents• Basic intro to PHPUnit and concepts

• Unit test concepts

• How to think about test cases?

• Intro to MVC structure

• Role of Model layer

• Testing models using PHPUnit

• Testing models for database integration using DBUnit extension of PHPUnit

• Unit testing models using Mocks

• Conclusion

Page 3: How to test models using php unit testing framework?

Overall session coverage and take

aways from it.

Page 4: How to test models using php unit testing framework?

Basic intro to PHPUnit and concepts

• PHPUnit is a programmer-oriented testing framework for PHP.

• A clear precise documentation: https://phpunit.de/documentation.html

• Installation:- 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

Page 5: How to test models using php unit testing framework?

Unit test concepts• 3 A’s of unit testing:

- Assemble- Action- Assert

• Single 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.

Page 6: How to test models using php unit testing framework?

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.

Page 7: How to test models using php unit testing framework?

Intro to MVC structure

Page 8: How to test models using php unit testing framework?

Role of Model layer• Models 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.

Page 9: How to test models using php unit testing framework?

Testing models• Unit 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.

Page 10: How to test models using php unit testing framework?

Techniques to test models using PHPUnit

• DBUnit extension for database integration testing

• Mocks to unit test models

Page 11: How to test models using php unit testing framework?

Getting bored with FACTS :(

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

Page 12: How to test models using php unit testing framework?
Page 13: How to test models using php unit testing framework?

Testing models for database integration using DBUnit extension

of PHPUnit• Known state of data for each test case

• Data fixtures

• Types of data fixtures

• getConnection()

• getDataSet()

Page 14: How to test models using php unit testing framework?

Unit testing models using Mocks

• Mocking means emulating or imitate something or some behaviour.

• <?php

require_once 'SomeClass.php';

class StubTest extends PHPUnit_Framework_TestCase{ public function testOnConsecutiveCallsMock() { // Create a mock for the SomeClass class. $mock = $this->getMockBuilder('SomeClass') ->getMock();

// Configure the mock. $mock->method('doSomething') ->will($this->onConsecutiveCalls(2, 3, 5, 7));

// $stub->doSomething() returns a different value each time $this->assertEquals(2, $mock->doSomething()); $this->assertEquals(3, $mock->doSomething()); $this->assertEquals(5, $mock->doSomething()); }}

Page 15: How to test models using php unit testing framework?

Conclusion• We 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.

Page 16: How to test models using php unit testing framework?

Any?