2019-09-18 14:43:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
use Tests\MockAccountData;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Models\Client
|
|
|
|
*/
|
|
|
|
class ClientModelTest extends TestCase
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MockAccountData;
|
2019-09-18 14:43:37 +02:00
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
public function setUp() :void
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->makeTestData();
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (config('ninja.testvars.travis') !== false) {
|
2019-10-07 08:43:19 +02:00
|
|
|
$this->markTestSkipped('Skip test for Travis');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-12-17 11:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (!config('ninja.testvars.stripe')) {
|
2019-12-17 11:50:45 +01:00
|
|
|
$this->markTestSkipped('Skip test no company gateways installed');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-04 12:20:02 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testPaymentMethods()
|
|
|
|
{
|
|
|
|
$amount = 40;
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$company_gateways = $this->client->getSetting('company_gateway_ids');
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
//todo create a test where we actually SET a value in the settings->company_gateways object and test if we can harvest.
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($company_gateways) {
|
2019-10-24 06:46:24 +02:00
|
|
|
$gateways = $this->company->company_gateways->whereIn('id', $payment_gateways);
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2019-10-24 06:46:24 +02:00
|
|
|
$gateways = $this->company->company_gateways;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$this->assertNotNull($gateways);
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$pre_count = $gateways->count();
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$gateways->filter(function ($method) use ($amount) {
|
|
|
|
if ($method->min_limit !== null && $amount < $method->min_limit) {
|
2019-10-24 06:46:24 +02:00
|
|
|
return false;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($method->max_limit !== null && $amount > $method->min_limit) {
|
2019-10-24 06:46:24 +02:00
|
|
|
return false;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
|
|
|
});
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$post_count = $gateways->count();
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$this->assertEquals($pre_count, $post_count);
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$payment_methods = [];
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($gateways as $gateway) {
|
|
|
|
foreach ($gateway->driver($this->client)->gatewayTypes() as $type) {
|
|
|
|
$payment_methods[] = [$gateway->id => $type];
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$this->assertEquals(8, count($payment_methods));
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$payment_methods_collections = collect($payment_methods);
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
//** Plucks the remaining keys into its own collection
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment_methods_intersect = $payment_methods_collections->intersectByKeys($payment_methods_collections->flatten(1)->unique());
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$this->assertEquals(4, $payment_methods_intersect->count());
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$payment_urls = [];
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($payment_methods_intersect as $key => $child_array) {
|
|
|
|
foreach ($child_array as $gateway_id => $gateway_type_id) {
|
|
|
|
$gateway = $gateways->where('id', $gateway_id)->first();
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertNotNull($gateway);
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$fee_label = $gateway->calcGatewayFeeLabel($amount, $this->client);
|
2019-09-18 14:43:37 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$payment_urls[] = [
|
2019-10-24 06:46:24 +02:00
|
|
|
'label' => ctrans('texts.' . $gateway->getTypeAlias($gateway_type_id)) . $fee_label,
|
|
|
|
'url' => URL::signedRoute('client.payments.process', [
|
|
|
|
'company_gateway_id' => $gateway_id,
|
|
|
|
'gateway_type_id' => $gateway_type_id])
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
|
|
|
|
|
2019-10-24 06:46:24 +02:00
|
|
|
$this->assertEquals(4, count($payment_urls));
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-09-18 14:43:37 +02:00
|
|
|
}
|