SlideShare a Scribd company logo
PHPUnit
Presented By: Polaki Viswanath
Kellton Tech
Welcome
About Me
1. Drupal developer.
2. Been coding in php for about 6 years.
3. And been using PHPUnit for around 1 year.
Goal of today’s talk
How to use PHPUnit with Drupal 7.
What is PHPUnit
PHPUnit is a programmer-oriented testing framework for PHP.
It is an instance of the xUnit architecture for unit testing frameworks.
Installation
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version
Or
Command: composer require --dev phpunit/phpunit ^3.7
Why to automate testing
1. Reduction in Development Time
2. Facilitates change
3. Simplifies integration
4. Enhance Design
5. Measure of Completion
What can you do with PHPUnit
1. Assertions
2. Stubs
3. Mocks
4. Annotations
Drupal Integration
Bootstrapping Drupal:
define('DRUPAL_ROOT', dirname(__FILE__) . '/../../../../..');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
require_once 'SageProductTest.php';
// Bootstrap Drupal.
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Drupal Integration cont...
Configuration xml:
<phpunit>
<php>
<server name="REMOTE_ADDR" value="127.0.0.1"/>
<server name="REQUEST_METHOD" value="GET"/>
</php>
</phpunit>
Demo
Some Assertions examples
$this->assertArrayHasKey('foo', ['bar' => 'baz']);
$this->assertClassHasAttribute('foo', stdClass::class);
$this->assertArraySubset(['config' => ['key-a', 'key-b']], ['config' => ['key-a']]);
$this->assertContains(4, [1, 2, 3]);
$this->assertContainsOnly('string', ['1', '2', 3]);
$this->assertContainsOnlyInstancesOf(
Foo::class,
[new Foo, new Bar, new Foo]
);
More assertions
$this->assertCount(0, ['foo']);
$this->assertDirectoryExists('/path/to/directory');
$this->assertDirectoryIsReadable('/path/to/directory');
$this->assertDirectoryIsWritable('/path/to/directory');
$this->assertEmpty(['foo']);
$this->assertEquals(1, 0);
$expected = new DOMElement('foo');
$actual = new DOMElement('bar');
$this->assertEqualXMLStructure($expected, $actual);
Questions
Thankyou :)

More Related Content

PDF
notes on Programming fundamentals
PPTX
Basic programming and data structure
PPT
Lsn02 fundamentals
PDF
What is cms_in_php
PPTX
PPTX
Behavior Driven Development
PDF
00 Prontuario inge 3016
PPTX
CS8251 programming in c | programming paradigm |
notes on Programming fundamentals
Basic programming and data structure
Lsn02 fundamentals
What is cms_in_php
Behavior Driven Development
00 Prontuario inge 3016
CS8251 programming in c | programming paradigm |

What's hot (6)

PDF
Pair programming and pair training
PPT
Intro To Programming Concepts
PPTX
Programming Fundamentals lecture 2
PPTX
1. importance of c
PPTX
GOOD PROGRAMMING
PPTX
introduction to programming
Pair programming and pair training
Intro To Programming Concepts
Programming Fundamentals lecture 2
1. importance of c
GOOD PROGRAMMING
introduction to programming
Ad

Similar to Drupal 7 with php unit (20)

PPTX
Php unit for drupal 8
PDF
13 PHPUnit #burningkeyboards
PDF
PHPUnit with Magento
PPTX
PHPUnit: from zero to hero
PDF
The State of PHPUnit
PDF
Fighting Fear-Driven-Development With PHPUnit
PDF
The state of PHPUnit
PDF
The State of PHPUnit
PDF
Unit testing symfony plugins with php unit
PDF
Unit Testing from Setup to Deployment
PDF
Leveling Up With Unit Testing - LonghornPHP 2022
PDF
Test all the things! Automated testing with Drupal 8
PDF
Leveling Up With Unit Testing - php[tek] 2023
PPTX
Unit Testing in PHP
PPTX
Getting started with PHPUnit
PPTX
PDF
PHPUnit your bug exterminator
PDF
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
PDF
Php unit (eng)
Php unit for drupal 8
13 PHPUnit #burningkeyboards
PHPUnit with Magento
PHPUnit: from zero to hero
The State of PHPUnit
Fighting Fear-Driven-Development With PHPUnit
The state of PHPUnit
The State of PHPUnit
Unit testing symfony plugins with php unit
Unit Testing from Setup to Deployment
Leveling Up With Unit Testing - LonghornPHP 2022
Test all the things! Automated testing with Drupal 8
Leveling Up With Unit Testing - php[tek] 2023
Unit Testing in PHP
Getting started with PHPUnit
PHPUnit your bug exterminator
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Php unit (eng)
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Assigned Numbers - 2025 - Bluetooth® Document
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
sap open course for s4hana steps from ECC to s4
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx

Drupal 7 with php unit

Editor's Notes

  • #4: xUnit is the collective name for several unit testing frameworks that derive their structure and functionality from Smalltalk's SUnit. SUnit, designed by Kent Beck in 1998, was written in a highly structured object-oriented style, which lent easily to contemporary languages such as Java and C#. Following its introduction in Smalltalk the framework was ported to Java by Kent Beck and Erich Gamma and gained wide popularity, eventually gaining ground in the majority of programming languages in current use. The names of many of these frameworks are a variation on "SUnit", usually replacing the "S" with the first letter (or letters) in the name of their intended language
  • #6: Reduction in Development Time With the help of PHPUnit testing, you can find issues early in the development cycle. This includes both bugs and missing parts of the specification for the unit. As you’ll write and run tests alongside development, issues will be picked up as soon as they occur, instead of being found at the end of the development process. The backtracking of the issues may be costly if found towards the end. Facilitates change We all know that changing code conveys a hazard. Will the new code work the same way as the old one? PHPUnit testing permits the programmer to refactor code or upgrade framework libraries at a later date, and ensure the module still works effectively. The methodology is to write test cases for all functions and methods so that at whatever point a change causes a fault, it can be immediately identified. Simplifies integration PHPUnit testing may reduce uncertainty in the units themselves and can be utilized as a part of a bottom-up testing approach. By testing the elements of a program first and then testing its subparts, integration testing becomes much easier. Enhance Design At the point when programming is developed using a test-driven approach, the combination of writing the PHPUnit test to determine the interface in addition to the refactoring activities performed after the test is passing, may replace formal design. Each unit test can be viewed as a design element specifying classes, methods, and perceptible behavior. When you include Mock Objects this can drive the design of multiple classes and their interactions with each other. It will enhance design because testable classes should essentially be more adaptable and decoupled. Measure of Completion If you’ve written your tests to cover every single required features and functionality of classes, and all are passing, then it’s reasonably safe to accept that you’ve completed the code successfully. If some time later, there might arise a need to add new feature to the application, in this scenario with agile practices the tests can not only drive design but also capture functional prerequisites. As per the saying “The only way to go fast is to go well” testing is inevitable aspect of effective application development. You can create something truly rapidly but without testing the technical depth, it might create drawbacks and each change to overcome these drawbacks will be very costly. PHPUnit frameworks empower you to write higher quality PHP code and empowers you to develop PHP web applications.
  • #7: Assertions these are what you add to your tests to ‘assert’ that something is valid, for example, an AssertFileExists would be used to check that a specified file exists what is expected. As a best practice, you would have one assertion for each test though there’s nothing to stop you having a few. Stubs If the unit of code you’re testing has a relied upon another object, you could use a ‘stub’ as a ‘test double’ to replace the original component. This allows you to focus on only testing of that one unit of code. You can configure stub techniques to return custom qualities. Mocks It is also used as a ‘test double’ similar like Stub, but to check that the test double object has received requests from the subject code as per defined expectations. Annotations An annotation is a unique type of syntactic metadata that can be added to the source code in PHP. It is used for annotating source code. For an instance @after annotation can be used to specify methods that should be called after each test method in a test case class. When we are writing a test in which we cannot (or chose not to) use a real depended-on component (DOC), we can replace it with a Test Double. The Test Double doesn't have to behave exactly like the real DOC; it merely has to provide the same API as the real one so that the SUT thinks it is the real one!
  • #11: Source: https://phpunit.de/manual/6.1/en/index.html