Skip to content

Commit

Permalink
Implement package migrations
Browse files Browse the repository at this point in the history
This adds Cashier's migrations to the package itself so they're shipped with Cashier and can be published just like with Passport and Telescope. This also directly integrates the migrations into the test suite so they're tested. Orchestra testbench helps up implement this.

Closes #639
  • Loading branch information
driesvints committed May 3, 2019
1 parent e1a4f15 commit 3b118f1
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 39 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"require-dev": {
"mockery/mockery": "^1.0",
"orchestra/testbench": "^3.8",
"phpunit/phpunit": "^7.5"
},
"autoload": {
Expand Down
40 changes: 40 additions & 0 deletions database/migrations/2019_05_03_000001_create_customer_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class CreateCustomerColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('stripe_id')->nullable()->collation('utf8mb4_bin');
$table->string('card_brand')->nullable();
$table->string('card_last_four', 4)->nullable();
$table->timestamp('trial_ends_at')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn([
'stripe_id',
'card_brand',
'card_last_four',
'trial_ends_at',
]);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

class CreateSubscriptionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('name');
$table->string('stripe_id')->collation('utf8mb4_bin');
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('subscriptions');
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="STRIPE_MODEL" value="Laravel\Cashier\Tests\Fixtures\User"/>
</php>
</phpunit>
19 changes: 19 additions & 0 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class Cashier
*/
protected static $formatCurrencyUsing;

/**
* Indicates if Cashier migrations will be run.
*
* @var bool
*/
public static $runsMigrations = true;

/**
* Get the publishable Stripe API key.
*
Expand Down Expand Up @@ -231,4 +238,16 @@ public static function formatAmount($amount)

return static::usesCurrencySymbol().$amount;
}

/**
* Configure Cashier to not register its migrations.
*
* @return static
*/
public static function ignoreMigrations()
{
static::$runsMigrations = false;

return new static;
}
}
26 changes: 23 additions & 3 deletions src/CashierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,28 @@ public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'cashier');

$this->publishes([
__DIR__.'/../resources/views' => $this->app->basePath('resources/views/vendor/cashier'),
]);
if ($this->app->runningInConsole()) {
$this->registerMigrations();

$this->publishes([
__DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
], 'cashier-migrations');

$this->publishes([
__DIR__.'/../resources/views' => $this->app->resourcePath('views/vendor/cashier'),
], 'cashier-views');
}
}

/**
* Register Passport's migration files.
*
* @return void
*/
protected function registerMigrations()
{
if (Cashier::$runsMigrations) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}
}
45 changes: 9 additions & 36 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
use Stripe\Token;
use Stripe\Stripe;
use Stripe\ApiResource;
use PHPUnit\Framework\TestCase;
use Stripe\Error\InvalidRequest;
use Orchestra\Testbench\TestCase;
use Illuminate\Database\Schema\Builder;
use Laravel\Cashier\Tests\Fixtures\User;
use Illuminate\Database\Schema\Blueprint;
use Laravel\Cashier\CashierServiceProvider;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;

abstract class IntegrationTestCase extends TestCase
Expand All @@ -28,47 +27,20 @@ public static function setUpBeforeClass()
Stripe::setApiKey(getenv('STRIPE_SECRET'));
}

public function setUp()
public function setUp(): void
{
parent::setUp();

Eloquent::unguard();

$db = new DB;
$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
$db->bootEloquent();
$db->setAsGlobal();

$this->schema()->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('name');
$table->string('stripe_id')->nullable();
$table->string('card_brand')->nullable();
$table->string('card_last_four')->nullable();
$table->timestamps();
});

$this->schema()->create('subscriptions', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->string('stripe_id');
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
$this->loadLaravelMigrations();

$this->artisan('migrate')->run();
}

public function tearDown()
protected function getPackageProviders($app)
{
$this->schema()->drop('users');
$this->schema()->drop('subscriptions');
return [CashierServiceProvider::class];
}

protected static function deleteStripeResource(ApiResource $resource)
Expand Down Expand Up @@ -112,6 +84,7 @@ protected function createCustomer($description = 'taylor'): User
return User::create([
'email' => "{$description}@cashier-test.com",
'name' => 'Taylor Otwell',
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
]);
}
}

0 comments on commit 3b118f1

Please sign in to comment.