1. Go to this page and download the library: Download hjerichen/dbunit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hjerichen / dbunit example snippets
use HJerichen\DBUnit\Dataset\Attribute\DatasetForExpected
use HJerichen\DBUnit\Dataset\Attribute\DatasetForSetup
use HJerichen\DBUnit\Dataset\Dataset;
use HJerichen\DBUnit\Dataset\DatasetArray;
use HJerichen\DBUnit\MySQLTestCaseTrait;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Comparator\ComparisonFailure;
class MySQLTest extends TestCase
{
use MySQLTestCaseTrait;
private PDO $database;
protected function getDatabase(): PDO
{
if (!isset($this->database)) {
$this->database = new PDO(MYSQL_DSN, MYSQL_USER, MYSQL_PASS);
}
return $this->database;
}
protected function getDatasetForSetup(): Dataset
{
return $this->getDatasetForSetupFromAttribute() ?? new DatasetArray([
'product' => [
['id' => 1, 'ean' => '123', 'stock' => 0],
['id' => 2, 'ean' => '456', 'stock' => 10],
]
]);
}
/* TESTS */
public function testImportAndCompare(): void
{
$expected = new DatasetArray([
'product' => [
['id' => 1, 'ean' => '123', 'stock' => 0],
['id' => 2, 'ean' => '456', 'stock' => 10],
]
]);
$this->assertDatasetEqualsCurrentOne($expected);
}
#[DatasetForSetup([
'product' => [
['id' => 1, 'ean' => '123', 'stock' => 0],
],
])]
#[DatasetForExpected([
'product' => [
['id' => 1, 'ean' => '123', 'stock' => 0],
],
])]
public function testImportAndCompare2(): void
{
$expected = $this->getDatasetForExpectedFromAttribute();
$this->assertDatasetEqualsCurrentOne($expected);
}
private function assertDatasetEqualsCurrentOne(DatasetArray $expected):void{
try {
$this->assertDatasetEqualsCurrent($expected);
} catch (ComparisonFailure $failure) {
throw new ExpectationFailedException($failure->getMessage(), $failure);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.