Skip to content

Instantly share code, notes, and snippets.

@shanerbaner82
Created October 25, 2020 02:12
Show Gist options
  • Save shanerbaner82/8167634293ca35641a0428021370b350 to your computer and use it in GitHub Desktop.
Save shanerbaner82/8167634293ca35641a0428021370b350 to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Feature;
use App\Models\Country;
use App\Models\Order;
use App\Models\Subscription;
use App\Models\User;
use App\Models\UserAddress;
use App\Utils\AtollaAPIFacade;
use Facades\App\Utils\AtollaAPI;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class SubscriptionTests extends TestCase
{
use WithFaker, RefreshDatabase;
/** @test */
public function if_user_has_no_payment_method_show_errors()
{
$this->withoutExceptionHandling();
$user = factory(User::class)->create(['password' => Hash::make('password'), 'app_user_id' => $this->faker->word]);
auth('api')->login($user);
$response = $this->post('/api/subscription')->assertStatus(400);
$response->assertJson([
'result' => 'error',
'data' => [
'errors' => [
'This customer has no attached payment source or default payment method.'
]
]
]);
}
/** @test */
public function if_user_already_subscribed_show_errors()
{
$this->withoutExceptionHandling();
$user = factory(User::class)->create(['password' => Hash::make('password'), 'app_user_id' => $this->faker->word]);
$user->newSubscription(User::SUBSCRIPTION_NAME, User::MONTHLY_SUBSCRIPTION_PLAN)->create('tok_us');
auth('api')->login($user);
$response = $this->post('/api/subscription')->assertStatus(400);
$response->assertJson([
'result' => 'error',
'errors' => ['You already have a subscription.'],
]);
}
/** @test */
public function a_user_can_add_a_payment_method()
{
$this->withoutExceptionHandling();
$user = factory(User::class)->create(['password' => Hash::make('password'), 'app_user_id' => $this->faker->word]);
auth('api')->login($user);
$this->post('/api/user/cards', ['stripeToken' => 'tok_us'])->assertStatus(200);
$this->assertDatabaseHas('users', [
'card_last_four' => '4242',
'card_brand' => 'Visa',
'card_exp_month' => (int)now()->format('m'),
'card_exp_year' => (int )now()->addYear()->format('Y')
]);
}
/** @test */
public function a_user_with_a_payment_method_can_subscribe()
{
$this->withoutExceptionHandling();
$this->loginUser();
dd(Country::count());
$this->post('/api/user/cards', ['stripeToken' => 'tok_us'])->assertStatus(200);
AtollaAPIFacade::shouldReceive('getPersonalizedSerum')->andReturn($this->getPersonalizedSerum());
AtollaAPIFacade::shouldReceive('postSkinSequence')->andReturn($this->postSkinSequence());
AtollaAPIFacade::shouldReceive('getSkinConcern')->andReturn($this->getSkinConcern());
AtollaAPIFacade::shouldReceive('getBasicSkinInfo')->andReturn($this->getBasicSkinInfo());
AtollaAPIFacade::shouldReceive('getSkinSummary')->andReturn($this->getSkinSummary());
$this->post('/api/subscription')->assertStatus(200);
$this->assertTrue(Subscription::count() === 1);
$this->assertTrue(Order::count() === 1);
$this->assertDatabaseHas('subscriptions', [
'user_id' => auth()->id(),
'name' => User::SUBSCRIPTION_NAME,
'quantity' => 1,
'status' => 'active'
]);
}
public function loginUser()
{
$this->seed();
$password = 'eightcharacterslong';
$email = $this->faker->email;
$name = $this->faker->name;
$this->post('/api/auth/register', [
'email' => $email,
'password' => $password,
'password_confirmation' => $password,
'name' => $name
])->assertStatus(200);
factory(UserAddress::class)->create(['user_id' => auth('api')->id(), 'default_shipping' => 1, 'default_billing' => 0]);
factory(UserAddress::class)->create(['user_id' => auth('api')->id(), 'default_shipping' => 0, 'default_billing' => 1]);
// $this->post('/api/auth/login', [
// 'email' => $email,
// 'password' => $password
// ]);
}
private function getPersonalizedSerum()
{
return json_decode(file_get_contents(base_path('/tests/Datasets/Serum.json')), true);
}
private function postSkinSequence()
{
return json_decode(file_get_contents(base_path('/tests/Datasets/SkinSequence.json')), true);
}
private function getSkinConcern()
{
return json_decode(file_get_contents(base_path('/tests/Datasets/SkinConcern.json')), true);
}
private function getBasicSkinInfo()
{
return json_decode(file_get_contents(base_path('/tests/Datasets/BasicSkinInfo.json')), true);
}
private function getSkinSummary()
{
return json_decode(file_get_contents(base_path('/tests/Datasets/SkinSummary.json')), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment