mirror of
https://github.com/Icinga/icingadb-web.git
synced 2026-05-28 04:36:06 -04:00
Model: Implement createDefaults where applicable
This commit is contained in:
parent
30f77db269
commit
1a73126938
4 changed files with 36 additions and 82 deletions
|
|
@ -9,8 +9,6 @@ use ipl\Orm\Relations;
|
|||
|
||||
class Customvar extends Model
|
||||
{
|
||||
protected $accessorsAndMutatorsEnabled = true;
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return 'customvar';
|
||||
|
|
@ -60,16 +58,4 @@ class Customvar extends Model
|
|||
|
||||
$relations->hasMany('customvar_flat', CustomvarFlat::class);
|
||||
}
|
||||
|
||||
protected function mutateValueProperty($json)
|
||||
{
|
||||
if (is_string($json)) {
|
||||
$data = json_decode($json);
|
||||
if ($data !== null) {
|
||||
$json = $data;
|
||||
}
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use Icinga\Module\Icingadb\Common\Auth;
|
|||
use Icinga\Module\Icingadb\Model\Behavior\BoolCast;
|
||||
use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
|
||||
use ipl\Orm\Behaviors;
|
||||
use ipl\Orm\Defaults;
|
||||
use ipl\Orm\Model;
|
||||
use ipl\Orm\Query;
|
||||
use ipl\Orm\Relations;
|
||||
use ipl\Orm\ResultSet;
|
||||
|
||||
|
|
@ -20,8 +20,6 @@ class Host extends Model
|
|||
{
|
||||
use Auth;
|
||||
|
||||
protected $accessorsAndMutatorsEnabled = true;
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return 'host';
|
||||
|
|
@ -147,25 +145,20 @@ class Host extends Model
|
|||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mutates flattened custom vars to an associative array
|
||||
*
|
||||
* @param Query|ResultSet $_
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function mutateVarsProperty($_): array
|
||||
public function createDefaults(Defaults $defaults)
|
||||
{
|
||||
if (! $this->customvar_flat instanceof ResultSet) {
|
||||
$this->applyRestrictions($this->customvar_flat);
|
||||
}
|
||||
$defaults->add('vars', function (self $subject) {
|
||||
if (! $subject->customvar_flat instanceof ResultSet) {
|
||||
$this->applyRestrictions($subject->customvar_flat);
|
||||
}
|
||||
|
||||
$vars = [];
|
||||
foreach ($this->customvar_flat as $customVar) {
|
||||
$vars[$customVar->flatname] = $customVar->flatvalue;
|
||||
}
|
||||
$vars = [];
|
||||
foreach ($subject->customvar_flat as $customVar) {
|
||||
$vars[$customVar->flatname] = $customVar->flatvalue;
|
||||
}
|
||||
|
||||
return $vars;
|
||||
return $vars;
|
||||
});
|
||||
}
|
||||
|
||||
public function createRelations(Relations $relations)
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use Icinga\Module\Icingadb\Common\Auth;
|
|||
use Icinga\Module\Icingadb\Model\Behavior\BoolCast;
|
||||
use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
|
||||
use ipl\Orm\Behaviors;
|
||||
use ipl\Orm\Defaults;
|
||||
use ipl\Orm\Model;
|
||||
use ipl\Orm\Query;
|
||||
use ipl\Orm\Relations;
|
||||
use ipl\Orm\ResultSet;
|
||||
|
||||
|
|
@ -17,8 +17,6 @@ class Service extends Model
|
|||
{
|
||||
use Auth;
|
||||
|
||||
protected $accessorsAndMutatorsEnabled = true;
|
||||
|
||||
public function getTableName()
|
||||
{
|
||||
return 'service';
|
||||
|
|
@ -137,25 +135,20 @@ class Service extends Model
|
|||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mutates flattened custom vars to an associative array
|
||||
*
|
||||
* @param Query|ResultSet $_
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function mutateVarsProperty($_): array
|
||||
public function createDefaults(Defaults $defaults)
|
||||
{
|
||||
if (! $this->customvar_flat instanceof ResultSet) {
|
||||
$this->applyRestrictions($this->customvar_flat);
|
||||
}
|
||||
$defaults->add('vars', function (self $subject) {
|
||||
if (! $subject->customvar_flat instanceof ResultSet) {
|
||||
$this->applyRestrictions($subject->customvar_flat);
|
||||
}
|
||||
|
||||
$vars = [];
|
||||
foreach ($this->customvar_flat as $customVar) {
|
||||
$vars[$customVar->flatname] = $customVar->flatvalue;
|
||||
}
|
||||
$vars = [];
|
||||
foreach ($subject->customvar_flat as $customVar) {
|
||||
$vars[$customVar->flatname] = $customVar->flatvalue;
|
||||
}
|
||||
|
||||
return $vars;
|
||||
return $vars;
|
||||
});
|
||||
}
|
||||
|
||||
public function createRelations(Relations $relations)
|
||||
|
|
|
|||
|
|
@ -15,25 +15,25 @@ class MacrosTest extends TestCase
|
|||
{
|
||||
use Macros;
|
||||
|
||||
const VARS = [
|
||||
'os' => "Ubuntu",
|
||||
'days[0]' => 'mo',
|
||||
'days[1]' => 'tue',
|
||||
'days[2]' => 'wed',
|
||||
'days[3]' => 'thu',
|
||||
'days[4]' => 'fr'
|
||||
];
|
||||
|
||||
public function testHostMacros()
|
||||
{
|
||||
$mock = \Mockery::mock(Host::class);
|
||||
$mock->name = 'test';
|
||||
$mock->address = '1.1.1.1';
|
||||
$mock->address6 = '::1';
|
||||
$mock->vars = self::VARS;
|
||||
|
||||
$mock->hostgroup = new Query();
|
||||
|
||||
$mock->vars = null;
|
||||
$mock->shouldReceive('mutateVarsProperty')->once()->andReturn([
|
||||
'os' => "Ubuntu",
|
||||
'days[0]' => 'mo',
|
||||
'days[1]' => 'tue',
|
||||
'days[2]' => 'wed',
|
||||
'days[3]' => 'thu',
|
||||
'days[4]' => 'fr'
|
||||
]);
|
||||
|
||||
$this->assertEquals($mock->name, $this->expandMacros('$host.name$', $mock));
|
||||
$this->assertEquals($mock->name, $this->expandMacros('$name$', $mock));
|
||||
$this->assertEquals($mock->address, $this->expandMacros('$host.address$', $mock));
|
||||
|
|
@ -64,33 +64,15 @@ class MacrosTest extends TestCase
|
|||
$mock = \Mockery::mock(Service::class);
|
||||
$mock->name = 'test-service';
|
||||
$mock->description = 'A test service';
|
||||
$mock->vars = self::VARS;
|
||||
|
||||
$mock->servicegroup = new Query();
|
||||
|
||||
$mock->vars = null;
|
||||
$mock->shouldReceive('mutateVarsProperty')->once()->andReturn([
|
||||
'os' => "Ubuntu",
|
||||
'days[0]' => 'mo',
|
||||
'days[1]' => 'tue',
|
||||
'days[2]' => 'wed',
|
||||
'days[3]' => 'thu',
|
||||
'days[4]' => 'fr'
|
||||
]);
|
||||
|
||||
$hostMock = \Mockery::mock(Host::class);
|
||||
$hostMock->name = 'test';
|
||||
$hostMock->address = '1.1.1.1';
|
||||
$hostMock->hostgroup = new ResultSet(new \ArrayIterator());
|
||||
|
||||
$hostMock->vars = null;
|
||||
$hostMock->shouldReceive('mutateVarsProperty')->once()->andReturn([
|
||||
'os' => "Ubuntu",
|
||||
'days[0]' => 'mo',
|
||||
'days[1]' => 'tue',
|
||||
'days[2]' => 'wed',
|
||||
'days[3]' => 'thu',
|
||||
'days[4]' => 'fr'
|
||||
]);
|
||||
$hostMock->vars = self::VARS;
|
||||
|
||||
$mock->host = $hostMock;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue