PHP code example of cspray / database-testing-phpunit
1. Go to this page and download the library: Download cspray/database-testing-phpunit 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-phpunit example snippets
declare(strict_types=1);
namespace Cspray\DatabaseTesting\PhpUnit\Demo;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use PHPUnit\Framework\TestCase;
#[RequiresTestDatabase(
new SqliteConnectionAdapterFactory(
':memory:',
'/path/to/my/schema'
),
new TransactionWithRollback()
)]
final class MyDemoTest extends TestCase {
}
declare(strict_types=1);
namespace Cspray\DatabaseTesting\PhpUnit\Demo;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use Cspray\DatabaseTesting\TestDatabase;
use PHPUnit\Framework\TestCase;
#[RequiresTestDatabase(
new SqliteConnectionAdapterFactory(
':memory:',
'/path/to/my/schema'
),
new TransactionWithRollback()
)]
final class MyDemoTest extends TestCase {
#[InjectTestDatabase]
private static TestDatabase $testDatabase;
}
declare(strict_types=1);
namespace Cspray\DatabaseTesting\PhpUnit\Demo;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\Fixture\LoadFixture;
use Cspray\DatabaseTesting\Fixture\SingleRecordFixture;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use Cspray\DatabaseTesting\PhpUnit\InjectTestDatabase;
use Cspray\DatabaseTesting\PhpUnit\RequiresTestDatabase;
use Cspray\DatabaseTesting\TestDatabase;
use PHPUnit\Framework\TestCase;
#[RequiresTestDatabase(
new SqliteConnectionAdapterFactory(
':memory:',
'/path/to/my/schema'
),
new TransactionWithRollback()
)]
final class MyDemoTest extends TestCase {
#[InjectTestDatabase]
private static TestDatabase $testDatabase;
protected function setUp() : void{
parent::setUp(); // TODO: Change the autogenerated stub
self::$testDatabase->loadFixtures([
new SingleRecordFixture('my_table', ['name' => 'from setup'])
]);
}
#[LoadFixture(
new SingleRecordFixture('my_table', ['name' => 'from attribute'])
)]
public function testTableHasCorrectRecords() : void {
$table = self::$testDatabase->table('my_table');
self::assertCount(2, $table);
self::assertSame('from attribute', $table->row(0)->get('name'));
self::assertSame('from setup', $table->row(1)->get('name'));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.