定制 cspray/database-testing-pdo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cspray/database-testing-pdo

最新稳定版本:0.1.0

Composer 安装命令:

composer require cspray/database-testing-pdo

包简介

PDO ConnectionAdapter for cspray/database-testing

README 文档

README

A connection adapter for cspray/database-testing that allows you to use a PDO connection for testing database interactions.

Installation

Composer is the only supported means to install this package.

composer require --dev cspray/database-testing-pdo

Quick Start

This library works by providing an implementation of the Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapter interface, along with a variety of Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterFactory implementations designed to work with common databases. Check out the example appropriate for your database below or check out the "Database Connection Adapter Reference"!

All examples below will use code from the cspray/database-testing-phpunit extension. If you're using a different testing framework you may need to adjust your code as appropriate.

Postgres

<?php declare(strict_types=1);

namespace Cspray\DatabaseTesting\Pdo\Demo;

use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterConfig;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\RequiresTestDatabase;
use Cspray\DatabaseTesting\Pdo\Postgres\PostgresConnectionAdapterFactory;
use PHPUnit\Framework\TestCase;

#[RequiresTestDatabase(
    new PostgresConnectionAdapterFactory(new ConnectionAdapterConfig(
        'my_database',
        '127.0.0.1',
        5432,
        'my_user',
        'my_password'
    )),
    new TransactionWithRollback()
)]
final class PostgresDemoTest extends TestCase {

}

MySql

<?php declare(strict_types=1);

namespace Cspray\DatabaseTesting\Pdo\Demo;

use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterConfig;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\RequiresTestDatabase;
use Cspray\DatabaseTesting\Pdo\Postgres\PostgresConnectionAdapterFactory;
use PHPUnit\Framework\TestCase;

#[RequiresTestDatabase(
    new PostgresConnectionAdapterFactory(new ConnectionAdapterConfig(
        'my_database',
        '127.0.0.1',
        5432,
        'my_user',
        'my_password'
    )),
    new TransactionWithRollback()
)]
final class PostgresDemoTest extends TestCase {

}

Sqlite

<?php declare(strict_types=1);

namespace Cspray\DatabaseTesting\Pdo\Demo;

use Cspray\DatabaseTesting\ConnectionAdapter\ConnectionAdapterConfig;
use Cspray\DatabaseTesting\DatabaseCleanup\TransactionWithRollback;
use Cspray\DatabaseTesting\RequiresTestDatabase;
use Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory;
use PHPUnit\Framework\TestCase;

#[RequiresTestDatabase(
    // The default file path for Sqlite is an in-memory database
    // Provide the path to the SQL file that should be loaded when
    // the database is created
    // if you're using a persisted file, provide it as the first argument
    // and remove the initial schema path
    new SqliteConnectionAdapterFactory(
        initialSchemaPath: __DIR__ . '/schemas/sqlite.sql'
    ),
    new TransactionWithRollback()
)]
final class SqliteDemoTest extends TestCase {

}

Database Connection Adapter Reference

Database ConnectionAdapterFactory
Postgres Cspray\DatabaseTesting\Pdo\Postgres\PostgresConnectionAdapterFactory
MySql Cspray\DatabaseTesting\Pdo\Mysql\MysqlConnectionAdapterFactory
SQLite Cspray\DatabaseTesting\Pdo\Sqlite\SqliteConnectionAdapterFactory

Running Tests

By the nature of this library, we need to interact with a database during our tests. This presents some challenges and concessions that otherwise wouldn't be present when writing tests. Most importantly, that means we need to have multiple running database servers. To run tests for this library you must use docker compose run --rm tests. This will ensure the appropriate database servers are up and running so tests have something to connect to.

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2025-05-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固