working on type casting

This commit is contained in:
David Bomba 2019-10-10 10:15:35 +11:00
parent 859efb9445
commit 7a4decdd3d
3 changed files with 38 additions and 16 deletions

View File

@ -207,6 +207,8 @@ class CompanyController extends BaseController
$company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account);
$company->saveSettings($request->input('settings'));
$this->uploadLogo($request->file('company_logo'), $company, $company);
auth()->user()->companies()->attach($company->id, [

View File

@ -41,6 +41,8 @@ trait CompanySettingsSaver
foreach($settings as $key => $value)
$company_settings->{$key} = $value;
//$company_settings = CompanySettings::setCasts($company_settings, CompanySettings::$casts);
$this->settings = $company_settings;
$this->save();
}
@ -57,10 +59,9 @@ trait CompanySettingsSaver
$value = "integer";
if($this->checkAttribute($value, $settings->{$key})){
\Log::error("System says true {$key} a {$value} = ".$settings->{$key});
settype($settings->{$key}, $value);
}
else {
\Log::error('popping '.$key.' '.$value.' '.$settings->{$key}.' off the stack');
unset($settings->{$key});
}
@ -69,20 +70,18 @@ trait CompanySettingsSaver
/* Handles unset settings or blank strings */
if(is_null($settings->{$key}) || !isset($settings->{$key}) || $settings->{$key} == ''){
continue;
}
/*Catch all filter */
if($this->checkAttribute($value, $settings->{$key})){
\Log::error("System says true {$key} a {$value} = ".$settings->{$key});
settype($settings->{$key}, $value);
}
else {
unset($settings->{$key});
}
}
\Log::error(print_r($settings,1));
return $settings;
}
@ -93,8 +92,6 @@ trait CompanySettingsSaver
{
case 'int':
case 'integer':
//return is_int($value);
//return strval($value) === strval(intval($value)) ;
return ctype_digit(strval($value));
case 'real':
case 'float':
@ -104,7 +101,8 @@ trait CompanySettingsSaver
return method_exists($value, '__toString' ) || is_null($value) || is_string($value);
case 'bool':
case 'boolean':
return is_bool($value);
//\Log::error("is {$value} boolean ? = ".is_bool($value) || (int) filter_var($value, FILTER_VALIDATE_BOOLEAN));
return is_bool($value) || (int) filter_var($value, FILTER_VALIDATE_BOOLEAN);
case 'object':
return is_object($value);
case 'array':
@ -117,13 +115,7 @@ trait CompanySettingsSaver
}
}
// \Log::error('popping '.$key.' '.$value.' '.$settings->{$key}.' off the stack');
// \Log::error('popping '.$key.' '.$value.' '.$settings->{$key}.' off the stack');
}

View File

@ -22,6 +22,7 @@ use Tests\TestCase;
/**
* @test
* @covers App\Utils\Traits\CompanySettingsSaver
*/
class CompanySettingsTest extends TestCase
{
@ -117,5 +118,32 @@ class CompanySettingsTest extends TestCase
$this->assertEquals($arr['data']['settings']['tax_rate3'],10.5);
}
public function testBoolEdgeCases()
{
$settings = $this->company->settings;
$settings->require_invoice_signature = true;
$settings->require_quote_signature = true;
$settings->show_accept_quote_terms = false;
$settings->show_accept_invoice_terms = "TRUE";
$settings->show_tasks_in_portal = "FALSE";
$this->company->settings = $settings;
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-Token' => $this->token,
])->put('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $this->company->toArray());
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($arr['data']['settings']['require_invoice_signature'],1);
$this->assertEquals($arr['data']['settings']['require_quote_signature'],1);
$this->assertEquals($arr['data']['settings']['show_accept_quote_terms'],0);
$this->assertEquals($arr['data']['settings']['show_accept_invoice_terms'],1);
$this->assertEquals($arr['data']['settings']['show_tasks_in_portal'],0);
}
}