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

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 |
PDF
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 |
notes on Programming fundamentals

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)

PPT
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
PPTX
PDF
PHPUnit with Magento
PDF
Codeception: introduction to php testing
PDF
Codeception: introduction to php testing (v2 - Aberdeen php)
PPT
Zend Framework 2 - PHPUnit
PDF
Cursus phpunit
PPT
build your own php extension
ODP
Professional PHP: an open-source alternative for enterprise development [Antw...
PDF
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
ODP
PDF
Behavior & Specification Driven Development in PHP - #OpenWest
PDF
Continuous Integration In Php
PDF
TDD for joomla extensions
PDF
Php7 tutorial
PDF
Php7 tutorial
PDF
Build & deploy PHP application (intro level)
PPTX
Web DEVELEOPMENT FULL Stack Chapter 10 1st Ed
PPTX
DDD (Debugger Driven Development)
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
PHPUnit with Magento
Codeception: introduction to php testing
Codeception: introduction to php testing (v2 - Aberdeen php)
Zend Framework 2 - PHPUnit
Cursus phpunit
build your own php extension
Professional PHP: an open-source alternative for enterprise development [Antw...
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Behavior & Specification Driven Development in PHP - #OpenWest
Continuous Integration In Php
TDD for joomla extensions
Php7 tutorial
Php7 tutorial
Build & deploy PHP application (intro level)
Web DEVELEOPMENT FULL Stack Chapter 10 1st Ed
DDD (Debugger Driven Development)
Ad

Recently uploaded (20)

PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Advanced IT Governance
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
madgavkar20181017ppt McKinsey Presentation.pdf
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
GamePlan Trading System Review: Professional Trader's Honest Take
Advanced IT Governance
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
Sensors and Actuators in IoT Systems using pdf
Spectral efficient network and resource selection model in 5G networks
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Bridging biosciences and deep learning for revolutionary discoveries: a compr...

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