Download the PHP package samuelgfeller/test-traits without Composer
On this page you can find all versions of the php package samuelgfeller/test-traits. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download samuelgfeller/test-traits
More information about samuelgfeller/test-traits
Files in samuelgfeller/test-traits
Package test-traits
Short Description A collection of PHPUnit test traits
License MIT
Homepage https://github.com/samuelgfeller/test-traits
Informations about the package test-traits
Test traits
This is a clone of selective/test-traits containing additional test traits, including a fixture trait to easily insert fixtures with test-case-relevant custom data and an SQL schema generator to set up the test database for integration test.
Requirements
- PHP 8.2+
- Composer
- PHPUnit 10+
Installation
Documentation
- Migration from selective/test-traits
- FixtureTestTrait
- HttpTestTrait
- RouteTestTrait
- MailerTestTrait
- SQL schema generation
Test environment setup:
Test Setup
Write tests using these traits:
Writing Tests
Migration from selective/test-traits
If you have been using the selective/test-traits
package and want to migrate to this library,
the following functions have to be renamed:
- The function
insertFixtures
(with "s") is nowinsertDefaultFixtureRecords
. - The old
insertFixture
function is nowinsertFixtureRow
andinsertFixture
is another function with offers a flexible way to insert fixtures with optional custom attributes.
Otherwise, the traits are the same and can be used in the same way.
This package will be kept in sync with selective/test-traits
.
FixtureTestTrait
A trait designed to create and insert fixtures with data that can be defined in the test function.
Provided method
Usage
Fixture
Each fixture must have a property $table
with the table name and an array $records
with the
default data to insert.
Insert fixture with custom attributes
To define custom data that should override the default values of the fixture class,
the insertFixture()
function can be used.
The first parameter is the fixture fully qualified class name e.g. UserFixture::class
and
the second (optional) is an array of attributes.
An array of attributes contains the data for one database row
e.g.:
['field_name' => 'value', 'other_field_name' => 'other_value']
.
Multiple attribute arrays can be passed to the function to insert multiple rows with different data as shown below.
Not all fields of the table need to be specified in the attribute array.
For unspecified fields, the values of the first $records
entry from the fixture will be used.
The function returns an array with the inserted data from the fixture including the auto-incremented id or an array for each row that was inserted when multiple rows were passed.
The FixtureTestTrait
uses the DatabaseTestTrait
for the interaction with the database.
HttpTestTrait
Requirements
- Any PSR-7 and PSR-17 factory implementation.
Provided methods
createRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
createFormRequest(string $method, $uri, array $data = null): ServerRequestInterface
createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
Usage
Creating a request with query string parameters:
RouteTestTrait
A Slim 4 framework router test trait.
Requirements
- A Slim 4 framework application
Provided method
Usage
Creating a request with a named route and query string parameters:
MailerTestTrait
Requirements: composer require symfony/mailer
DI container setup example:
Usage
PHPUnit test case:
License
This project is licensed under the MIT Licence — see the LICENCE file for details.
All versions of test-traits with dependencies
ext-pdo Version *