Fixes for tests

This commit is contained in:
David Bomba 2024-06-18 12:57:15 +10:00
parent cb0385dd39
commit e349245c45
5 changed files with 51 additions and 46 deletions

View File

@ -66,7 +66,7 @@ class EmailQuotaNotification extends Notification
{ {
$content = "Email quota exceeded by Account {$this->account->key} \n"; $content = "Email quota exceeded by Account {$this->account->key} \n";
$owner = $this->account->companies()->first()->owner(); $owner = $this->account->companies()->first()->owner() ?? $this->account->users()->orderBy('id','asc')->first();
$owner_name = $owner->present()->name() ?? 'No Owner Found'; $owner_name = $owner->present()->name() ?? 'No Owner Found';
$owner_email = $owner->email ?? 'No Owner Email Found'; $owner_email = $owner->email ?? 'No Owner Email Found';

View File

@ -21,10 +21,6 @@ class PaymentMethod
{ {
use MakesHash; use MakesHash;
private $client;
private $amount;
/** @var \Illuminate\Support\Collection<CompanyGateway> $gateways **/ /** @var \Illuminate\Support\Collection<CompanyGateway> $gateways **/
private $gateways; private $gateways;
@ -32,10 +28,8 @@ class PaymentMethod
private $payment_urls = []; private $payment_urls = [];
public function __construct(Client $client, float $amount) public function __construct(private Client $client, private float $amount)
{ {
$this->client = $client;
$this->amount = $amount;
} }
public function run() public function run()
@ -105,7 +99,6 @@ class PaymentMethod
$transformed_ids = []; $transformed_ids = [];
} }
$this->gateways = $this->client $this->gateways = $this->client
->company ->company
->company_gateways ->company_gateways
@ -140,7 +133,7 @@ class PaymentMethod
foreach ($gateway->driver($this->client)->gatewayTypes() as $type) { foreach ($gateway->driver($this->client)->gatewayTypes() as $type) {
if (isset($gateway->fees_and_limits) && is_object($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) { if (isset($gateway->fees_and_limits) && is_object($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) {
if ($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $this->amount) && $gateway->fees_and_limits->{$type}->is_enabled) { if ($this->validGatewayForAmount($gateway->fees_and_limits->{$type}) && $gateway->fees_and_limits->{$type}->is_enabled) {
$this->payment_methods[] = [$gateway->id => $type]; $this->payment_methods[] = [$gateway->id => $type];
} }
} else { } else {
@ -159,8 +152,8 @@ class PaymentMethod
{ {
foreach ($type as $gateway_id => $gateway_type_id) foreach ($type as $gateway_id => $gateway_type_id)
{ {
$gate = $this->gateways->where('id',$gateway_id)->first(); $gate = $this->gateways->where('id', $gateway_id)->first();
$this->buildUrl($gate, $gateway_type_id); $this->buildUrl($gate, $gateway_type_id);
} }
} }
@ -211,6 +204,7 @@ class PaymentMethod
]; ];
} }
return $this;
} }
//@deprecated as buildUrl() supercedes //@deprecated as buildUrl() supercedes
@ -256,14 +250,14 @@ class PaymentMethod
return $this; return $this;
} }
private function validGatewayForAmount($fees_and_limits_for_payment_type, $amount): bool private function validGatewayForAmount($fees_and_limits_for_payment_type): bool
{ {
if (isset($fees_and_limits_for_payment_type)) { if (isset($fees_and_limits_for_payment_type)) {
$fees_and_limits = $fees_and_limits_for_payment_type; $fees_and_limits = $fees_and_limits_for_payment_type;
} else { } else {
return true; return true;
} }
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && ($this->amount < $fees_and_limits->min_limit && $this->amount != -1)) { if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && ($this->amount < $fees_and_limits->min_limit && $this->amount != -1)) {
return false; return false;
} }

View File

@ -38,12 +38,15 @@ class ClientModelTest extends TestCase
if (! config('ninja.testvars.stripe')) { if (! config('ninja.testvars.stripe')) {
$this->markTestSkipped('Skip test no company gateways installed'); $this->markTestSkipped('Skip test no company gateways installed');
} }
if(CompanyGateway::count() == 0)
$this->markTestSkipped('Skip test no company gateways installed');
} }
public function testPaymentMethodsWithCreditsEnforced() public function testPaymentMethodsWithCreditsEnforced()
{ {
$amount = 40;
$payment_methods = $this->client->service()->getPaymentMethods(40); $payment_methods = $this->client->service()->getPaymentMethods(40);
$this->assertGreaterThan(0, CompanyGateway::count()); $this->assertGreaterThan(0, CompanyGateway::count());

View File

@ -54,7 +54,9 @@ class CompanyGatewayResolutionTest extends TestCase
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
CompanyGateway::whereNotNull('id')->delete(); CompanyGateway::query()->withTrashed()->cursor()->each(function ($cg){
$cg->forceDelete();
});
$data = []; $data = [];
$data[1]['min_limit'] = -1; $data[1]['min_limit'] = -1;
@ -123,11 +125,14 @@ class CompanyGatewayResolutionTest extends TestCase
{ {
$amount = 10; $amount = 10;
$this->client->country_id = 840;
$this->client->save();
$this->assertInstanceOf('\\stdClass', $this->cg->fees_and_limits); $this->assertInstanceOf('\\stdClass', $this->cg->fees_and_limits);
// $this->assertObjectHasAttribute('min_limit', $this->cg->fees_and_limits->{1}); // $this->assertObjectHasAttribute('min_limit', $this->cg->fees_and_limits->{1});
$this->assertNotNull($this->cg->fees_and_limits->{1}->min_limit); $this->assertNotNull($this->cg->fees_and_limits->{1}->min_limit);
$payment_methods = $this->client->service()->getPaymentMethods($amount); $payment_methods = $this->client->service()->getPaymentMethods($amount);
$this->assertEquals(2, count($payment_methods)); $this->assertEquals(2, count($payment_methods));
} }
@ -135,7 +140,9 @@ class CompanyGatewayResolutionTest extends TestCase
{ {
$amount = 10; $amount = 10;
CompanyGateway::whereNotNull('id')->delete(); CompanyGateway::query()->withTrashed()->cursor()->each(function ($cg) {
$cg->forceDelete();
});
$data = []; $data = [];
$data[1]['min_limit'] = -1; $data[1]['min_limit'] = -1;
@ -181,9 +188,10 @@ class CompanyGatewayResolutionTest extends TestCase
$this->cg->fees_and_limits = $data; $this->cg->fees_and_limits = $data;
$this->cg->save(); $this->cg->save();
// nlog($this->client->service()->getPaymentMethods($amount)); $this->client->country_id = 840;
$this->client->save();
$this->assertEquals(2, count($this->client->service()->getPaymentMethods($amount))); $this->assertEquals(1, count($this->client->service()->getPaymentMethods($amount)));
} }
public function testEnableFeeAdjustment() public function testEnableFeeAdjustment()

View File

@ -203,32 +203,32 @@ trait MockAccountData
{ {
config(['database.default' => config('ninja.db.default')]); config(['database.default' => config('ninja.db.default')]);
/* Warm up the cache !*/ // /* Warm up the cache !*/
$cached_tables = config('ninja.cached_tables'); // $cached_tables = config('ninja.cached_tables');
Artisan::call('db:seed', [ // Artisan::call('db:seed', [
'--force' => true // '--force' => true
]); // ]);
foreach ($cached_tables as $name => $class) { // foreach ($cached_tables as $name => $class) {
// check that the table exists in case the migration is pending // // check that the table exists in case the migration is pending
if (! Schema::hasTable((new $class())->getTable())) { // if (! Schema::hasTable((new $class())->getTable())) {
continue; // continue;
} // }
if ($name == 'payment_terms') { // if ($name == 'payment_terms') {
$orderBy = 'num_days'; // $orderBy = 'num_days';
} elseif ($name == 'fonts') { // } elseif ($name == 'fonts') {
$orderBy = 'sort_order'; // $orderBy = 'sort_order';
} elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) { // } elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) {
$orderBy = 'name'; // $orderBy = 'name';
} else { // } else {
$orderBy = 'id'; // $orderBy = 'id';
} // }
$tableData = $class::orderBy($orderBy)->get(); // $tableData = $class::orderBy($orderBy)->get();
if ($tableData->count()) { // if ($tableData->count()) {
Cache::forever($name, $tableData); // Cache::forever($name, $tableData);
} // }
} // }
$this->faker = \Faker\Factory::create(); $this->faker = \Faker\Factory::create();
$fake_email = $this->faker->email(); $fake_email = $this->faker->email();
@ -808,7 +808,7 @@ trait MockAccountData
if (config('ninja.testvars.stripe')) { if (config('ninja.testvars.stripe')) {
$data = []; $data = [];
$data[1]['min_limit'] = 234; $data[1]['min_limit'] = 22;
$data[1]['max_limit'] = 65317; $data[1]['max_limit'] = 65317;
$data[1]['fee_amount'] = 0.00; $data[1]['fee_amount'] = 0.00;
$data[1]['fee_percent'] = 0.000; $data[1]['fee_percent'] = 0.000;