Download the PHP package noj/fabrica without Composer
On this page you can find all versions of the php package noj/fabrica. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fabrica
Fabrica
Fabrica handles the definition and creation of objects for use in testing.
Coupled with its Doctrine integration, Fabrica allows you to fully test CRUD logic without mocking. See the What Problem Does it Solve? page for a full explanation.
- Installing
- Usage
- Setup
- Basic Usage
- Setters
- Overrides
- Create Multiple
- Relations
- Overriding Relation Properties
- Entity Types
- Extending
- Doctrine Integration
- Refreshing the Database Between Tests
- PHPUnit Assertions
- Faker Integration
- Inspirations
Installing
Usage
Setup
First initialise Fabrica somewhere within your test suite. For PHPUnit, this can be done using the bootstrap option:
Basic Usage
Assuming that you have a User
entity, create a new factory factories/UserFactory.php
:
Now within your test, you can create a new User
instance:
Setters
If your entity's properties are only accessible through setters then you can use the @
syntax to call methods instead:
Given that a value is an array, you can indicate that you wish the method to be called for each item using the multiple times suffix (*
):
Overrides
You can override any default values when creating your entity:
Create Multiple
You can create multiple entities like so:
Relations
You can automatically create related entities. For example, if you have Comment
entity that belongs to a User
then you can define a factory:
Whenever a Comment
is created it will have an associated User
:
You can also define the inverse side of the relation. For example, you can define that each created User
should have an associated Comment
:
You can create multiple child relations:
Will create a User
with 3 Comments
.
If the entity has a property that depends on the relation then you can define this like so:
Overriding Relation Properties
You can also override properties of nested relations when creating an entity:
For a single entity of a one-to-many relation:
Or even every entity:
Entity Types
Rather than always having to pass overrides when creating entities, you can define different types of entities:
Extending
If a sub-type shares attributes with the parent-type then you can specify that the factory extends from it:
You can also extend from a sub-type:
Doctrine Integration
Fabrica ships with a Doctrine adapter that will automatically persist your entities on creation. Simply configure the store in your bootstrap file:
Where the EntityManager comes from and how it is configured may depend on your application.
Fabrica provides a simple way of creating an annotation backed EntityManager using in-memory SQLite if that meets your requirements:
Refreshing the Database Between Tests
Most likely you will want to reset the state of your database before each test runs. There are 2 ways of doing this:
-
If you are using PHPUnit 7.5 or above then you can add the following to your
phpunit.xml
which will reset your database between each test: - If you are using a lower version of PHPUnit or you would only like to create the database for specific tests then you can add the trait to your test class:
PHPUnit Assertions
Fabrica ships with a set of PHPUnit assertions for validating the state of the database during a test.
This provides the following assertions:
assertDatabaseContainsEntity(string $class, array $criteria = [])
assertDatabaseContainsEntities(string $class, int $amount, array $criteria = [])
assertDatabaseContainsExactlyOneEntity(string $class, array $criteria = [])
assertDatabaseDoesNotContainEntity(string $class, array $criteria = [])
Note: If you are using NeedsDatabase
described above then the assertions are already included.
Example usage:
Faker Integration
Fabrica can be configured with a faker instance to help generate fake data in your entity definitions.
You will then receive the faker instance in the define callback: