SlideShare a Scribd company logo
How to test models using
PHPUnit testing
framework?
Satej Kumar Sahu
Mindfire Solutions
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
Overall session coverage
and take aways from it.
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
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.
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 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.
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.
Techniques to test models
using PHPUnit
• DBUnit extension for database integration testing
• Mocks to unit test models
Getting bored with FACTS :(
• We are done with the boring FACTS stuff and now
into some practical coding :)
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()
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());
}
}
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.
Any?

More Related Content

What's hot (20)

PPTX
Automated php unit testing in drupal 8
Jay Friendly
 
PPT
Test Driven Development with PHPUnit
Mindfire Solutions
 
PDF
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
PPTX
Unit testing presentation
Arthur Freyman
 
PDF
New Features PHPUnit 3.3 - Sebastian Bergmann
dpc
 
PDF
Unit testing with PHPUnit - there's life outside of TDD
Paweł Michalik
 
ODP
Testing In Java
David Noble
 
PPTX
Tdd & unit test
GomathiNayagam S
 
PDF
Unit-testing and E2E testing in JS
Michael Haberman
 
PPTX
PHPUnit: from zero to hero
Jeremy Cook
 
PDF
Unit testing PHP apps with PHPUnit
Michelangelo van Dam
 
PPTX
Codeception
少東 張
 
PPTX
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
Jen Wong
 
PPT
Google mock for dummies
Harry Potter
 
PDF
How and what to unit test
Eugenio Lentini
 
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
PDF
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
PPTX
Unit Testing Presentation
nicobn
 
ODP
Embrace Unit Testing
alessiopace
 
PDF
Unit testing for WordPress
Harshad Mane
 
Automated php unit testing in drupal 8
Jay Friendly
 
Test Driven Development with PHPUnit
Mindfire Solutions
 
Introduction to Unit Testing with PHPUnit
Michelangelo van Dam
 
Unit testing presentation
Arthur Freyman
 
New Features PHPUnit 3.3 - Sebastian Bergmann
dpc
 
Unit testing with PHPUnit - there's life outside of TDD
Paweł Michalik
 
Testing In Java
David Noble
 
Tdd & unit test
GomathiNayagam S
 
Unit-testing and E2E testing in JS
Michael Haberman
 
PHPUnit: from zero to hero
Jeremy Cook
 
Unit testing PHP apps with PHPUnit
Michelangelo van Dam
 
Codeception
少東 張
 
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
Jen Wong
 
Google mock for dummies
Harry Potter
 
How and what to unit test
Eugenio Lentini
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
JAVASCRIPT Test Driven Development & Jasmine
Anup Singh
 
Unit Testing Presentation
nicobn
 
Embrace Unit Testing
alessiopace
 
Unit testing for WordPress
Harshad Mane
 

Viewers also liked (8)

PDF
AngularJS und TYP-D'oh!3
Christian Keuerleber
 
ODP
Breve introducción a TDD con Phpunit
moisesgallego
 
PPT
Testing persistence in PHP with DbUnit
Peter Wilcsinszky
 
PPT
PHP Unit-Testing With Doubles
Mihail Irintchev
 
PDF
Stub you!
Andrea Giuliano
 
PPTX
TDD Course (Spanish)
Pedro Ballesteros
 
PPT
Search Engine Optimization
Mindfire Solutions
 
PDF
B M Social Media Fortune 100
Burson-Marsteller
 
AngularJS und TYP-D'oh!3
Christian Keuerleber
 
Breve introducción a TDD con Phpunit
moisesgallego
 
Testing persistence in PHP with DbUnit
Peter Wilcsinszky
 
PHP Unit-Testing With Doubles
Mihail Irintchev
 
Stub you!
Andrea Giuliano
 
TDD Course (Spanish)
Pedro Ballesteros
 
Search Engine Optimization
Mindfire Solutions
 
B M Social Media Fortune 100
Burson-Marsteller
 
Ad

Similar to How to test models using php unit testing framework? (20)

PPTX
Coldbox developer training – session 4
Billie Berzinskas
 
PPTX
Refactoring Legacy Web Forms for Test Automation
Stephen Fuqua
 
PPTX
Unit tests and TDD
Roman Okolovich
 
PPTX
Unit Testing
Sergey Podolsky
 
PPT
Testing Frameworks
Moataz Nabil
 
PPTX
Unit Testing in Swift
GlobalLogic Ukraine
 
PDF
Build Java Web Application Using Apache Struts
weili_at_slideshare
 
PPTX
Unit Tests with Microsoft Fakes
Aleksandar Bozinovski
 
PPTX
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Aleksandar Bozinovski
 
PPTX
Testing mule
Sindhu VL
 
PPTX
Automated testing of ASP .Net Core applications
nispas
 
PPTX
Practical unit testing in c & c++
Matt Hargett
 
PPTX
Presentation
Igor Vlahek
 
PPTX
Unit Testing Full@
Alex Borsuk
 
PDF
How to Start Unit Testing in Laravel.pdf
Shiv Technolabs
 
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
PDF
Testing Angular
Lilia Sfaxi
 
PDF
Devday2016 real unittestingwithmockframework-phatvu
Phat VU
 
PPTX
MVC Framework
Ashton Feller
 
KEY
Testing w-mocks
Macon Pegram
 
Coldbox developer training – session 4
Billie Berzinskas
 
Refactoring Legacy Web Forms for Test Automation
Stephen Fuqua
 
Unit tests and TDD
Roman Okolovich
 
Unit Testing
Sergey Podolsky
 
Testing Frameworks
Moataz Nabil
 
Unit Testing in Swift
GlobalLogic Ukraine
 
Build Java Web Application Using Apache Struts
weili_at_slideshare
 
Unit Tests with Microsoft Fakes
Aleksandar Bozinovski
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Aleksandar Bozinovski
 
Testing mule
Sindhu VL
 
Automated testing of ASP .Net Core applications
nispas
 
Practical unit testing in c & c++
Matt Hargett
 
Presentation
Igor Vlahek
 
Unit Testing Full@
Alex Borsuk
 
How to Start Unit Testing in Laravel.pdf
Shiv Technolabs
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Erick M'bwana
 
Testing Angular
Lilia Sfaxi
 
Devday2016 real unittestingwithmockframework-phatvu
Phat VU
 
MVC Framework
Ashton Feller
 
Testing w-mocks
Macon Pegram
 
Ad

Recently uploaded (20)

PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Tally software_Introduction_Presentation
AditiBansal54083
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 

How to test models using php unit testing framework?

  • 1. How to test models using PHPUnit testing framework? Satej Kumar Sahu Mindfire Solutions
  • 2. 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
  • 3. Overall session coverage and take aways from it.
  • 4. 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
  • 5. 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.
  • 6. 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.
  • 7. Intro to MVC structure
  • 8. 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.
  • 9. 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.
  • 10. Techniques to test models using PHPUnit • DBUnit extension for database integration testing • Mocks to unit test models
  • 11. Getting bored with FACTS :( • We are done with the boring FACTS stuff and now into some practical coding :)
  • 13. 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()
  • 14. 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()); } }
  • 15. 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.
  • 16. Any?