Skip to content

Commit

Permalink
Merge pull request #881 from laravel/rename-some-exceptions
Browse files Browse the repository at this point in the history
[11.x] Rename some exceptions
  • Loading branch information
taylorotwell authored Feb 28, 2020
2 parents 0b6868a + 93d0f48 commit 3b6eaff
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 40 deletions.
13 changes: 8 additions & 5 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Exception;
use Illuminate\Support\Collection;
use Laravel\Cashier\Exceptions\CustomerAlreadyCreated;
use Laravel\Cashier\Exceptions\InvalidCustomer;
use Laravel\Cashier\Exceptions\InvalidInvoice;
use Laravel\Cashier\Exceptions\InvalidStripeCustomer;
use Stripe\BankAccount as StripeBankAccount;
use Stripe\Card as StripeCard;
use Stripe\Customer as StripeCustomer;
Expand Down Expand Up @@ -738,12 +739,12 @@ public function hasStripeId()
*
* @return void
*
* @throws \Laravel\Cashier\Exceptions\InvalidStripeCustomer
* @throws \Laravel\Cashier\Exceptions\InvalidCustomer
*/
protected function assertCustomerExists()
{
if (! $this->hasStripeId()) {
throw InvalidStripeCustomer::nonCustomer($this);
throw InvalidCustomer::notYetCreated($this);
}
}

Expand All @@ -752,11 +753,13 @@ protected function assertCustomerExists()
*
* @param array $options
* @return \Stripe\Customer
*
* @throws \Laravel\Cashier\Exceptions\CustomerAlreadyCreated
*/
public function createAsStripeCustomer(array $options = [])
{
if ($this->hasStripeId()) {
throw InvalidStripeCustomer::exists($this);
if (! $this->hasStripeId()) {
throw CustomerAlreadyCreated::exists($this);
}

$options = array_key_exists('email', $options)
Expand Down
19 changes: 19 additions & 0 deletions src/Exceptions/CustomerAlreadyCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Cashier\Exceptions;

use Exception;

class CustomerAlreadyCreated extends Exception
{
/**
* Create a new CustomerAlreadyCreated instance.
*
* @param \Illuminate\Database\Eloquent\Model $owner
* @return static
*/
public static function exists($owner)
{
return new static(class_basename($owner)." is already a Stripe customer with ID {$owner->stripe_id}.");
}
}
19 changes: 19 additions & 0 deletions src/Exceptions/InvalidCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Cashier\Exceptions;

use Exception;

class InvalidCustomer extends Exception
{
/**
* Create a new InvalidCustomer instance.
*
* @param \Illuminate\Database\Eloquent\Model $owner
* @return static
*/
public static function notYetCreated($owner)
{
return new static(class_basename($owner).' is not a Stripe customer yet. See the createAsStripeCustomer method.');
}
}
30 changes: 0 additions & 30 deletions src/Exceptions/InvalidStripeCustomer.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Feature/InvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Laravel\Cashier\Tests\Feature;

use Laravel\Cashier\Exceptions\InvalidCustomer;
use Laravel\Cashier\Exceptions\InvalidInvoice;
use Laravel\Cashier\Exceptions\InvalidStripeCustomer;
use Laravel\Cashier\Invoice;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

Expand All @@ -13,7 +13,7 @@ public function test_require_stripe_customer_for_invoicing()
{
$user = $this->createCustomer('require_stripe_customer_for_invoicing');

$this->expectException(InvalidStripeCustomer::class);
$this->expectException(InvalidCustomer::class);

$user->invoice();
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Laravel\Cashier\Tests\Unit;

use Carbon\Carbon;
use Laravel\Cashier\Exceptions\InvalidStripeCustomer;
use Laravel\Cashier\Exceptions\CustomerAlreadyCreated;
use Laravel\Cashier\Exceptions\InvalidCustomer;
use Laravel\Cashier\Tests\Fixtures\User;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -47,7 +48,7 @@ public function test_stripe_customer_method_throws_exception_when_stripe_id_is_n
{
$user = new User;

$this->expectException(InvalidStripeCustomer::class);
$this->expectException(InvalidCustomer::class);

$user->asStripeCustomer();
}
Expand All @@ -57,7 +58,7 @@ public function test_stripe_customer_cannot_be_created_when_stripe_id_is_already
$user = new User();
$user->stripe_id = 'foo';

$this->expectException(InvalidStripeCustomer::class);
$this->expectException(CustomerAlreadyCreated::class);

$user->createAsStripeCustomer();
}
Expand Down

0 comments on commit 3b6eaff

Please sign in to comment.