Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding refresh DB traits #6

Merged
merged 7 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions .github/workflows/ci-mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: MySQL

on:
push:
branches: [ 2.0 ]
pull_request:
branches: [ 2.0 ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.1 ]
mysql: [ 8.0 ]
stability: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

services:
mysql:
image: mysql:${{ matrix.mysql }}
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: spiral
MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password
ports:
- 13306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Validate Composer
run: composer validate

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Restore Composer Cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer

- name: Install Dependencies
uses: nick-invision/retry@v2.7.0
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --group driver-mysql
53 changes: 53 additions & 0 deletions .github/workflows/ci-sqlite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: SQLite

on:
push:
branches: [ 2.0 ]
pull_request:
branches: [ 2.0 ]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.1 ]
stability: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Validate Composer
run: composer validate

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Restore Composer Cache
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.stability }}-composer

- name: Install Dependencies
uses: nick-invision/retry@v2.7.0
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --group driver-sqlite
18 changes: 15 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: run-tests

on:
push:
branches: [ master ]
branches: [ 2.0 ]
pull_request:
branches: [ master ]
branches: [ 2.0 ]

jobs:
test:
Expand All @@ -13,11 +13,23 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.0, 8.1 ]
php: [ 8.1 ]
mysql: [ 8.0 ]
stability: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

services:
mysql:
image: mysql:${{ matrix.mysql }}
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: spiral
MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password
ports:
- 13306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: run-tests

on:
push:
branches: [ master ]
branches: [ 2.0 ]
pull_request:
branches: [ master ]
branches: [ 2.0 ]

jobs:
static-analysis:
Expand All @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 8.0 ]
php: [ 8.1 ]
os: [ ubuntu-latest ]

steps:
Expand Down
134 changes: 71 additions & 63 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
{
"name": "spiral-packages/database-seeder",
"description": "The package provides the ability to seed your database with data using seed classes",
"keywords": [
"spiral-packages",
"spiral",
"seeder",
"database-seeder"
],
"homepage": "https://github.com/spiral-packages/database-seeder",
"license": "MIT",
"authors": [
{
"name": "Maxim Smakouz",
"email": "m.smakouz@gmail.com",
"role": "Developer"
}
],
"require": {
"php": "^8.1",
"cycle/orm": "^2.1",
"butschster/entity-faker": "^0.9",
"fakerphp/faker": "^1.19",
"laminas/laminas-hydrator": "^4.3",
"spiral/attributes": "^3.0",
"spiral/scaffolder": "^3.0",
"doctrine/annotations": "^1.13",
"spiral/boot": "^3.0",
"spiral/console": "^3.0"
},
"require-dev": {
"spiral/framework": "^3.0",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5",
"spiral/testing": "^2.0",
"vimeo/psalm": "^4.20"
},
"autoload": {
"psr-4": {
"Spiral\\DatabaseSeeder\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\App\\": "tests/app",
"Tests\\": "tests/src"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm --config=psalm.xml ./src"
},
"config": {
"sort-packages": true
},
"extra": {
"spiral": {
"bootloaders": [
"Spiral\\DatabaseSeeder\\Bootloader\\DatabaseSeederBootloader"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
"name": "spiral-packages/database-seeder",
"description": "The package provides the ability to seed your database with data using seed classes",
"keywords": [
"spiral-packages",
"spiral",
"seeder",
"database-seeder"
],
"homepage": "https://github.com/spiral-packages/database-seeder",
"license": "MIT",
"authors": [
{
"name": "Maxim Smakouz",
"email": "m.smakouz@gmail.com",
"role": "Developer"
}
],
"require": {
"php": "^8.1",
"butschster/entity-faker": "^0.9",
"fakerphp/faker": "^1.19",
"laminas/laminas-hydrator": "^4.3",
"spiral/attributes": "^3.0",
"spiral/scaffolder": "^3.0",
"doctrine/annotations": "^1.13",
"spiral/boot": "^3.0",
"spiral/console": "^3.0"
},
"require-dev": {
"spiral/framework": "^3.0",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.20",
"spiral/cycle-bridge": "^2.0",
"spiral/testing": "^2.0"
},
"autoload": {
"files": [
"src/polyfill.php"
],
"psr-4": {
"Spiral\\DatabaseSeeder\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\App\\": "tests/app/src",
"Tests\\Database\\": "tests/app/database",
"Tests\\": "tests/src"
}
},
"suggest": {
"spiral/testing": "To use the Spiral\\DatabaseSeeder\\TestCase class and helpers to test an app with DB",
"spiral/cycle-bridge": "For easy database and ORM configuration in a test application"
},
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm --config=psalm.xml ./src"
},
"config": {
"sort-packages": true
},
"extra": {
"spiral": {
"bootloaders": [
"Spiral\\DatabaseSeeder\\Bootloader\\DatabaseSeederBootloader"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
10 changes: 10 additions & 0 deletions src/Database/DatabaseState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Spiral\DatabaseSeeder\Database;

class DatabaseState
{
public static bool $migrated = false;
}
9 changes: 9 additions & 0 deletions src/Database/Exception/DatabaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\DatabaseSeeder\Database\Exception;

class DatabaseException extends \RuntimeException
{
}
9 changes: 9 additions & 0 deletions src/Database/Exception/DatabaseMigrationsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\DatabaseSeeder\Database\Exception;

class DatabaseMigrationsException extends \RuntimeException
{
}
42 changes: 42 additions & 0 deletions src/Database/Traits/DatabaseMigrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Spiral\DatabaseSeeder\Database\Traits;

use Spiral\Boot\FinalizerInterface;
use Spiral\DatabaseSeeder\Database\DatabaseState;
use Spiral\DatabaseSeeder\Database\Exception\DatabaseMigrationsException;

trait DatabaseMigrations
{
/**
* Migrate the database before and after each test.
*/
public function runDatabaseMigrations(): void
{
$config = $this->getConfig('migration');
if (empty($config['directory'])) {
throw new DatabaseMigrationsException(
'Please, configure migrations in your test application to use DatabaseMigrations.'
);
}

if (!isset($config['safe']) || $config['safe'] !== true) {
throw new DatabaseMigrationsException(
'The `safe` parameter in the test application migrations configuration must be set to true.'
);
}

$this->runCommand('cycle:migrate', ['--run' => true]);

$self = $this;
$this->getContainer()->get(FinalizerInterface::class)->addFinalizer(static function () use($self, $config) {
$self->runCommand('migrate:rollback', ['--all' => true]);

$self->cleanupDirectories($config['directory']);

DatabaseState::$migrated = false;
});
}
}
Loading