phpunitHow to load fixtures with PHPUnit?
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
More of Phpunit
- How to skip a PHPUnit test?
- How to run tests in parallel with PHPUnit?
- How to use hooks in PHPUnit?
- What are PHPUnit required extensions
- How to use the PHPUnit Framework TestCase?
- How to run PHPUnit in quiet mode?
- How to show warnings in PHPUnit?
- How to use a listener with PHPUnit?
- How to stop PHPUnit on failure?
See more codes...