The presentation explains the PHP unit testing framework and provides a basic training on Testing with PHPunit. It Covers fundamental of unit testing, unit testing benefits and best practices. Starting ground up from PHP unit fundamentals, features of unit testing tries to explore advanced topics like Data providers, Fuxtures, Test doubles and more...
phpunit is testing framework for php testing.
A browser is able to give the syntax error but what if in case of some logical error.
Phpunit testing enables you to check your logic.
It is fully object oriented framework.
It also can be enabled in Netbeans.
you can find the option of phpunit testing in Netbeans
This presentation would help learn how to install, integrate, write automated test script with PHPUnit.Would also involve looking into different example and execute them.
phpunit is testing framework for php testing.
A browser is able to give the syntax error but what if in case of some logical error.
Phpunit testing enables you to check your logic.
It is fully object oriented framework.
It also can be enabled in Netbeans.
you can find the option of phpunit testing in Netbeans
This presentation would help learn how to install, integrate, write automated test script with PHPUnit.Would also involve looking into different example and execute them.
TestNG is a testing framework inspired from JUnit and NUnit, which can be used as a core unit test framework for Java project.
Demo: https://github.com/bethmi/testng-demo
API testing is a type of software testing that involves testing application programming interfaces (APIs) directly and as part of integration testing to determine if they meet expectations for functionality, reliability, performance, and security.
TestNG is a testing framework inspired from JUnit and NUnit, which can be used as a core unit test framework for Java project.
Demo: https://github.com/bethmi/testng-demo
API testing is a type of software testing that involves testing application programming interfaces (APIs) directly and as part of integration testing to determine if they meet expectations for functionality, reliability, performance, and security.
Automated Testing in WordPress, Really?!Ptah Dunbar
Did you know that WordPress has an automated test suite? It contains well over 1500 integration tests and growing. However one of the primary culprits of WordPress is in the quality of its plugins. Most plugins don't have an automated test suite you can run to verify all features are working as expected, and fail gracefully.
In this talk, Ptah will introduce you to automated testing in WordPress using PHPUnit. We will cover concepts like unit testing, integration testing and end-to-end testing with examples in WordPress. You will leave the talk equipped with practical knowledge and ready to start adding an automated test suite to your plugins.
For the first time ever, there are four generations (Traditionals, Baby Boomers, Generation X and Millenials) in the workplace. This can be the root of many communication issues, ranging from employee interaction to job seekers interfacing with younger hiring managers and recruiters. In this presentation, I attempt to shed some light on generational characteristics as well as share relevant communication tips. Thanks to all who attended the event and requested the content. Any questions, let me know in the comments below or by contacting me (info on the last slide). I am also considering creation of a companion video for this presentation; LMK what you think.
Test automation principles, terminologies and implementationsSteven Li
A general slides for test automation principle, terminologies and implementation
Also, the slides provide an example - PET, which is a platform written by Perl, but not just for Perl. It provides a general framework to use.
Developer testing 101: Become a Testing FanaticLB Denker
In this workshop we will cover the methodologies and three basic levels of testing, then we will deep dive into how to use PHPUnit to achieve developer testing. The tests may not be the prettiest, most robust, or efficient, but you should leave the course with the ability and confidence to write tests for your code.
Topics include: xUnit framework basics and workflows, test classification, asserts, data driven testing, and an introduction to mocking.
This is a beginner course, but seasoned veterans may discover features they never knew.
Principles and patterns for test driven developmentStephen Fuqua
Developed to help introduce key topics in Test Driven Development, for new and veteran developers alike. Some examples are language-specific (C# / MSTest / Moq), but the principles apply to any object oriented language.
Software testing
Developers Belief on Software Testing
Developers Responsibility for testing
Test writing methods
State based testing
Behavioural/interaction based testing
Writing a Testable Code
Flaw 1 - Constructor does Real Work
Flaw 2 - API lies about it's real dependencies
Flaw 3 - Brittle Global State & Singletons
Testing Frameworks and tools for Java...
Mockito and PowerMock...
Testing Models
Stubs Based Testing Model
Mocked Objects Based Testing Model
JUit 4.+ and TestNG
https://www.adroitlogic.com
https://developer.adroitlogic.com
We investigate one of the most popular approaches to creating software: test driven development. From the basic understanding why tests are important to a new software development paradigm, where you start with tests and them do the implementation. We glance over different areas of testing and see how one should really do the software testing in different situation.
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
Worried about document security while sharing them in Salesforce? Fret no more! Here are the top-notch security standards XfilesPro upholds to ensure strong security for your Salesforce documents while sharing with internal or external people.
To learn more, read the blog: https://www.xfilespro.com/how-does-xfilespro-make-document-sharing-secure-and-seamless-in-salesforce/
In software engineering, the right architecture is essential for robust, scalable platforms. Wix has undergone a pivotal shift from event sourcing to a CRUD-based model for its microservices. This talk will chart the course of this pivotal journey.
Event sourcing, which records state changes as immutable events, provided robust auditing and "time travel" debugging for Wix Stores' microservices. Despite its benefits, the complexity it introduced in state management slowed development. Wix responded by adopting a simpler, unified CRUD model. This talk will explore the challenges of event sourcing and the advantages of Wix's new "CRUD on steroids" approach, which streamlines API integration and domain event management while preserving data integrity and system resilience.
Participants will gain valuable insights into Wix's strategies for ensuring atomicity in database updates and event production, as well as caching, materialization, and performance optimization techniques within a distributed system.
Join us to discover how Wix has mastered the art of balancing simplicity and extensibility, and learn how the re-adoption of the modest CRUD has turbocharged their development velocity, resilience, and scalability in a high-growth environment.
Code reviews are vital for ensuring good code quality. They serve as one of our last lines of defense against bugs and subpar code reaching production.
Yet, they often turn into annoying tasks riddled with frustration, hostility, unclear feedback and lack of standards. How can we improve this crucial process?
In this session we will cover:
- The Art of Effective Code Reviews
- Streamlining the Review Process
- Elevating Reviews with Automated Tools
By the end of this presentation, you'll have the knowledge on how to organize and improve your code review proces
How to Position Your Globus Data Portal for Success Ten Good PracticesGlobus
Science gateways allow science and engineering communities to access shared data, software, computing services, and instruments. Science gateways have gained a lot of traction in the last twenty years, as evidenced by projects such as the Science Gateways Community Institute (SGCI) and the Center of Excellence on Science Gateways (SGX3) in the US, The Australian Research Data Commons (ARDC) and its platforms in Australia, and the projects around Virtual Research Environments in Europe. A few mature frameworks have evolved with their different strengths and foci and have been taken up by a larger community such as the Globus Data Portal, Hubzero, Tapis, and Galaxy. However, even when gateways are built on successful frameworks, they continue to face the challenges of ongoing maintenance costs and how to meet the ever-expanding needs of the community they serve with enhanced features. It is not uncommon that gateways with compelling use cases are nonetheless unable to get past the prototype phase and become a full production service, or if they do, they don't survive more than a couple of years. While there is no guaranteed pathway to success, it seems likely that for any gateway there is a need for a strong community and/or solid funding streams to create and sustain its success. With over twenty years of examples to draw from, this presentation goes into detail for ten factors common to successful and enduring gateways that effectively serve as best practices for any new or developing gateway.
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTier1 app
Even though at surface level ‘java.lang.OutOfMemoryError’ appears as one single error; underlyingly there are 9 types of OutOfMemoryError. Each type of OutOfMemoryError has different causes, diagnosis approaches and solutions. This session equips you with the knowledge, tools, and techniques needed to troubleshoot and conquer OutOfMemoryError in all its forms, ensuring smoother, more efficient Java applications.
top nidhi software solution freedownloadvrstrong314
This presentation emphasizes the importance of data security and legal compliance for Nidhi companies in India. It highlights how online Nidhi software solutions, like Vector Nidhi Software, offer advanced features tailored to these needs. Key aspects include encryption, access controls, and audit trails to ensure data security. The software complies with regulatory guidelines from the MCA and RBI and adheres to Nidhi Rules, 2014. With customizable, user-friendly interfaces and real-time features, these Nidhi software solutions enhance efficiency, support growth, and provide exceptional member services. The presentation concludes with contact information for further inquiries.
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Anthony Dahanne
Les Buildpacks existent depuis plus de 10 ans ! D’abord, ils étaient utilisés pour détecter et construire une application avant de la déployer sur certains PaaS. Ensuite, nous avons pu créer des images Docker (OCI) avec leur dernière génération, les Cloud Native Buildpacks (CNCF en incubation). Sont-ils une bonne alternative au Dockerfile ? Que sont les buildpacks Paketo ? Quelles communautés les soutiennent et comment ?
Venez le découvrir lors de cette session ignite
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
Data privacy is one of the most critical issues that businesses face. This presentation shares insights on the principles and best practices for ensuring the resilience and security of your workload.
Drawing on a real-life project from the HR industry, the various challenges will be demonstrated: data protection, self-healing, business continuity, security, and transparency of data processing. This systematized approach allowed to create a secure AWS cloud infrastructure that not only met strict compliance rules but also exceeded the client's expectations.
Developing Distributed High-performance Computing Capabilities of an Open Sci...Globus
COVID-19 had an unprecedented impact on scientific collaboration. The pandemic and its broad response from the scientific community has forged new relationships among public health practitioners, mathematical modelers, and scientific computing specialists, while revealing critical gaps in exploiting advanced computing systems to support urgent decision making. Informed by our team’s work in applying high-performance computing in support of public health decision makers during the COVID-19 pandemic, we present how Globus technologies are enabling the development of an open science platform for robust epidemic analysis, with the goal of collaborative, secure, distributed, on-demand, and fast time-to-solution analyses to support public health.
Cyaniclab : Software Development Agency Portfolio.pdfCyanic lab
CyanicLab, an offshore custom software development company based in Sweden,India, Finland, is your go-to partner for startup development and innovative web design solutions. Our expert team specializes in crafting cutting-edge software tailored to meet the unique needs of startups and established enterprises alike. From conceptualization to execution, we offer comprehensive services including web and mobile app development, UI/UX design, and ongoing software maintenance. Ready to elevate your business? Contact CyanicLab today and let us propel your vision to success with our top-notch IT solutions.
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus
As part of the DOE Integrated Research Infrastructure (IRI) program, NERSC at Lawrence Berkeley National Lab and ALCF at Argonne National Lab are working closely with General Atomics on accelerating the computing requirements of the DIII-D experiment. As part of the work the team is investigating ways to speedup the time to solution for many different parts of the DIII-D workflow including how they run jobs on HPC systems. One of these routes is looking at Globus Compute as a way to replace the current method for managing tasks and we describe a brief proof of concept showing how Globus Compute could help to schedule jobs and be a tool to connect compute at different facilities.
Check out the webinar slides to learn more about how XfilesPro transforms Salesforce document management by leveraging its world-class applications. For more details, please connect with sales@xfilespro.com
If you want to watch the on-demand webinar, please click here: https://www.xfilespro.com/webinars/salesforce-document-management-2-0-smarter-faster-better/
Software Engineering, Software Consulting, Tech Lead.
Spring Boot, Spring Cloud, Spring Core, Spring JDBC, Spring Security,
Spring Transaction, Spring MVC,
Log4j, REST/SOAP WEB-SERVICES.
Enhancing Research Orchestration Capabilities at ORNL.pdfGlobus
Cross-facility research orchestration comes with ever-changing constraints regarding the availability and suitability of various compute and data resources. In short, a flexible data and processing fabric is needed to enable the dynamic redirection of data and compute tasks throughout the lifecycle of an experiment. In this talk, we illustrate how we easily leveraged Globus services to instrument the ACE research testbed at the Oak Ridge Leadership Computing Facility with flexible data and task orchestration capabilities.
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...informapgpstrackings
Keep tabs on your field staff effortlessly with Informap Technology Centre LLC. Real-time tracking, task assignment, and smart features for efficient management. Request a live demo today!
For more details, visit us : https://informapuae.com/field-staff-tracking/
Unleash Unlimited Potential with One-Time Purchase
BoxLang is more than just a language; it's a community. By choosing a Visionary License, you're not just investing in your success, you're actively contributing to the ongoing development and support of BoxLang.
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Globus
The U.S. Geological Survey (USGS) has made substantial investments in meeting evolving scientific, technical, and policy driven demands on storing, managing, and delivering data. As these demands continue to grow in complexity and scale, the USGS must continue to explore innovative solutions to improve its management, curation, sharing, delivering, and preservation approaches for large-scale research data. Supporting these needs, the USGS has partnered with the University of Chicago-Globus to research and develop advanced repository components and workflows leveraging its current investment in Globus. The primary outcome of this partnership includes the development of a prototype enterprise repository, driven by USGS Data Release requirements, through exploration and implementation of the entire suite of the Globus platform offerings, including Globus Flow, Globus Auth, Globus Transfer, and Globus Search. This presentation will provide insights into this research partnership, introduce the unique requirements and challenges being addressed and provide relevant project progress.
Large Language Models and the End of ProgrammingMatt Welsh
Talk by Matt Welsh at Craft Conference 2024 on the impact that Large Language Models will have on the future of software development. In this talk, I discuss the ways in which LLMs will impact the software industry, from replacing human software developers with AI, to replacing conventional software with models that perform reasoning, computation, and problem-solving.
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Globus
Large Language Models (LLMs) are currently the center of attention in the tech world, particularly for their potential to advance research. In this presentation, we'll explore a straightforward and effective method for quickly initiating inference runs on supercomputers using the vLLM tool with Globus Compute, specifically on the Polaris system at ALCF. We'll begin by briefly discussing the popularity and applications of LLMs in various fields. Following this, we will introduce the vLLM tool, and explain how it integrates with Globus Compute to efficiently manage LLM operations on Polaris. Attendees will learn the practical aspects of setting up and remotely triggering LLMs from local machines, focusing on ease of use and efficiency. This talk is ideal for researchers and practitioners looking to leverage the power of LLMs in their work, offering a clear guide to harnessing supercomputing resources for quick and effective LLM inference.
3. Introduction to Unit Testing
What is Unit Testing?
• A test is code that executes another piece of code (function/ method) in a known
context with knunit own inputs, and compares the output to the expected values.
• A method of testing that verifies the individual units of source code are working
properly
• Unit Testing is the testing of units (function or method). The tests are done to ensure
that each unit is working like it is intended to work.
Key Features of Unit Testing
• Automated testing
• Conducted at Regular Intervals
• Tests independent of each other
• Tests functional behavior – expected results & unexpected failures
4. Why Unit Testing – The Benefits
Benefits of Unit Testing:
• Think ahead of coding – understand the behavior before
writing code
• Test code on functionality and identify issues early
• Being automated, it saves overall effort and repetitive work
• Progress indication of the project
• Alerts generation for monitoring tools e.g. CI
• Facilitates code refactoring (change existing code without
changing behavior)
• Good documentation of how code works
Note: Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure. It is
a disciplined way to clean up code that minimizes the chances
of introducing bugs
5. Features of Good Unit Test
• Independent: Each test needs to run independently from
other tests and environments.
• Fast: To have useful tests and be able to run them as often
as possible, tests need to be fast.
• Repeatable: You should be able to run a test as many times
as you want with the same result.
• Up to date: Tests are written once, but code can be changed
or extended. Whenever this happens, tests should be
updated accordingly.
• Short: Tests should be just a few lines— easy to read and
understand.
• Resilient: Once written, tests shouldn't change till the
behavior of tested class/ method changes.
6. Writing Unit Tests – Some Guidelines
• When to write tests
› TDD or TFD: Write tests before writing any code.
› Write tests just after writing a class or method.
• How to write tests
› Test classes not methods – behavior is the key
› Do not write tests that do not test anything
› Do not write tests that test too much
› Exploit dependencies between tests
› Use the most specific assertion available to express what you
want to test
› Decouple test code from test data
7. PHPUnit
• Part of xUnit familiy (JUnit, SUnit,...)
• Created by Sebastian Bergmann
• Developed in PHP
• Initially released in March 2004
• Current release 5.3
• Integrated/supported by IDEs
› Zend Studio
› Zend Framework
› Netbeans
› Eclipse PDT
› PhpStorm
9. Anatomy of PHP Unit Test
• Test Classes
› Every PHP Unit test case is a class.
› The class name should end with Test.
› Every Test class inherits (most of the time) from
phpunitframeworkTestCase (or
PHPUnit_Framework_TestCase)
› Names and hierarchy of test classes should mirror the tested
class, with difference of ‘test’ in test class name.
• Test Methods
› The tests are public methods that are named test*.
› Alternatively, you can use the @test annotation in a method's
docblock to mark it as a test method.
› Inside the test methods, assertion methods such as
assertEquals() are used to assert that an actual value
matches an expected value.
10. Assertions
• An assertion is the heart and soul of unit testing.
• It is an assertion that a given value suits particular
constraint defined by the assert statement.
• The most basic assertions
› assertTrue(): This verifies that a condition is true
› assertFalse(): This verifies that a condition is false
› assertEquals(): This verifies that expected and actual values are
equal, the same way as the PHP comparison operator = =
› assertSame(): This is similar to assertEquals(), but it checks
whether values are identical, the same way as the = = = operator
› assertNull(): This verifies that value is null
› assertEmpty(): This verifies that value is empty, but it uses the PHP
function empty(), which means empty can be false, null, '', array()
• Complete list:
https://phpunit.de/manual/current/en/appendixes.assertion
s.html
11. Annotations
• An annotation is doc block comment used to annotate
source code.
• PHPUnit uses this information at runtime to configure
runtime behaviour.
• A doc comment in PHP must start with /** and end with
*/
• Examples:
• @backupGlobals
• @backupStaticAttributes
• @dataProvider
• @depends
12. Command line Runner
• Phpunit – command to run unit tests
• Usage:
› phpunit [options] UnitTest [UnitTest.php]
› phpunit [options] <directory>
• Examples
› phpunit UnitTest: Runs the tests that are provided by the
class UnitTest that inherits from phpunitframeworkTestCase
› phpunit UnitTest UnitTest.php: Runs the tests that are
provided by the class UnitTest. This class is expected to be
declared in the specified sourcefile.
• More options:
https://phpunit.de/manual/current/en/textui.html
13. Test Dependencies
• PHPUnit supports the declaration of explicit dependencies between test
methods.
• PHPUnit supports the declaration of explicit dependencies between test
methods.
• Dependencies do not define the order in which the test methods are to
be executed
• They allow the returning of an instance of the test fixture by a
producer and passing it to the dependent consumers.
• A producer is a test method that yields its unit under test as return
value.
• A consumer is a test method that depends on one or more producers
and their return values.
• PHPUnit uses @depends annotation to express dependencies.
• When a producer returns an object a reference to that object is passed
to the consumers.
• When a copy should be used instead of a reference then @depends
clone should be used instead of @depends.
• Example:
14. Data Providers
• Data Providers are used to pass arguments to methods that
need arbitrary arguments.
• Name of data provider method is specified using the
@dataProvider annotation.
• A data provider method must be public and
› either return an array of arrays or
› an object that implements the Iterator interface and yields an array
for each iteration step.
• All data providers are executed before both the call to the
setUpBeforeClass static method and the first call to the
setUp method.
Note: Iterator is an Interface for external iterators or objects that can be
iterated themselves internally.
15. Exercise – Testing ATM Class
• Create a simple ATM class with methods deposit
(deposit cash), withdraw (withdraw cash),
currentBalance (show current balance) and write unit
tests for the class.
• Use different basic assertions in tests
• Use @depends and @dataProvider options for
dependency and argument data
16. Testing Exceptions
• expectException() method is used to test if an exception is
thrown by the code under test.
• In addition expectExceptionCode(),
expectExceptionMessage(), and
expectExceptionMessageRegExp() methods exist to set up
expectations for exceptions raised by the code under test
• Alternatively, the annotations @expectedException,
@expectedExceptionCode, @expectedExceptionMessage,
and @expectedExceptionMessageRegExp can be used.
• Standard PHP exceptions:
http://php.net/manual/en/spl.exceptions.php
• Be as specific as possible when testing exceptions. Testing
for classes that are too generic might lead to undesirable
side-effects.
17. Testing PHP Errors
• PHP errors can be tested using exceptions as below:
• @expectedException PHPUnit_Framework_Error
• For notices and errors use
PHPUnit_Framework_Error_Notice and
PHPUnit_Framework_Error_Warning respectively
• Use error suppression while testing functions that
trigger errors like fopen
18. Testing Output
• PHPUnit uses PHP's Output Buffering feature to provide
the functionality of testing outputs of functions like
‘echo’ or ‘print’
• Following methods are used:
Method Meaning
void expectOutputRegex(string
$regularExpression)
Set up the expectation that the output
matches a$regularExpression.
void expectOutputString(string
$expectedString)
Set up the expectation that the output is
equal to an$expectedString.
bool setOutputCallback(callable $callback) Sets up a callback that is used to, for
instance, normalize the actual output.
19. Error Output & Edge Cases
• PHPUnit tries to be as specific as possible in reporting
issues when assertion fails.
• Uses textual representation of inputs for comparison
• This may result in reporting more issues / differences
than actual.
• Whenever a test fails PHPUnit tries its best to provide
you with as much context as possible that can help to
identify the problem but it may not be complete.
• E.g. when comparing long arrays, not all differences
may be reported.
• https://phpunit.de/manual/current/en/writing-tests-for-
phpunit.html#writing-tests-for-phpunit.exceptions
20. Fixtures
• When testing, we need to set the environment up to a
known state and cleanup when the test is complete.
• This known state is called the fixture of the test.
• As number and complexity of tests increases, fixture of
the test starts becoming cumbersome.
• It also leads to writing duplicate code for setting up and
cleaning objects.
• PHPUnit provides two options for defining fixtures:
› Set it before each test case class, runs once before and/ or
after each test case
› Set it before each test method, runs before each test and/ or
after each test
21. Fixtures - Before & After each method
• PHPUnit provides methods setUp() and tearDown() that
setup and clean objects before / after each test
method.
• The setUp() method is called before every test is
executed.
• The tearDown() method is opposite to setUp(), and it is
called when the test finishes.
• We need to implement tearDown() only if we have
allocated external resources like files or sockets in
setUp().
• If you have a bug in the code causing a fatal error,
tearDown() will not be called.
22. Fixtures – Before and After Class
• There are two other template methods:
setUpBeforeClass() and tearDownAfterClass()
• These are called before the first test of the test case
class is run and after the last test of the test case class
is run, respectively
• These methods can be used for setting resources that
need to be shared across test methods e.g. database
connections.
• Other considerations
› Variations in fixtures – ideally all tests in a case should have
same fixture
› Sharing fixtures – Generally not a good idea.. except for
specific cases
23. Global State
• Global state includes global variable like: $_GLOBALS, $_SERVER,
$_POST etc.
• It also includes static attributes of classes are also part of the
global state
• Undesirable effects of depending on global state:
› Unwanted side effects: One piece of code changes global state, while
another piece not expecting this change, behaves differently
› Hidden dependencies: It is difficult to spot required dependency, which is a
high risk when changing the code
› Not clearly defined responsibility: It is difficult to figure out how to test
when code might change behavior depending on change, which is difficult to
predict and replicate
• By default, PHPUnit runs your tests in a way where changes to
global and super-global variables do not affect other tests.
However, PHPUnit will not store and restore your $_SESSION data.
• It can be optionally extended to static variables
24. Global State
• Annotations and properties can used to control the backup and
restore operations for global variables
• Annotations
› @backupGlobals (enabled/disabled)
› @backupStaticAttributes
• Properties
› protected $ backupGlobals = true;
› protected $ backupGlobalsBlacklist = array('variable');
› protected $ backupStaticAttributes = true;
• You can provide a blacklist of global variables that are to be
excluded from the backup and restore operations using
$backupGlobalsBlacklist property.
• Setting the $backupGlobalsBlacklist property inside e.g. the
setUp() method has no effect. Hence it should not be tried.
25. Test Dependencies & Dependency Injection
• Discussion on problems due to test dependencies and
how to resolve them..
26. Test Doubles
• PHPUnit offers ways to modify the code on the fly and create test doubles.
• This feature allows create a double of your code (class) – a simplified version
– in order to remove dependencies and reduce complexity.
• Dependency may be due to other classes, Databases or third party APIs
• Allows to focus on testing the isolated code,
• Types of doubles:
› Dummy: Just an empty shell which is not called or used in itself. It is used only when
we need to pass things such as required arguments.
› Fake: This imitates the real object functionality, but is written and used only for tests.
› Stub: This returns predefined values for the method that is called or null for other
methods. Sometimes, they are also called indirect input to the tests.
› Spy: This is similar to the stub. It just remembers returned values that can be verified
later.
› Mock: The simplest definition of this double is a stub with expectations. An expectation
is the specification of the method on when and how it should be called during a test
execution.
Note: Final, private and static methods cannot be stubbed or mocked. They are
ignored by PHPUnit's test double functionality and retain their original behavior.
27. Creating Test Doubles
PHPUnit offers two basic ways of creating test doubles, which are as follows:
• $double = $ this-> getMock(' MyClass'); // This is deprecated now and createMock()
should be used.
• $double = $ this-> getMockBuilder(' MyClass')-> getMock();
The getMock() method accepts eight different (10 in Version 4) parameters, which affects how the
double will be created. They are described as follows:
• string $originalClassName: This is a class name from which the double will be created.
• array $methods: This will replace all methods by default and will return Null. If the name of the
methods are passed in an array, then only these methods will be replaced and the original
methods stay untouched.
• array $arguments: These arguments are used for the original constructor.
• string $mockClassName: This indicates the class name for the created test double.
• boolean $callOriginalConstructor: This is used when you want to enable/ disable the original
constructor call.
• boolean $callOriginalClone: This is used when you want to enable/ disable the original clone
method usage.
• boolean $callAutoload: This is used when you want to disable __autoload() for loading classes.
• boolean $cloneArguments: This allows to enable/ disable cloning of all object parameters.
28. Creating Test Doubles
PHPUnit Version 4 added two more arguments for test proxies, which are as
follows:
• boolean $ callOriginalMethods: This argument allows to call the original
methods, when test double is used Object
• $proxyTarget: This calls the original object for the test proxy
• Both of the methods do the same thing— create a test double— just in slightly
different ways.
› With the getMockBuilder() instead of passing 10 arguments, you can use method chaining to
set them up, as shown in the following code snippet:
$ double = $ this-> getMockBuilder(' MyClass')
-> enableOriginalClone()
-> enableArgumentCloning()
-> getMock();
30. Test Double - Stubs
• Practice of replacing an object with a test double that (optionally) returns
configured return values is referred to as stubbing
• E.g. // Create a stub for the SomeClass class.
$stub = $this->createMock(SomeClass::class);
// Configure the stub.
$stub->method('doSomething')
->willReturn('foo'); // or will($this->returnValue(‘foo’));
$this->assertEquals('foo', $stub->doSomething());
• Stub can also be configured to return a array map instead of single return.
E.g. $stub->method('doSomething')
• ->will($this->returnValueMap($map)); // $map is array of expected
inputs and return values.
• Stub can be used to return calculated value using a callback function. E.g.
$stub->method('doSomething')
->will($this->returnCallback('str_rot13')); // pass function name
• Limitation: There should not be method named "method“ in original class
31. Test Doubles – Mocking Objects
• The practice of replacing an object with a test double that verifies expectations, for instance asserting that a
method has been called, is referred to as mocking.
• We use the expects() and with() methods to specify how the interaction with Arguments should look. E.g.
$mock->expects($this->once()) // to be called only once
->method('update')
->with($this->equalTo('something'));
• Expectation has a functionality that asserts when and how a method is called. Its basic structure has the
following elements:
› expects: This shows how many times a method is called e.g any, never, atLeastOnce, once etc.
› method: This calls a method name
› with: This shows what needs to be passed to the method
› will: This shows what the method will return
• The with() method can take any number of arguments, corresponding to the number of arguments to the
method being mocked.
• The withConsecutive() method can take any number of arrays of arguments, depending on the calls you want to
test against. e.g.
withConsecutive(
[$this->equalTo('foo'), $this->greaterThan(0)],
[$this->equalTo('bar'), $this->greaterThan(0)]
);
• The callback() constraint can be used for more complex argument verification.
32. Test Doubles - Example
• Testing a sample transaction class that takes an array
input and post data to API in XML format.
• Includes following classes:
› Transaction – the class to be tested
› Logger – class for logging API action
› HTTPClient – class for interacting with the API
33. Mocking Other Class & Objects Types
• Abstract Class and Traits: PHPUnit provide special methods below for
mocking Traits and Abstract classes:
› getMockForTrait() - This method returns a mock object that uses a specified
trait. All abstract methods of the given trait are mocked. This allows for testing
the concrete methods of a trait.
› getMockForAbstractClass() - This method returns a mock object for an abstract
class. All abstract methods of the given abstract class are mocked. This allows
for testing the concrete methods of an abstract class.
• Web Services: The method getMockFromWsdl() can be used just like
getMock() to mock SOAP Web Services. It requires SOAP extension
enabled in PHP
• FileSystem: vfsStream is a stream wrapper for a virtual filesystem that
may be helpful in unit tests to mock the real filesystem.
› To use, add a dependency on mikey179/vfsStream to your project's
composer.json
› vfsStream gives the test developer full control over what the filesystem
environment looks like to the tested code.
› Since the filesystem operations do not operate on the real filesystem anymore,
cleanup operations in a tearDown() method are no longer required.
› Documentation url: https://github.com/mikey179/vfsStream/wiki
34. Stages of Database Testing
• The four stages of a database test
› Set up fixture: Putting database into a known state.
› Exercise System Under Test
› Verify outcome: : Asserting contents of the database are same as expected.
› Teardown: Clean up the database.
• Fixture: A fixture describes the initial state your application and database are
in when you execute a test.
• Testing the database requires you to hook into at least the setup and
teardown to clean-up and write the required fixture data into your tables.
• General stages in a database test resemble the following workflow that is
executed for each single test:
› 1. Clean-Up Database: Execute a TRUNCATE against all the tables specified to reset
their status to empty, to ensure clean database.
› 2. Set up fixture: Iterate over all the fixture rows specified and insert them into their
respective tables.
› 3–5. Run Test, Verify outcome and Teardown: After the database is reset and loaded
with its initial state the actual test is executed by PHPUnit.
Note: The part of the test code does not require awareness of the Database
Extension at all, you can go on and test whatever you like with your code.
35. DB Unit – PHP Unit’s extension for Database Testing
• DBUnit is a PHPUnit extension for DB testing
• To start with, extend the abstract class:
PHPUnit_Extensions_Database_TestCase.
• In this testcase we implement two abstract methods:
› getConnection(): This provides a database connection
wrapped in PHPUnit class - abstracted across vendors through
the PDO library
› It allows the clean-up and fixture loading functionalities to
work.
› getDataSet(): The getDataSet() method defines how the
initial state of the database should look before each test is
executed.
• The getDataSet() method is called once during setUp()
to retrieve the fixture data-set and insert it into the
36. Data Sets and Data Tables
• DBUnit uses the concepts DataSet and DataTable:
› DataSet: represented by interface
PHPUnit_Extensions_Database_DataSet_IDataSet
› DataTable: represented by interface
PHPUnit_Extensions_Database_DataSet_IDataTable.
• The DataSet and DataTable are an abstraction layer around database
tables, rows and columns.
• It hides the underlying database contents in an object structure, which
can also be implemented by other non-database sources.
• This abstraction is used to compare the actual contents of a database
against the expected contents.
• Expectations can be represented as XML, YAML, CSV files or PHP array
for example.
• The DataSet and DataTable interfaces enable the comparison of the
conceptually different sources, emulating relational database storage.
• We can also implement our own datasets and tables if the available
one’s do not meet our requirements.
37. Data Set Types
• There are three different types of basic datasets / data tables:
• File-Based DataSets and DataTables
› Flat XML DataSet: createFlatXmlDataSet($filename)
› XML DataSet:There is another more structured XML dataset, which is a bit more
verbose to write but avoids the NULL problems of the Flat XML -
>createXmlDataSet($filename)
› MySQL XML DataSet:This new XML format is specific to the MySQL database server. can
be generated using the mysqldump utility -> createMySQLXMLDataSet($filename)
› YAML DataSet:This is simple, convient AND it solves the NULL issue that the similar Flat
XML dataset has. A NULL in YAML is just the column name without no value specified.
The YAML Dataset has no factory method on the Database TestCase currently, so you
have to instantiate it manually.
› CSV DataSet:
› Array DataSet: There is no Array based DataSet in PHPUnit's Database Extension (yet),
but we can implement our own easily
• Query-Based DataSet and DataTable: For database assertions you do not only
need the file-based datasets but also a Query/SQL based Dataset that
contains the actual contents of the database.
$ds = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this-
>getConnection());
$ds->addTable('guestbook', 'SELECT * FROM guestbook');
38. Data Set Types
• Database (DB) Dataset: a DataSet that consists of all the tables with their
content in the database specified as second parameter to the Connections
Factory method.
› $tableNames = ['guestbook'];
› $dataSet = $this->getConnection()->createDataSet($tableNames);
• Other Data Sets: The content of the basic datasets can be modified by other
datasets below, which use them as inputs and then perform some operations
before returning a modified dataset
› Replacement DataSet: The Replacement DataSet is a decorator for an existing dataset
and allows you to replace values in any column of the dataset by another replacement
value.
› DataSet Filter: If you have a large fixture file you can use the DataSet Filter for white-
and blacklisting of tables and columns that should be contained in a sub-dataset.
◦ Blacklisting or white listing of tables / columns is done using include / exclude statements.
◦ However, both exclude and exclude cannot be used together
› Composite DataSet: The composite DataSet is very useful for aggregating several
already existing datasets into a single dataset.
Beware of Foreign Keys: The getDataSet() method is called once during setUp()
to retrieve the fixture data-set and insert it into the database. During Fixture
SetUp PHPUnit's Database Extension inserts the rows into the database in the
order they are specified in your fixture. If your database schema uses foreign
keys this means you have to specify the tables in an order that does not cause
foreign key constraints to fail.
39. Organizing Tests
Unit Tests should be composable. We should be able to run
any number or combination of tests together.
• Composing a Test Suite Using the Filesystem
› The test case classes in the tests directory mirror the package and
class structure of the System Under Test (SUT) in the src directory.
› Tests will be run on the basis of directory where phpunit is pointed.
› For more fine-grained control of which tests to run we can use the -
-filter option.
› A drawback of this approach is that we have no control over the
order in which the tests are run.
• Composing a Test Suite Using XML Configuration.
› Test composition can also be defined in phpunit.xml configuration
file.
› If phpunit.xml or phpunit.xml.dist (in that order) exist in the
current working directory and --configuration is not used, the
configuration will be automatically read from that file.
› The order in which tests are executed can be made explicit.
40. Risky Tests
PHPUnit can be configured to filter out risky tests e.g. tests that test nothing,
unintentional coverage, output during execution etc. Following options are available:
Effect Command line
option
XML Configuration
A test that does not perform an assertion will be
marked as risky when this check is enabled
--report-useless-
tests
beStrictAboutTestsThatDoNotT
estAnything="true"
A test that is annotated with @covers and executes
code that is not listed using
a @covers or @uses annotation will be marked as
risky
--strict-coverage checkForUnintentionallyCovere
dCode="true"
A time limit can be enforced for the execution of a
test. Tests can be annotated as @small, @medium
or @large with pre configured timeout for each.
--enforce-time-limit beStrictAboutTestSize="true"
Tests that manipulate Global State. --strict-global-state beStrictAboutChangesToGlobal
State="true"
41. Incomplete and Skipped Tests
Incomplete Tests
• PHPUnit allows marking test as incomplete if it is not completely
implemented yet.
• Following methods are available:
› markTestIncomplete(): Marks the current test as incomplete.
› markTestIncomplete(string $message): Marks the current test as incomplete
using $message as an explanatory message.
Skipped Tests
• Allows marking tests as skipped.
› void markTestSkipped():Marks the current test as skipped.
› void markTestSkipped(string $message): Marks the current test as skipped
using $message as an explanatory message.
• Tests can be skipped for various reasons like any dependency,
incompleteness etc.
• It is also possible to use the @requires annotation to express common
preconditions for a test case and skipping if needed.
42. Unit Test Best Practices
• Use most specific assertion for a given case.
• Test method name should reflect what it is intended to test.
• Write small and precise tests to have fast test suite.
• Organize your tests well using either file system or
phpunit.xml config.
• Separate unit tests, functional tests and integration tests.
• Bootstrap only what you need
• Test classes not methods – observable behavior from
outside the class e.g.
› Return values
› Method calls to other objects
› Global state