Skip to content

Commit 69a8d63

Browse files
committed
✅ Add Plan and feature test
1 parent 027fe42 commit 69a8d63

File tree

9 files changed

+144
-13
lines changed

9 files changed

+144
-13
lines changed

database/migrations/2020_01_01_000001_create_plans_table.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ public function up(): void
1212
Schema::create(config('laravel-subscriptions.tables.plans'), function (Blueprint $table): void {
1313
$table->id();
1414

15-
$table->string('slug')->unique();
1615
$table->json('name');
16+
$table->string('slug')->unique();
1717
$table->json('description')->nullable();
1818
$table->boolean('is_active')->default(true);
1919
$table->decimal('price')->default('0.00');
2020
$table->decimal('signup_fee')->default('0.00');
2121
$table->string('currency', 3);
22-
$table->smallInteger('trial_period')->unsigned()->default(0);
22+
$table->unsignedSmallInteger('trial_period')->default(0);
2323
$table->string('trial_interval')->default('day');
24-
$table->smallInteger('invoice_period')->unsigned()->default(0);
24+
$table->unsignedSmallInteger('invoice_period')->default(0);
2525
$table->string('invoice_interval')->default('month');
26-
$table->smallInteger('grace_period')->unsigned()->default(0);
26+
$table->unsignedSmallInteger('grace_period')->default(0);
2727
$table->string('grace_interval')->default('day');
28-
$table->tinyInteger('prorate_day')->unsigned()->nullable();
29-
$table->tinyInteger('prorate_period')->unsigned()->nullable();
30-
$table->tinyInteger('prorate_extend_due')->unsigned()->nullable();
31-
$table->smallInteger('active_subscribers_limit')->unsigned()->nullable();
32-
$table->mediumInteger('sort_order')->unsigned()->default(0);
28+
$table->unsignedTinyInteger('prorate_day')->nullable();
29+
$table->unsignedTinyInteger('prorate_period')->nullable();
30+
$table->unsignedTinyInteger('prorate_extend_due')->nullable();
31+
$table->unsignedSmallInteger('active_subscribers_limit')->nullable();
32+
$table->unsignedSmallInteger('sort_order')->default(0);
3333

3434
$table->timestamps();
3535
$table->softDeletes();

database/migrations/2020_01_01_000002_create_plan_features_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public function up(): void
1414
$table->id();
1515

1616
$table->foreignIdFor(Plan::class);
17-
$table->string('slug')->unique();
1817
$table->json('name');
18+
$table->string('slug')->unique();
1919
$table->json('description')->nullable();
2020
$table->string('value');
2121
$table->unsignedSmallInteger('resettable_period')->default(0);

database/migrations/2020_01_01_000003_create_plan_subscriptions_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public function up(): void
1515

1616
$table->morphs('subscriber');
1717
$table->foreignIdFor(Plan::class);
18-
$table->string('slug')->unique();
1918
$table->json('name');
19+
$table->string('slug')->unique();
2020
$table->json('description')->nullable();
2121
$table->string('timezone')->nullable();
2222

src/Models/Plan.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @property string $slug
2222
* @property array $name
2323
* @property array $description
24-
* @property bool$is_active
24+
* @property bool $is_active
2525
* @property float $price
2626
* @property float $signup_fee
2727
* @property string $currency
@@ -39,7 +39,7 @@
3939
* @property \Carbon\Carbon|null $created_at
4040
* @property \Carbon\Carbon|null $updated_at
4141
* @property \Carbon\Carbon|null $deleted_at
42-
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravelcm\Subscriptions\Models\Feature[] $features
42+
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravelcm\Subscriptions\Models\Feature[] $features
4343
* @property-read \Illuminate\Database\Eloquent\Collection|\Laravelcm\Subscriptions\Models\Subscription[] $subscriptions
4444
*
4545
* @method static \Illuminate\Database\Eloquent\Builder|\Laravelcm\Subscriptions\Models\Plan ordered($direction = 'asc')

tests/Feature/PlanTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tests\Models\Feature;
6+
use Tests\Models\Plan;
7+
8+
beforeEach(function (): void {
9+
$this->plan = Plan::factory()->create();
10+
});
11+
12+
it('can create a plan', function (): void {
13+
expect(Plan::count())->toBe(1);
14+
})->group('plan');
15+
16+
it('a plan can have many features', function (): void {
17+
expect($this->plan->features()->count())->toBe(0);
18+
19+
Feature::factory()->create(['plan_id' => $this->plan->id]);
20+
Feature::factory()->create([
21+
'name' => 'pictures_per_listing',
22+
'value' => 10,
23+
'sort_order' => 5,
24+
'plan_id' => $this->plan->id,
25+
]);
26+
27+
expect($this->plan->features()->count())->toBe(2);
28+
})->group('plan');
29+
30+
it('a plan can be free with trial period', function (): void {
31+
$this->plan->update([
32+
'price' => 0,
33+
'signup_fee' => 0,
34+
]);
35+
36+
expect($this->plan->isFree())
37+
->toBeTrue()
38+
->and($this->plan->hasTrial())
39+
->toBeTrue();
40+
})->group('plan');
41+
42+
it('a plan can have a grace period', function (): void {
43+
$this->plan->update([
44+
'grace_period' => 7,
45+
'grace_interval' => 'day',
46+
]);
47+
48+
expect($this->plan->hasGrace())
49+
->toBeTrue();
50+
})->group('plan');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Database\Factories;
6+
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Tests\Models\Feature;
9+
10+
class FeatureFactory extends Factory
11+
{
12+
protected $model = Feature::class;
13+
14+
public function definition(): array
15+
{
16+
return [
17+
'name' => 'listings',
18+
'value' => 50,
19+
'sort_order' => 1,
20+
];
21+
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Database\Factories;
6+
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Tests\Models\Plan;
9+
10+
final class PlanFactory extends Factory
11+
{
12+
protected $model = Plan::class;
13+
14+
public function definition(): array
15+
{
16+
return [
17+
'name' => 'Pro',
18+
'description' => 'Pro plan',
19+
'price' => 9.99,
20+
'signup_fee' => 1.99,
21+
'invoice_period' => 1,
22+
'invoice_interval' => 'month',
23+
'trial_period' => 15,
24+
'trial_interval' => 'day',
25+
'sort_order' => 1,
26+
'currency' => 'USD',
27+
];
28+
}
29+
}

tests/src/Models/Feature.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Models;
6+
7+
use Tests\Database\Factories\FeatureFactory;
8+
9+
class Feature extends \Laravelcm\Subscriptions\Models\Feature
10+
{
11+
protected static function newFactory(): FeatureFactory
12+
{
13+
return FeatureFactory::new();
14+
}
15+
}

tests/src/Models/Plan.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Models;
6+
7+
use Tests\Database\Factories\PlanFactory;
8+
9+
class Plan extends \Laravelcm\Subscriptions\Models\Plan
10+
{
11+
protected static function newFactory(): PlanFactory
12+
{
13+
return PlanFactory::new();
14+
}
15+
}

0 commit comments

Comments
 (0)