|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Features\ShowAuthButtons; |
| 6 | +use App\Models\Product; |
| 7 | +use App\Models\ProductLicense; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Laravel\Pennant\Feature; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class PurchaseHistoryTest extends TestCase |
| 14 | +{ |
| 15 | + use RefreshDatabase; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + Feature::define(ShowAuthButtons::class, true); |
| 22 | + } |
| 23 | + |
| 24 | + public function test_guest_cannot_view_purchase_history(): void |
| 25 | + { |
| 26 | + $response = $this->get('/customer/purchase-history'); |
| 27 | + |
| 28 | + $response->assertRedirect('/login'); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_purchase_history_shows_product_licenses(): void |
| 32 | + { |
| 33 | + $user = User::factory()->create(); |
| 34 | + $product = Product::factory()->create(['name' => 'Plugin Dev Kit']); |
| 35 | + |
| 36 | + ProductLicense::factory()->create([ |
| 37 | + 'user_id' => $user->id, |
| 38 | + 'product_id' => $product->id, |
| 39 | + 'price_paid' => 4900, |
| 40 | + 'currency' => 'USD', |
| 41 | + 'purchased_at' => now()->subDay(), |
| 42 | + ]); |
| 43 | + |
| 44 | + $response = $this->actingAs($user)->get('/customer/purchase-history'); |
| 45 | + |
| 46 | + $response->assertStatus(200); |
| 47 | + $response->assertSee('Plugin Dev Kit'); |
| 48 | + $response->assertSee('Product'); |
| 49 | + $response->assertSee('$49.00'); |
| 50 | + } |
| 51 | + |
| 52 | + public function test_purchase_history_shows_multiple_product_types(): void |
| 53 | + { |
| 54 | + $user = User::factory()->create(); |
| 55 | + |
| 56 | + $devKit = Product::factory()->create(['name' => 'Plugin Dev Kit']); |
| 57 | + $masterclass = Product::factory()->create(['name' => 'The NativePHP Masterclass']); |
| 58 | + |
| 59 | + ProductLicense::factory()->create([ |
| 60 | + 'user_id' => $user->id, |
| 61 | + 'product_id' => $devKit->id, |
| 62 | + 'price_paid' => 4900, |
| 63 | + 'purchased_at' => now()->subDays(2), |
| 64 | + ]); |
| 65 | + |
| 66 | + ProductLicense::factory()->create([ |
| 67 | + 'user_id' => $user->id, |
| 68 | + 'product_id' => $masterclass->id, |
| 69 | + 'price_paid' => 10100, |
| 70 | + 'purchased_at' => now()->subDay(), |
| 71 | + ]); |
| 72 | + |
| 73 | + $response = $this->actingAs($user)->get('/customer/purchase-history'); |
| 74 | + |
| 75 | + $response->assertStatus(200); |
| 76 | + $response->assertSee('Plugin Dev Kit'); |
| 77 | + $response->assertSee('The NativePHP Masterclass'); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_dashboard_total_purchases_includes_product_licenses(): void |
| 81 | + { |
| 82 | + $user = User::factory()->create(); |
| 83 | + $product = Product::factory()->create(); |
| 84 | + |
| 85 | + ProductLicense::factory()->create([ |
| 86 | + 'user_id' => $user->id, |
| 87 | + 'product_id' => $product->id, |
| 88 | + ]); |
| 89 | + |
| 90 | + $response = $this->actingAs($user)->get('/dashboard'); |
| 91 | + |
| 92 | + $response->assertStatus(200); |
| 93 | + $response->assertViewHas('totalPurchases', 1); |
| 94 | + } |
| 95 | + |
| 96 | + public function test_purchase_history_shows_empty_state_when_no_purchases(): void |
| 97 | + { |
| 98 | + $user = User::factory()->create(); |
| 99 | + |
| 100 | + $response = $this->actingAs($user)->get('/customer/purchase-history'); |
| 101 | + |
| 102 | + $response->assertStatus(200); |
| 103 | + $response->assertSee('No purchases yet'); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_product_purchases_show_as_active(): void |
| 107 | + { |
| 108 | + $user = User::factory()->create(); |
| 109 | + $product = Product::factory()->create(['name' => 'Plugin Dev Kit']); |
| 110 | + |
| 111 | + ProductLicense::factory()->create([ |
| 112 | + 'user_id' => $user->id, |
| 113 | + 'product_id' => $product->id, |
| 114 | + 'purchased_at' => now(), |
| 115 | + ]); |
| 116 | + |
| 117 | + $response = $this->actingAs($user)->get('/customer/purchase-history'); |
| 118 | + |
| 119 | + $response->assertStatus(200); |
| 120 | + $response->assertSee('Active'); |
| 121 | + } |
| 122 | +} |
0 commit comments