1. Go to this page and download the library: Download cspray/database-testing 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/ */
cspray / database-testing example snippets
declare(strict_types=1);
namespace Cspray\DatabaseTesting\Demo;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Fixture\LoadFixture;
use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;
use Cspray\DatabaseTesting\TestDatabase;
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use PHPUnit\Framework\TestCase;
use PDO;
#[RequiresTestDatabase(
// this should be implemented by you or provided by an extension to this library
new MyPdoConnectionAdapterFactory(),
// you could also use Cspray\DatabaseTesting\DatabaseCleanup\TruncateTables
// or implement your own Cspray\DatabaseTesting\DatabaseCleanup\CleanupStrategy
new TransactionWithRollback()
)]
final class RepositoryTest extends TestCase {
#[InjectTestDatabase]
private static TestDatabase $testDatabase;
private PDO $pdo;
private MyRepository $myRepository;
protected function setUp() : void {
// be sure to use the connection from TestDatabase! depending on CleanupStrategy,
// using a different connection could wind up with a dirty database state
$this->pdo = self::$testDatabase->connection();
$this->myRepository = new MyRepository($this->pdo);
}
// populate with more appropriate data. recommended to implement your own
// Cspray\DatabaseTesting\Fixture\Fixture to reuse datasets across tests
#[LoadFixture(
new SingleRecordFixture('my_table', [
'name' => 'cspray',
'website' => 'https://cspray.io'
])
)]
public function testTableHasCorrectlyLoadedFixtures() : void {
$table = self::$testDatabase->table('my_table');
self::assertCount(1, $table);
self::assertSame('cspray', $table->row(0)->get('name'))
self::assertSame('website', $table->row(0)->get('website'));
}
public function testTableCanBeReloadedToGetNewlyInsertedRecords() : void {
$table = self::$testDatabase->table('my_table');
self::assertCount(0, $table);
$this->myRepository->save(new MyEntity());
$table->reload();
self::assertCount(1, $table);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.