Download the PHP package buildotter/php-core without Composer
On this page you can find all versions of the php package buildotter/php-core. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download buildotter/php-core
More information about buildotter/php-core
Files in buildotter/php-core
Package php-core
Short Description Buildotter Core: Foundation to implement the Test Data Builder Pattern
License GPL-3.0-or-later
Homepage https://github.com/buildotter/php-core
Informations about the package php-core
Buildotter Core
Foundation to implement the Test Data Builder Pattern in PHP.
Installation
What's the Test Data Builder Pattern?
Creating and managing test data is a critical aspect of ensuring the quality and reliability of an application.
The Test Data Builder Pattern is a technique for simplifying the process of constructing test data, making tests more maintainable and readable.
It provides a structured way to create complex and consistent test data for your test cases.
This pattern isolate the process of creating test data from the actual test logic.
By doing so, it promotes separation of concerns and helps you write cleaner and more focused tests.
It helps for the following problems:
- How to decouple object creation from implementation in your tests?
- How to improve code readability in your tests?
- How to speed up writing your test setup?
The 4 rules to keep in mind are:
- Have a property for each constructor parameter
- Initialize its properties to commonly used or safe values
- Have a build method that creates a new object using the values in its properties
- Have chainable public methods for overriding the values in its properties
To know more about the Test Data Builder Pattern, you can read the following articles:
- Test Data Builders: an alternative to the Object Mother pattern: we encourage you to read the whole series
- Xtrem T.D.D's explanation of Test Data Builders
- Are You Using Mocks When You Should Be Using Test Data Builders?
Why do we need libraries to implement the Test Data Builder Pattern?
We don't.
It is more about helping with repetitive tasks, speed up the process of creating test data builders and being focused on the test logic itself.
With time passing, we gathered parts that are common to projects. So why not share them?
Furthermore, it helps to have a common foundation for the adoption of the pattern in a team.
Usage
We tried not to be too opinionated about the foundation here.
For instance, we like to use functions like anElephant()
instead of ElephantBuilder::new()
(we find it more readable and allow us to focus on what is important) but nothing
forces you to do the same.
The main foundation: Buildotter\Core\Buildatable
interface
You may found the name weird, it is a mix of "build" and "data" with the suffix "-able". "-able" is used to form adjectives from verbs, with the meaning "capable of, fit for, tending to". With this name, we want to express that the object is capable of building data as a test data builder.
The Buildotter\Core\Buildatable::new()
method is the named constructor to create a new instance of the class with commonly used or safe values.
Imagine that you develop a dating app, you know the age of your customers.
Most of the time you don't mind about the exact age, you just need one respecting the invariants of your domain
(see Propery-Based Testing for more about this).
It is less likely that a customer is 300 or 10 years old than between 18 and 60 years old for instance.
It means that a commonly used or safe value for the age of a customer is between 18 and 60 years old.
So instead of choosing an arbitrary value, you may use a random value between 18 and 60 years old.
The Buildotter\Core\Buildatable::build()
method to create a new object using the values in its properties.
A ::with()
method with Buildotter\Core\BuildableWithXXX
traits
We provide some traits that already implement a ::with()
method.
Buildotter\Core\BuildableWithArgUnpacking
allows you to use named arguments to set the properties of the builder.
Buildotter\Core\BuildableWithArray
allows you to set properties of the builder by passing an array as its argument.
You may prefer one or the other depending on your preferences. You may choose to use both. You may choose to not use them at all.
Build multiple objects
You may need multiple objects of the same type at once.
Buildotter\Core\Many
static methods are here to help you.
Example
Imagine you have a class Elephant
with the following constructor:
You may create a test data builder for this class like this:
Feel free to create functions like anElephant()
and someTopics()
to make your tests more readable.
For instance, we may have a file containing the following functions:
And then, during testing:
More
To generate test data builders, you may use the Buildotter Maker Standalone. Instead, you may prefer to use PHPStorm Live templates. Or combine them.
Question: Do you think that it could be helpful to share some live templates that helps for testing?
Contributing
See the contributing guide.
TODOs
- [ ] provide a symfony bridge in a dedicated package