|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\Payments\Tests\Unit; |
|
4
|
|
|
|
|
5
|
|
|
use App\Containers\Paypal\Actions\CreatePaypalAccountAction; |
|
6
|
|
|
use App\Containers\Paypal\Services\ChargeWithPaypalService; |
|
7
|
|
|
use App\Containers\Stripe\Actions\CreateStripeAccountAction; |
|
8
|
|
|
use App\Containers\Stripe\Services\ChargeWithStripeService; |
|
9
|
|
|
use App\Port\Tests\PHPUnit\Abstracts\TestCase; |
|
10
|
|
|
use App\Containers\Payments\Services\PaymentsFactory; |
|
11
|
|
|
use Illuminate\Support\Facades\App; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ChargeUsersTest. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
View Code Duplication |
class ChargeUsersTest extends TestCase |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
public function testChargeWithStripe() |
|
22
|
|
|
{ |
|
23
|
|
|
// get the logged in user (create one if no one is logged in) |
|
24
|
|
|
$user = $this->registerAndLoginTestingUser(); |
|
25
|
|
|
|
|
26
|
|
|
// create stripe account for this user |
|
27
|
|
|
$createStripeAccountAction = App::make(CreateStripeAccountAction::class); |
|
28
|
|
|
$stripeAccount = $createStripeAccountAction->run($user, 'cus_8mBD5S1SoyD4zL', 'card_18Uck6KFvMcBUkvQorbBkYhR', 'credit', '4242', 'WsNM4K8puHbdS2VP'); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$payId = 'ch_z8WDARKFvzcBUkvQzrbBvfhz'; |
|
31
|
|
|
|
|
32
|
|
|
// mock the ChargeWithStripeService external API call |
|
33
|
|
|
$this->mock(ChargeWithStripeService::class)->shouldReceive('charge')->andReturn([ |
|
34
|
|
|
'payment_method' => 'stripe', |
|
35
|
|
|
'description' => $payId |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
$paymentsFactory = new PaymentsFactory(); |
|
39
|
|
|
$result = $paymentsFactory->charge($user, 1000, 'USD'); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals($result['payment_method'], 'stripe'); |
|
42
|
|
|
$this->assertEquals($result['description'], $payId); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testChargeWithPaypal() |
|
46
|
|
|
{ |
|
47
|
|
|
// get the logged in user (create one if no one is logged in) |
|
48
|
|
|
$user = $this->registerAndLoginTestingUser(); |
|
49
|
|
|
|
|
50
|
|
|
// create stripe account for this user |
|
51
|
|
|
$createPaypalAccountAction = App::make(CreatePaypalAccountAction::class); |
|
52
|
|
|
$paypalAccount = $createPaypalAccountAction->run($user, '8mBD5S1SoyD4zL'); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$payId = 'PAY-04797768K5905283VK6DGEMZ'; |
|
55
|
|
|
|
|
56
|
|
|
// mock the ChargeWithPaypalService external API call |
|
57
|
|
|
$this->mock(ChargeWithPaypalService::class)->shouldReceive('charge')->andReturn([ |
|
58
|
|
|
'payment_method' => 'paypal', |
|
59
|
|
|
'description' => $payId |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
// TODO: comment out the pocking part above and test the real API call |
|
63
|
|
|
|
|
64
|
|
|
$paymentsFactory = new PaymentsFactory(); |
|
65
|
|
|
$result = $paymentsFactory->charge($user, 1000, 'USD'); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals($result['payment_method'], 'paypal'); |
|
68
|
|
|
$this->assertEquals($result['description'], $payId); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.