Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Use Mollie mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sandervanhooft committed Oct 8, 2020
1 parent 5da3054 commit a81729d
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 29 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<groups>
<exclude>
<group>generate_new_invoice_template</group>
<group>mollie_integration</group>
</exclude>
</groups>
<php>
Expand Down
11 changes: 8 additions & 3 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

abstract class BaseTestCase extends TestCase
{
protected $interactWithMollieAPI = false;

/**
* Setup the test environment.
*/
Expand All @@ -31,7 +33,10 @@ protected function setUp(): void
config(['cashier.webhook_url' => 'https://www.example.com/webhook']);
config(['cashier.first_payment.webhook_url' => 'https://www.example.com/mandate-webhook']);

$this->mock(MollieApiWrapper::class, null); // TODO temporary check
if(! $this->interactWithMollieAPI) {
// Disable the Mollie API
$this->mock(MollieApiWrapper::class, null);
}
}

/**
Expand Down Expand Up @@ -231,14 +236,14 @@ protected function getMandatedCustomer()
protected function getMandatedUser($persist = true, $overrides = [])
{
return $this->getCustomerUser($persist, array_merge([
'mollie_mandate_id' => $this->getMandateId(),
'mollie_mandate_id' => 'mdt_unique_mandate_id',
], $overrides));
}

protected function getCustomerUser($persist = true, $overrides = [])
{
return $this->getUser($persist, array_merge([
'mollie_customer_id' => $this->getMandatedCustomerId(),
'mollie_customer_id' => 'cst_unique_customer_id',
], $overrides));
}

Expand Down
49 changes: 49 additions & 0 deletions tests/FirstPayment/Actions/StartSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
use Laravel\Cashier\Coupon\AppliedCoupon;
use Laravel\Cashier\Coupon\RedeemedCoupon;
use Laravel\Cashier\FirstPayment\Actions\StartSubscription;
use Laravel\Cashier\Mollie\Contracts\GetMollieMandate;
use Laravel\Cashier\Mollie\GetMollieCustomer;
use Laravel\Cashier\Order\OrderItem;
use Laravel\Cashier\Order\OrderItemCollection;
use Laravel\Cashier\Tests\BaseTestCase;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Customer;
use Mollie\Api\Resources\Mandate;

class StartSubscriptionTest extends BaseTestCase
{
Expand Down Expand Up @@ -275,6 +280,8 @@ public function canStartDefaultSubscription()
$user = $this->getMandatedUser(true, [
'trial_ends_at' => now()->addWeek(), // on generic trial
]);
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();

$this->assertFalse($user->subscribed('default'));

Expand Down Expand Up @@ -313,6 +320,8 @@ public function canStartDefaultSubscription()
public function canStartSubscriptionWithTrialDays()
{
$user = $this->getMandatedUser(true, ['tax_percentage' => 20]);
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();

$this->assertFalse($user->subscribed('default'));

Expand Down Expand Up @@ -361,8 +370,11 @@ public function canStartSubscriptionWithTrialDays()
/** @test */
public function canStartSubscriptionWithTrialUntil()
{
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$user = $this->getMandatedUser();


$this->assertFalse($user->subscribed('default'));

$action = new StartSubscription(
Expand Down Expand Up @@ -406,6 +418,8 @@ public function canStartSubscriptionWithTrialUntil()
/** @test */
public function canStartSubscriptionWithQuantityNoTrial()
{
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$user = $this->getMandatedUser();

$this->assertFalse($user->subscribed('default'));
Expand Down Expand Up @@ -452,6 +466,8 @@ public function canStartSubscriptionWithQuantityNoTrial()
/** @test */
public function canStartSubscriptionWithQuantityAndTrialUntil()
{
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$user = $this->getMandatedUser();

$this->assertFalse($user->subscribed('default'));
Expand Down Expand Up @@ -501,6 +517,8 @@ public function canStartSubscriptionWithQuantityAndTrialUntil()
public function canStartSubscriptionWithCouponNoTrial()
{
$this->withMockedCouponRepository();
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$user = $this->getMandatedUser();

$this->assertFalse($user->subscribed('default'));
Expand Down Expand Up @@ -552,6 +570,8 @@ public function canStartSubscriptionWithCouponNoTrial()
public function canStartSubscriptionWithCouponAndTrial()
{
$this->withMockedCouponRepository();
$this->withMockedGetMollieCustomer();
$this->withMockedGetMollieMandate();
$user = $this->getMandatedUser(true, ['tax_percentage' => 20]);

$this->assertFalse($user->subscribed('default'));
Expand Down Expand Up @@ -632,4 +652,33 @@ protected function assertFromPayloadToPayload($overrides = [])

$this->assertEquals($payload, $result);
}

protected function withMockedGetMollieCustomer($customerId = 'cst_unique_customer_id', $times = 1): void
{
$this->mock(GetMollieCustomer::class, function ($mock) use ($customerId, $times) {
$customer = new Customer(new MollieApiClient);
$customer->id = $customerId;

return $mock->shouldReceive('execute')->with($customerId)->times($times)->andReturn($customer);
});
}

protected function withMockedGetMollieMandate($attributes = [[
'mandateId' => 'mdt_unique_mandate_id',
'customerId' => 'cst_unique_customer_id',
]], $times = 1): void
{
$this->mock(GetMollieMandate::class, function ($mock) use ($times, $attributes) {
foreach ($attributes as $data) {
$mandate = new Mandate(new MollieApiClient);
$mandate->id = $data['mandateId'];
$mandate->status = 'valid';
$mandate->method = 'directdebit';

$mock->shouldReceive('execute')->with($data['customerId'], $data['mandateId'])->times($times)->andReturn($mandate);
}

return $mock;
});
}
}
10 changes: 10 additions & 0 deletions tests/FirstPayment/FirstPaymentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Laravel\Cashier\FirstPayment\Actions\AddBalance;
use Laravel\Cashier\FirstPayment\Actions\AddGenericOrderItem;
use Laravel\Cashier\FirstPayment\FirstPaymentBuilder;
use Laravel\Cashier\Mollie\Contracts\CreateMollieCustomer;
use Laravel\Cashier\Mollie\Contracts\CreateMolliePayment;
use Laravel\Cashier\Mollie\GetMollieCustomer;
use Laravel\Cashier\Tests\BaseTestCase;
use Laravel\Cashier\Tests\Fixtures\User;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Customer;
use Mollie\Api\Resources\Payment;
use Mollie\Api\Types\SequenceType;

Expand All @@ -18,6 +21,13 @@ protected function setUp(): void
{
parent::setUp();
$this->withPackageMigrations();
$customer = new Customer(new MollieApiClient);
$customer->id = 'cst_unique_customer_id';

$this->mock(CreateMollieCustomer::class, function ($mock) use ($customer) {
return $mock->shouldReceive('execute')
->andReturn($customer);
});
}

/** @test */
Expand Down
81 changes: 77 additions & 4 deletions tests/ManageSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
use Laravel\Cashier\Events\SubscriptionResumed;
use Laravel\Cashier\Events\SubscriptionStarted;
use Laravel\Cashier\Events\SubscriptionQuantityUpdated;
use Laravel\Cashier\Mollie\Contracts\CreateMolliePayment;
use Laravel\Cashier\Mollie\Contracts\GetMollieMandate;
use Laravel\Cashier\Mollie\Contracts\GetMollieMethodMinimumAmount;
use Laravel\Cashier\Mollie\GetMollieCustomer;
use Laravel\Cashier\Order\OrderItem;
use Laravel\Cashier\Subscription;
use Laravel\Cashier\Tests\Fixtures\User;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Customer;
use Mollie\Api\Resources\Mandate;
use Mollie\Api\Resources\Payment;

class ManageSubscriptionTest extends BaseTestCase
{
Expand All @@ -28,7 +36,17 @@ protected function setUp(): void
*/
public function canCreateDirectDebitSubscriptionForMandatedCustomer()
{
$this->withMockedGetMollieCustomer('cst_unique_customer_id', 5);
$this->withMockedGetMollieMandate([[
'mandateId' => 'mdt_unique_mandate_id',
'customerId' => 'cst_unique_customer_id',
]], 5);
$this->withMockedGetMollieMethodMinimumAmount(4);
$this->withMockedCreateMolliePayment(4);

$user = $this->getMandatedUser(true, [
'mollie_mandate_id' => 'mdt_unique_mandate_id',
'mollie_customer_id' => 'cst_unique_customer_id',
'tax_percentage' => 10,
'trial_ends_at' => now()->addWeek(),
]);
Expand All @@ -39,7 +57,7 @@ public function canCreateDirectDebitSubscriptionForMandatedCustomer()

Event::fake();

$user->newSubscriptionForMandateId($this->getMandateId(), 'main', 'monthly-10-1')->create();
$user->newSubscriptionForMandateId('mdt_unique_mandate_id', 'main', 'monthly-10-1')->create();

$subscription = $user->subscription('main')->fresh();

Expand Down Expand Up @@ -210,12 +228,21 @@ public function canCreateDirectDebitSubscriptionForMandatedCustomer()

public function testCreatingSubscriptionWithTrial()
{
$user = factory(User::class)->create([
'mollie_customer_id' => $this->getMandatedCustomerId(),
$this->withMockedGetMollieCustomer('cst_unique_customer_id', 1);
$this->withMockedGetMollieMandate([[
'mandateId' => 'mdt_unique_mandate_id',
'customerId' => 'cst_unique_customer_id',
]], 1);

$user = $this->getMandatedUser(true, [
'mollie_mandate_id' => 'mdt_unique_mandate_id',
'mollie_customer_id' => 'cst_unique_customer_id',
'tax_percentage' => 10,
'trial_ends_at' => now()->addWeek(),
]);

// Create Subscription
$user->newSubscriptionForMandateId($this->getMandateId(), 'main', 'monthly-10-1')
$user->newSubscriptionForMandateId('mdt_unique_mandate_id', 'main', 'monthly-10-1')
->trialDays(7)
->create();

Expand Down Expand Up @@ -279,4 +306,50 @@ public function testGenericTrials()
$user->trial_ends_at = Carbon::today()->subDays(5);
$this->assertFalse($user->onGenericTrial());
}

protected function withMockedGetMollieCustomer($customerId = 'cst_unique_customer_id', $times = 1): void
{
$this->mock(GetMollieCustomer::class, function ($mock) use ($customerId, $times) {
$customer = new Customer(new MollieApiClient);
$customer->id = $customerId;

return $mock->shouldReceive('execute')->with($customerId)->times($times)->andReturn($customer);
});
}

protected function withMockedGetMollieMandate($attributes = [[
'mandateId' => 'mdt_unique_mandate_id',
'customerId' => 'cst_unique_customer_id',
]], $times = 1): void
{
$this->mock(GetMollieMandate::class, function ($mock) use ($times, $attributes) {
foreach ($attributes as $data) {
$mandate = new Mandate(new MollieApiClient);
$mandate->id = $data['mandateId'];
$mandate->status = 'valid';
$mandate->method = 'directdebit';

$mock->shouldReceive('execute')->with($data['customerId'], $data['mandateId'])->times($times)->andReturn($mandate);
}

return $mock;
});
}

protected function withMockedGetMollieMethodMinimumAmount($times = 1): void
{
$this->mock(GetMollieMethodMinimumAmount::class, function ($mock) use ($times) {
return $mock->shouldReceive('execute')->with('directdebit', 'EUR')->times($times)->andReturn(money(100, 'EUR'));
});
}

protected function withMockedCreateMolliePayment($times = 1): void
{
$this->mock(CreateMolliePayment::class, function ($mock) use ($times) {
$payment = new Payment(new MollieApiClient);
$payment->id = 'tr_unique_payment_id';

return $mock->shouldReceive('execute')->times($times)->andReturn($payment);
});
}
}
11 changes: 11 additions & 0 deletions tests/Mollie/BaseMollieInteractionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Laravel\Cashier\Tests\Mollie;

use Laravel\Cashier\Tests\BaseTestCase;

abstract class BaseMollieInteractionTest extends BaseTestCase
{
protected $interactWithMollieAPI = true;
}
7 changes: 4 additions & 3 deletions tests/Mollie/CreateMollieCustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
namespace Laravel\Cashier\Tests\Mollie;

use Laravel\Cashier\Mollie\Contracts\CreateMollieCustomer;
use Laravel\Cashier\Tests\BaseTestCase;
use Mollie\Api\Resources\Customer;

class CreateMollieCustomerTest extends BaseTestCase
class CreateMollieCustomerTest extends BaseMollieInteractionTest
{
protected $interactWithMollieAPI = true;

/**
* @test
* @group integration
* @group mollie_integration
*/
public function testExecute()
{
Expand Down
5 changes: 2 additions & 3 deletions tests/Mollie/CreateMolliePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace Laravel\Cashier\Tests\Mollie;

use Laravel\Cashier\Mollie\Contracts\CreateMolliePayment;
use Laravel\Cashier\Tests\BaseTestCase;
use Mollie\Api\Resources\Payment;

class CreateMolliePaymentTest extends BaseTestCase
class CreateMolliePaymentTest extends BaseMollieInteractionTest
{
/**
* @test
* @group integration
* @group mollie_integration
*/
public function testExecute()
{
Expand Down
5 changes: 2 additions & 3 deletions tests/Mollie/GetMollieCustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace Laravel\Cashier\Tests\Mollie;

use Laravel\Cashier\Mollie\Contracts\GetMollieCustomer;
use Laravel\Cashier\Tests\BaseTestCase;
use Mollie\Api\Resources\Customer;

class GetMollieCustomerTest extends BaseTestCase
class GetMollieCustomerTest extends BaseMollieInteractionTest
{
/**
* @test
* @group integration
* @group mollie_integration
*/
public function testExecute()
{
Expand Down
5 changes: 2 additions & 3 deletions tests/Mollie/GetMollieMandateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
namespace Laravel\Cashier\Tests\Mollie;

use Laravel\Cashier\Mollie\Contracts\GetMollieMandate;
use Laravel\Cashier\Tests\BaseTestCase;
use Mollie\Api\Resources\Mandate;

class GetMollieMandateTest extends BaseTestCase
class GetMollieMandateTest extends BaseMollieInteractionTest
{
/**
* @test
* @group integration
* @group mollie_integration
*/
public function testExecute()
{
Expand Down
Loading

0 comments on commit a81729d

Please sign in to comment.