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

[10.0] Ship migrations with package #663

Merged
merged 1 commit into from
May 3, 2019
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
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 Cashier'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',
]);
}
}