PHPackages                             samuelgfeller/test-traits - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. samuelgfeller/test-traits

ActiveLibrary[Testing &amp; Quality](/categories/testing)

samuelgfeller/test-traits
=========================

A collection of PHPUnit test traits

6.2.1(6mo ago)01.7k—0%1MITPHPPHP ^8.2CI passing

Since Mar 22Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/samuelgfeller/test-traits)[ Packagist](https://packagist.org/packages/samuelgfeller/test-traits)[ Docs](https://github.com/samuelgfeller/test-traits)[ RSS](/packages/samuelgfeller-test-traits/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (7)Versions (14)Used By (1)

Test traits
===========

[](#test-traits)

This is a clone of [selective/test-traits](https://github.com/selective-php/test-traits) containing additional test traits, including a [fixture trait](#fixturetesttrait) to easily insert fixtures with test-case-relevant custom data and an [SQL schema generator](https://github.com/samuelgfeller/slim-example-project/wiki/Test-Setup#generating-the-schema-file)to set up the test database for integration test.

[![Latest Version on Packagist](https://camo.githubusercontent.com/ba7cef1a21b389eb7b703c9fd02cf6e57a2d6520a0a9c4ce08fb69671c947d01/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73616d75656c6766656c6c65722f746573742d7472616974732e737667)](https://packagist.org/packages/samuelgfeller/test-traits)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/samuelgfeller/test-traits/workflows/build/badge.svg)](https://github.com/samuelgfeller/test-traits/actions)[![Total Downloads](https://camo.githubusercontent.com/2222fa2aef87c24f3c60a189a7cbbb67e21b6105cf3f8beba023a9f1894b24dc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73616d75656c6766656c6c65722f746573742d7472616974732e737667)](https://packagist.org/packages/samuelgfeller/test-traits/stats)

Requirements
------------

[](#requirements)

- PHP 8.2+
- Composer
- PHPUnit 10+

Installation
------------

[](#installation)

```
composer require samuelgfeller/test-traits --dev
```

Documentation
-------------

[](#documentation)

- [Migration from selective/test-traits](#migration-from-selectivetest-traits)
- [FixtureTestTrait](#FixtureTestTrait)
- [HttpTestTrait](#HttpTestTrait)
- [RouteTestTrait](#RouteTestTrait)
- [MailerTestTrait](#MailerTestTrait)
- [SQL schema generation](https://github.com/samuelgfeller/slim-example-project/wiki/Test-Setup#generating-the-schema-file)

Test environment setup: [**Test Setup**](https://github.com/samuelgfeller/slim-example-project/wiki/Test-Setup)
Write tests using these traits: [**Writing Tests**](https://github.com/samuelgfeller/slim-example-project/wiki/Writing-Tests)

Migration from selective/test-traits
------------------------------------

[](#migration-from-selectivetest-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 now `insertDefaultFixtureRecords`.
- The old `insertFixture` function is now `insertFixtureRow` and [`insertFixture`](#FixtureTestTrait) 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
----------------

[](#fixturetesttrait)

A trait designed to create and insert fixtures with data that can be defined in the test function.

**Provided method**

```
/**
 * @param class-string $fixture
 * @param array $attributes
 */
protected function insertFixture(string $fixture, array $attributes = []): array
```

**Usage**

```
// Inserts the fixture with the first_name being "Bob" and the rest default values from the fixture.
// Returns the inserted row data with the auto-incremented id.
$bobUserRow = $this->insertFixture(UserFixture::class, ['first_name' => 'Bob']);
```

**Fixture**

Each fixture must have a property `$table` with the table name and an array `$records` with the default data to insert.

```
