Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.38 KB

how-to-load-fixtures-with-phpunit.md

File metadata and controls

38 lines (29 loc) · 1.38 KB

How to load fixtures with PHPUnit?

// plain

PHPUnit provides a way to load fixtures with the DataSet class. Fixtures are data used to test the application.

Example code

<?php
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    public function testFixtures()
    {
        // Load the fixtures
        $fixtureData = $this->createMySQLXMLDataSet(__DIR__ . '/fixtures.xml');
        // Use the fixtures
        $this->assertEquals($fixtureData->getTable('users')->getRow(0), array('id' => 1, 'name' => 'John'));
    }
}

The code above loads the fixtures from the fixtures.xml file and then uses the data to assert that the first row of the users table has the id of 1 and the name of John.

The DataSet class provides several methods to load fixtures from different formats, such as XML, CSV, JSON, YAML, and PHP.

  • createMySQLXMLDataSet: Loads fixtures from an XML file in the MySQL XML format.
  • createCsvDataSet: Loads fixtures from a CSV file.
  • createJsonDataSet: Loads fixtures from a JSON file.
  • createYamlDataSet: Loads fixtures from a YAML file.
  • createArrayDataSet: Loads fixtures from a PHP array.

Helpful links

onelinerhub: How to load fixtures with PHPUnit?