Skip to content

Commit

Permalink
Add ability to set service providers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljennings committed Oct 25, 2018
1 parent 43728f4 commit 87a196a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
20 changes: 20 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ At the minute this package only works with phpunit and sqlite.
- [Installation](#installation)
- [Usage](#usage)
- [Multiple Connections](#multiple-connections)
- [Service Providers](#service-providers)
- [Environments](#environments)
- [Migration Cache](#migration-cache)

Expand Down Expand Up @@ -126,6 +127,25 @@ return [
]
```

### Service Providers

In your application you may have migrations that rely on a service provider being registered, because this package creates it's own application instance you will need to tell it which service providers to register.

To register your service providers you set the providers property in your `.refresh-database.yml` file.

Below is an example config file where we are loading a utility service provider.

```yml
migrations:
- database/migrations
- vendor/other/package/database/migrations
providers:
- Database\UtilityServiceProvider
output: tests
```

## Environments

Occasionally you might find you to want to disable the database dump in certain environments.
Expand Down
8 changes: 8 additions & 0 deletions src/DatabaseMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ protected function getEnvironmentSetUp($app)
]);
}

/**
* @inheritdoc
*/
public function getPackageProviders($app)
{
return $this->config->get('providers', []);
}

/**
* @inheritdoc
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/DatabaseMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace MichaelJennings\RefreshDatabase\Tests;

use Illuminate\Support\Facades\Schema;
use MichaelJennings\RefreshDatabase\Config;
use MichaelJennings\RefreshDatabase\DatabaseMigrator;
use MichaelJennings\RefreshDatabase\Repositories\Yaml;
use MichaelJennings\RefreshDatabase\Tests\Concerns\CleanUpDatabase;
use MichaelJennings\RefreshDatabase\Tests\Concerns\WritesFiles;
use MichaelJennings\RefreshDatabase\Tests\Fixtures\TestServiceProvider;

class DatabaseMigratorTest extends TestCase
{
Expand Down Expand Up @@ -94,6 +96,34 @@ public function it_does_not_cache_the_migrations()
$this->assertTrue(file_exists(__DIR__ . '/.database/export.sql'));
}

/**
* @test
*/
public function it_registers_a_service_provider_before_migrating()
{
$migrator = new DatabaseMigrator(
new Config(
new Yaml([
'migrations' => [
'tests/migrations/macro',
],
'output' => 'tests',
'providers' => [
TestServiceProvider::class,
]
], __DIR__ . '/..')
)
);

$migrator->migrate();

$this->assertTrue(file_exists(__DIR__ . '/.database/testing.sqlite'));
$this->assertTrue(file_exists(__DIR__ . '/.database/migrations'));
$this->assertTrue(file_exists(__DIR__ . '/.database/export.sql'));
// Check the macro ran successfully
$this->assertTrue(Schema::hasColumn('test_departments', 'active'));
}

/**
* @test
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/TestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace MichaelJennings\RefreshDatabase\Tests\Fixtures;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Fluent;
use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
public function boot()
{
Blueprint::macro('active', function(): Fluent {
return self::tinyInteger('active')->default(0);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTestDepartmentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test_departments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->active();
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('test_departments');
}
}

0 comments on commit 87a196a

Please sign in to comment.