An open API service indexing awesome lists of open source software.

https://github.com/ecomdev/mysql-test-utils

Bare minimum MySQL Test utilities for writing automated tests of software that relies on MySQL as data storage.
https://github.com/ecomdev/mysql-test-utils

Last synced: 3 months ago
JSON representation

Bare minimum MySQL Test utilities for writing automated tests of software that relies on MySQL as data storage.

Awesome Lists containing this project

README

          

# MySQL Test Utilities
Bare minimum MySQL Test utilities for writing automated tests of software that relies on MySQL as data storage.

This package works great with any type of MySQL setup as soon as user for testing can create own databases.
Each instance of the database class creates a new unique database on the server that gets dropped upon test case completion.

## Configuration
In your PHPUnit xml you need to specify the connection settings for mysql connection and executable for mysql client.

Down below is an example of phpunit xml configuration with docker-compose database setup.
```xml


tests/






```

```yaml
version: "3"
services:
mysql:
image: percona/percona-server:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: tests
```

## Examples

#### Database gets created on each test
```php
use PHPUnit\Framework\TestCase;
use EcomDev\MySQLTestUtils\DatabaseFactory;

class SomeDatabaseRelatedTest extends TestCase
{
private $database;

protected function setUp() : void
{
$this->database = (new DatabaseFactory())->createDatabase();
$this->database->loadFixture('some/path/to/schema.sql');
}

public function testSomethingIsWritten()
{
$this->assertEquals(
[
['value1', 'value2']
],
$this->database->fetchTable('some_table_name', 'column1', 'column2')
);
}
}
```

#### Database gets created on each test class

```php
use PHPUnit\Framework\TestCase;
use EcomDev\MySQLTestUtils\DatabaseFactory;

class SomeDatabaseRelatedTest extends TestCase
{
private static $database;

public static function setUpBeforeClass() : void
{
self::$database = (new DatabaseFactory())->createDatabase();
self::$database->loadFixture('some/path/to/schema.sql');
}

public static function tearDownAfterClass() : void
{
self::$database = null;
}

public function testSomethingIsWritten()
{
$this->assertEquals(
[
['value1', 'value2']
],
self::$database->fetchTable('some_table_name', 'column1', 'column2')
);
}
}
```