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.
- Host: GitHub
- URL: https://github.com/ecomdev/mysql-test-utils
- Owner: EcomDev
- License: mit
- Created: 2020-06-08T06:33:15.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2020-06-15T19:41:56.000Z (almost 6 years ago)
- Last Synced: 2025-02-26T11:06:35.642Z (about 1 year ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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')
);
}
}
```