|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Notifications; |
| 4 | + |
| 5 | +use App\Enums\PayoutStatus; |
| 6 | +use App\Models\DeveloperAccount; |
| 7 | +use App\Models\Plugin; |
| 8 | +use App\Models\PluginLicense; |
| 9 | +use App\Models\PluginPayout; |
| 10 | +use App\Models\User; |
| 11 | +use App\Notifications\PluginSaleCompleted; |
| 12 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class PluginSaleCompletedTest extends TestCase |
| 16 | +{ |
| 17 | + use RefreshDatabase; |
| 18 | + |
| 19 | + public function test_email_lists_plugin_names_and_payout_amounts(): void |
| 20 | + { |
| 21 | + $developer = User::factory()->create(); |
| 22 | + $developerAccount = DeveloperAccount::factory() |
| 23 | + ->onboarded() |
| 24 | + ->withAcceptedTerms() |
| 25 | + ->create(['user_id' => $developer->id]); |
| 26 | + |
| 27 | + $plugin = Plugin::factory()->paid()->create([ |
| 28 | + 'user_id' => $developer->id, |
| 29 | + 'developer_account_id' => $developerAccount->id, |
| 30 | + 'name' => 'acme/camera-plugin', |
| 31 | + ]); |
| 32 | + |
| 33 | + $license = PluginLicense::factory()->create([ |
| 34 | + 'plugin_id' => $plugin->id, |
| 35 | + 'stripe_invoice_id' => 'in_test_123', |
| 36 | + 'price_paid' => 2900, |
| 37 | + ]); |
| 38 | + |
| 39 | + $payout = PluginPayout::create([ |
| 40 | + 'plugin_license_id' => $license->id, |
| 41 | + 'developer_account_id' => $developerAccount->id, |
| 42 | + 'gross_amount' => 2900, |
| 43 | + 'platform_fee' => 870, |
| 44 | + 'developer_amount' => 2030, |
| 45 | + 'status' => PayoutStatus::Pending, |
| 46 | + ]); |
| 47 | + |
| 48 | + $notification = new PluginSaleCompleted(collect([$payout->load('pluginLicense.plugin')])); |
| 49 | + $rendered = $notification->toMail($developer)->render()->toHtml(); |
| 50 | + |
| 51 | + $this->assertStringContainsString('acme/camera-plugin', $rendered); |
| 52 | + $this->assertStringContainsString('$20.30', $rendered); |
| 53 | + $this->assertStringContainsString('Total payout: $20.30', $rendered); |
| 54 | + |
| 55 | + $mail = $notification->toMail($developer); |
| 56 | + $this->assertEquals("You've made a sale!", $mail->subject); |
| 57 | + } |
| 58 | + |
| 59 | + public function test_email_lists_multiple_plugins_with_correct_total(): void |
| 60 | + { |
| 61 | + $developer = User::factory()->create(); |
| 62 | + $developerAccount = DeveloperAccount::factory() |
| 63 | + ->onboarded() |
| 64 | + ->withAcceptedTerms() |
| 65 | + ->create(['user_id' => $developer->id]); |
| 66 | + |
| 67 | + $plugin1 = Plugin::factory()->paid()->create([ |
| 68 | + 'user_id' => $developer->id, |
| 69 | + 'developer_account_id' => $developerAccount->id, |
| 70 | + 'name' => 'acme/camera-plugin', |
| 71 | + ]); |
| 72 | + |
| 73 | + $plugin2 = Plugin::factory()->paid()->create([ |
| 74 | + 'user_id' => $developer->id, |
| 75 | + 'developer_account_id' => $developerAccount->id, |
| 76 | + 'name' => 'acme/gps-plugin', |
| 77 | + ]); |
| 78 | + |
| 79 | + $license1 = PluginLicense::factory()->create([ |
| 80 | + 'plugin_id' => $plugin1->id, |
| 81 | + 'stripe_invoice_id' => 'in_test_456', |
| 82 | + 'price_paid' => 2900, |
| 83 | + ]); |
| 84 | + |
| 85 | + $license2 = PluginLicense::factory()->create([ |
| 86 | + 'plugin_id' => $plugin2->id, |
| 87 | + 'stripe_invoice_id' => 'in_test_456', |
| 88 | + 'price_paid' => 4900, |
| 89 | + ]); |
| 90 | + |
| 91 | + $payout1 = PluginPayout::create([ |
| 92 | + 'plugin_license_id' => $license1->id, |
| 93 | + 'developer_account_id' => $developerAccount->id, |
| 94 | + 'gross_amount' => 2900, |
| 95 | + 'platform_fee' => 870, |
| 96 | + 'developer_amount' => 2030, |
| 97 | + 'status' => PayoutStatus::Pending, |
| 98 | + ]); |
| 99 | + |
| 100 | + $payout2 = PluginPayout::create([ |
| 101 | + 'plugin_license_id' => $license2->id, |
| 102 | + 'developer_account_id' => $developerAccount->id, |
| 103 | + 'gross_amount' => 4900, |
| 104 | + 'platform_fee' => 1470, |
| 105 | + 'developer_amount' => 3430, |
| 106 | + 'status' => PayoutStatus::Pending, |
| 107 | + ]); |
| 108 | + |
| 109 | + $payout1->load('pluginLicense.plugin'); |
| 110 | + $payout2->load('pluginLicense.plugin'); |
| 111 | + $payouts = collect([$payout1, $payout2]); |
| 112 | + |
| 113 | + $notification = new PluginSaleCompleted($payouts); |
| 114 | + $rendered = $notification->toMail($developer)->render()->toHtml(); |
| 115 | + |
| 116 | + $this->assertStringContainsString('acme/camera-plugin', $rendered); |
| 117 | + $this->assertStringContainsString('$20.30', $rendered); |
| 118 | + $this->assertStringContainsString('acme/gps-plugin', $rendered); |
| 119 | + $this->assertStringContainsString('$34.30', $rendered); |
| 120 | + $this->assertStringContainsString('Total payout: $54.60', $rendered); |
| 121 | + } |
| 122 | + |
| 123 | + public function test_email_does_not_contain_buyer_information(): void |
| 124 | + { |
| 125 | + $buyer = User::factory()->create([ |
| 126 | + 'name' => 'BuyerFirstName BuyerLastName', |
| 127 | + 'email' => 'buyer@example.com', |
| 128 | + ]); |
| 129 | + |
| 130 | + $developer = User::factory()->create(); |
| 131 | + $developerAccount = DeveloperAccount::factory() |
| 132 | + ->onboarded() |
| 133 | + ->withAcceptedTerms() |
| 134 | + ->create(['user_id' => $developer->id]); |
| 135 | + |
| 136 | + $plugin = Plugin::factory()->paid()->create([ |
| 137 | + 'user_id' => $developer->id, |
| 138 | + 'developer_account_id' => $developerAccount->id, |
| 139 | + 'name' => 'acme/test-plugin', |
| 140 | + ]); |
| 141 | + |
| 142 | + $license = PluginLicense::factory()->create([ |
| 143 | + 'user_id' => $buyer->id, |
| 144 | + 'plugin_id' => $plugin->id, |
| 145 | + 'stripe_invoice_id' => 'in_test_789', |
| 146 | + 'price_paid' => 2900, |
| 147 | + ]); |
| 148 | + |
| 149 | + $payout = PluginPayout::create([ |
| 150 | + 'plugin_license_id' => $license->id, |
| 151 | + 'developer_account_id' => $developerAccount->id, |
| 152 | + 'gross_amount' => 2900, |
| 153 | + 'platform_fee' => 870, |
| 154 | + 'developer_amount' => 2030, |
| 155 | + 'status' => PayoutStatus::Pending, |
| 156 | + ]); |
| 157 | + |
| 158 | + $notification = new PluginSaleCompleted(collect([$payout->load('pluginLicense.plugin')])); |
| 159 | + $rendered = $notification->toMail($developer)->render()->toHtml(); |
| 160 | + |
| 161 | + $this->assertStringNotContainsString('BuyerFirstName', $rendered); |
| 162 | + $this->assertStringNotContainsString('BuyerLastName', $rendered); |
| 163 | + $this->assertStringNotContainsString('buyer@example.com', $rendered); |
| 164 | + } |
| 165 | + |
| 166 | + public function test_toarray_contains_payout_ids_and_total(): void |
| 167 | + { |
| 168 | + $developer = User::factory()->create(); |
| 169 | + $developerAccount = DeveloperAccount::factory() |
| 170 | + ->onboarded() |
| 171 | + ->withAcceptedTerms() |
| 172 | + ->create(['user_id' => $developer->id]); |
| 173 | + |
| 174 | + $plugin = Plugin::factory()->paid()->create([ |
| 175 | + 'user_id' => $developer->id, |
| 176 | + 'developer_account_id' => $developerAccount->id, |
| 177 | + ]); |
| 178 | + |
| 179 | + $license = PluginLicense::factory()->create([ |
| 180 | + 'plugin_id' => $plugin->id, |
| 181 | + 'stripe_invoice_id' => 'in_test_arr', |
| 182 | + 'price_paid' => 2900, |
| 183 | + ]); |
| 184 | + |
| 185 | + $payout = PluginPayout::create([ |
| 186 | + 'plugin_license_id' => $license->id, |
| 187 | + 'developer_account_id' => $developerAccount->id, |
| 188 | + 'gross_amount' => 2900, |
| 189 | + 'platform_fee' => 870, |
| 190 | + 'developer_amount' => 2030, |
| 191 | + 'status' => PayoutStatus::Pending, |
| 192 | + ]); |
| 193 | + |
| 194 | + $notification = new PluginSaleCompleted(collect([$payout])); |
| 195 | + $array = $notification->toArray($developer); |
| 196 | + |
| 197 | + $this->assertEquals([$payout->id], $array['payout_ids']); |
| 198 | + $this->assertEquals(2030, $array['total_developer_amount']); |
| 199 | + } |
| 200 | +} |
0 commit comments