Skip to content

Instantly share code, notes, and snippets.

@karakanb
Last active June 24, 2020 18:15
Show Gist options
  • Save karakanb/f1fdf77827aefd55f90cdc791f1872c3 to your computer and use it in GitHub Desktop.
Save karakanb/f1fdf77827aefd55f90cdc791f1872c3 to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Feature;
use App\User;
use Laravel\Cashier\SubscriptionBuilder;
use Tests\TestCase;
use function route;
class PlansTest extends TestCase
{
public function testSubscriptionIsSuccessfullyCreatedWithCoupon()
{
// Disable the authentication middleware, we don't need it.
$this->withoutMiddleware([Authenticate::class]);
// Prepare request variables.
$paymentMethodId = 'payment-method';
$couponCode = 'SOME_COUPON';
// Prepare the subscription builder mock.
$subscriptionBuilder = $this->prophesize(SubscriptionBuilder::class);
$subscriptionBuilder->withCoupon($couponCode)
->shouldBeCalled();
$subscriptionBuilder->create($paymentMethodId)
->shouldBeCalled();
// Prepare the user mock
$user = $this->prophesize(User::class);
$user->newSubscription('default', 'standard')
->shouldBeCalled()
->willReturn($subscriptionBuilder->reveal());
// Perform the request as our user mock.
$testRoute = route('plans.save', ['plan' => 'standard']);
$response = $this->actingAs($user->reveal())
->post($testRoute, [
'paymentMethodId' => $paymentMethodId,
'couponCode' => $couponCode,
]);
// Assert that everything went smoothly.
$response->assertRedirect(route('home'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment