PHPackages                             dknx01/data-fixtures - 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. dknx01/data-fixtures

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

dknx01/data-fixtures
====================

A PHP package for data fixtures with plain PDO connection.

0.2(1mo ago)00MITPHPPHP &gt;=8.5

Since Mar 11Pushed 1mo agoCompare

[ Source](https://github.com/dknx01/data-fixtures)[ Packagist](https://packagist.org/packages/dknx01/data-fixtures)[ RSS](/packages/dknx01-data-fixtures/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (13)Versions (5)Used By (0)

DKNX01/Data-fixtures
====================

[](#dknx01data-fixtures)

A PHP package for data fixtures with plain PDO connection.

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

[](#requirements)

- ext-pdo for PDO connections
- (optional, but recommended) [Faker](https://fakerphp.org)

Install
-------

[](#install)

2. run `composer require --dev dknx01/data-fixtures-phpunit`

Usage
-----

[](#usage)

If you want to use data fixtures in you tests you can do it in multiple ways. You can write a method to fill data in the database. or you can use a fixture and reuse it in multiple tests.

### With an existing PDO connection

[](#with-an-existing-pdo-connection)

If you already have a PDO connection you can use it directly.

```
new Configuration(pdo: $pdo, databaseName: 'foo');
```

### Create a new PDO connection

[](#create-a-new-pdo-connection)

```
new Configuration(dsn: 'mysql:host=localhost;port=3307;dbname=testdb', user: 'foo', password: 'bar', options: []);
```

### Configuration:

[](#configuration)

The Configuration object has the following parameters:

ParameterDescriptionpdoAn existing PDO connection that will be useddsnThe dsn of the PDO connectionuserDatabases user, if neededpasswordDatabases users password, if neededoptionsPDO options that will be used for the connection### Load and execute fixtures

[](#load-and-execute-fixtures)

Your class should use the `DataFixtureTrait`.

```
/// your code
    $this->loadFixtures();
    $this->executeFixtures($configuration);
/// your code
```

If you do not want to use the trait, you can load and execute the fixtures this way:

```
// your code
    $fixtures = new FixtureCollection();
    $fixtures->add(new SimpleFixture());
    $handler = new FixtureHandler($configuration);
    $handler->handle($fixtures);
// your code
```

**loadFixtures(int $stackPosition = 1)**

This option should be used to define at which stack position the fixtures should be loaded from. Mostly it will be "1" which means directly from the method/class the "loadFixtures" method is called from. If you have more inherited classes, you can change the number.

### Writing a fixture

[](#writing-a-fixture)

Each fixture must implement the `Dknx01\DataFixtures\Contract\FixtureInterface`. Example:

```
