Skip to content

Commit

Permalink
Publish migrations (#100)
Browse files Browse the repository at this point in the history
* Publish migrations instead of loading them

* Change TestCase to load database migrations, to use newer api and move app key to xml

* Update README.md with how-to publish migrations

* Move back artisan migrate just in case pgsql is relying on this
  • Loading branch information
peterquentin authored May 20, 2023
1 parent ac8287e commit dfe12b2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ MagicLink fires two events:

## Customization

### Config
To custom this package you can publish the config file:

```bash
Expand All @@ -308,6 +309,17 @@ php artisan vendor:publish --provider="MagicLink\MagicLinkServiceProvider" --tag

And edit the file `config/magiclink.php`


### Migrations
To customize the migration files of this package you need to publish the migration files:

```bash
php artisan vendor:publish --provider="MagicLink\MagicLinkServiceProvider" --tag="migrations"
```

You'll find the published files in `database/migrations/*`


### Custom response when magiclink is invalid

When the magicLink is invalid by default the http request return a status 403.
Expand Down
1 change: 1 addition & 0 deletions phpunit.postgres.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
</logging>
<php>
<env name="DB_DRIVER" value="pgsql" force="true"/>
<server name="APP_KEY" value="base64:mJlbzP1TMXUPouK3KK6e9zS/VvxtWTfzfVlkn1JTqpM="/>
</php>
</phpunit>
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<server name="APP_KEY" value="base64:mJlbzP1TMXUPouK3KK6e9zS/VvxtWTfzfVlkn1JTqpM="/>
</php>
</phpunit>
4 changes: 3 additions & 1 deletion src/MagicLinkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function boot()
__DIR__.'/../config/magiclink.php' => config_path('magiclink.php'),
], 'config');

$this->loadMigrationsFrom(__DIR__.'/../databases/migrations');
$this->publishes([
__DIR__.'/../databases/migrations' => database_path('migrations'),
], 'migrations');

if ($this->mustLoadRoute()) {
$this->loadRoutesFrom(__DIR__.'/../routes/routes.php');
Expand Down
43 changes: 23 additions & 20 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ abstract class TestCase extends Orchestra
public function setUp(): void
{
parent::setUp();

$this->setUpDatabase($this->app);
}

/**
Expand All @@ -32,10 +30,8 @@ protected function getPackageProviders($app)
*
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
protected function defineEnvironment($app)
{
$app['config']->set('app.key', 'base64:mJlbzP1TMXUPouK3KK6e9zS/VvxtWTfzfVlkn1JTqpM=');

$app['config']->set('auth.providers.users.model', 'MagicLink\Test\TestSupport\User');

$app['config']->set('view.paths', [__DIR__.'/stubs/resources/views']);
Expand All @@ -47,13 +43,6 @@ protected function getEnvironmentSetUp($app)
'root' => __DIR__.'/stubs/storage/app_alternative',
]);

$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
]);

$app['config']->set('database.connections.pgsql', [
'driver' => 'pgsql',
'host' => '127.0.0.1',
Expand All @@ -72,13 +61,27 @@ protected function getEnvironmentSetUp($app)
'database' => 'test',
]);

$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
]);

$driver = getenv('DB_DRIVER');

if($driver !== 'pgsql' && $driver !== 'mysql') {
$app['config']->set('database.default', 'testbench');
} else {
$app['config']->set('database.default', $driver);
}

if (getenv('DB_DRIVER') === 'pgsql') {
$app['config']->set('database.default', 'pgsql');
} elseif (getenv('DB_DRIVER') === 'mysql') {
$app['config']->set('database.default', 'mysql');
}
}

protected function defineDatabaseMigrations()
{
$this->loadMigrationsFrom(__DIR__.'/../databases/migrations');
$this->setUpDatabase($this->app);
}

/**
Expand All @@ -88,12 +91,12 @@ protected function getEnvironmentSetUp($app)
*/
protected function setUpDatabase($app)
{
if ($app['config']->get('database.default') !== 'sqlite') {
if ($app['config']->get('database.default') !== 'testbench') {
$app['db']->connection()->getSchemaBuilder()->dropIfExists('users');
$app['db']->connection()->getSchemaBuilder()->dropIfExists('migrations');
$app['db']->connection()->getSchemaBuilder()->dropIfExists('magic_links');
}

$this->artisan('migrate');

$app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
Expand Down

0 comments on commit dfe12b2

Please sign in to comment.