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)

PDF
Unit testing in php
Sudar Muthu
 
PPTX
Test in action – week 1
Yi-Huan Chan
 
KEY
Developer testing 101: Become a Testing Fanatic
LB Denker
 
PDF
Unit Testing from Setup to Deployment
Mark Niebergall
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
PPTX
Getting started-php unit
mfrost503
 
PDF
Unit testing in PHP
Chonlasith Jucksriporn
 
PPT
Automated Unit Testing
Mike Lively
 
PPTX
Testy dymne, integracyjne i jednostkowe w Laravel
Laravel Poland MeetUp
 
PDF
Test driven development - Zombie proof your code
Pascal Larocque
 
PPTX
Test in action week 2
Yi-Huan Chan
 
PDF
Leveling Up With Unit Testing - php[tek] 2023
Mark Niebergall
 
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
PPT
PHP Unit Testing
Tagged Social
 
PPTX
Unit Testing talk
Sergei Kukharev
 
PPT
Unit testing
davidahaskins
 
PPTX
Test in action week 4
Yi-Huan Chan
 
PDF
Unit testing for WordPress
Sudar Muthu
 
KEY
Client Side Unit Testing
cloud chen
 
PPTX
An Introduction to unit testing
Steven Casey
 
Unit testing in php
Sudar Muthu
 
Test in action – week 1
Yi-Huan Chan
 
Developer testing 101: Become a Testing Fanatic
LB Denker
 
Unit Testing from Setup to Deployment
Mark Niebergall
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Getting started-php unit
mfrost503
 
Unit testing in PHP
Chonlasith Jucksriporn
 
Automated Unit Testing
Mike Lively
 
Testy dymne, integracyjne i jednostkowe w Laravel
Laravel Poland MeetUp
 
Test driven development - Zombie proof your code
Pascal Larocque
 
Test in action week 2
Yi-Huan Chan
 
Leveling Up With Unit Testing - php[tek] 2023
Mark Niebergall
 
Leveling Up With Unit Testing - LonghornPHP 2022
Mark Niebergall
 
PHP Unit Testing
Tagged Social
 
Unit Testing talk
Sergei Kukharev
 
Unit testing
davidahaskins
 
Test in action week 4
Yi-Huan Chan
 
Unit testing for WordPress
Sudar Muthu
 
Client Side Unit Testing
cloud chen
 
An Introduction to unit testing
Steven Casey
 
Ad

Recently uploaded (20)

PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
PDF
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
PPTX
declaration of Variables and constants.pptx
meemee7378
 
PDF
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
PPTX
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
PDF
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PDF
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
Introduction to web development | MERN Stack
JosephLiyon
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
declaration of Variables and constants.pptx
meemee7378
 
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 

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?