new functions for container and new assert test

Former-commit-id: db29ea0fcecd5bb6c36ad1a21b15a35c56c5be24
This commit is contained in:
Daniel Maya 2019-03-19 12:03:02 +01:00
parent 5b58287f19
commit 1f0bb9cf21
3 changed files with 191 additions and 48 deletions

View File

@ -16,19 +16,13 @@ abstract class Model
protected abstract function decode(array $data): array; protected abstract function decode(array $data): array;
private function __construct(array $unknownData) protected function __construct(array $unknownData)
{ {
$this->validateData($unknownData); $this->validateData($unknownData);
$this->data = $this->decode($unknownData); $this->data = $this->decode($unknownData);
} }
public static function fromArray(array $data): self
{
return new self($data);
}
public function toJson(): string public function toJson(): string
{ {
return \json_encode($this->data); return \json_encode($this->data);
@ -41,7 +35,7 @@ abstract class Model
} }
protected static function parseBool(mixed $value): boolean protected static function parseBool($value): bool
{ {
if (\is_bool($value) === true) { if (\is_bool($value) === true) {
return $value; return $value;
@ -55,9 +49,21 @@ abstract class Model
} }
protected static function notEmptyStringOr(mixed $val, string $def): mixed protected static function notEmptyStringOr($val, $def)
{ {
return (\is_string($val) === true && count($val) > 0) ? $val : $def; return (\is_string($val) === true && strlen($val) > 0) ? $val : $def;
}
protected static function issetInArray(array $val, array $keys)
{
foreach ($keys as $key => $value) {
if (isset($val[$value])) {
return $val[$value];
}
}
return null;
} }

View File

@ -9,6 +9,12 @@ final class Container extends Model
{ {
public static function fromArray(array $data): self
{
return new self($data);
}
protected function validateData(array $data): void protected function validateData(array $data): void
{ {
if (isset($data['id']) === false if (isset($data['id']) === false
@ -58,22 +64,23 @@ final class Container extends Model
'groupId' => $this->extractGroupId($data), 'groupId' => $this->extractGroupId($data),
'backgroundURL' => $this->extractBackgroundUrl($data), 'backgroundURL' => $this->extractBackgroundUrl($data),
'backgroundColor' => $this->extractBackgroundColor($data), 'backgroundColor' => $this->extractBackgroundColor($data),
'isFavorite' => Model::parseBool($data['is_favourite']) 'isFavorite' => $this->extractFavorite($data),
|| Model::parseBool($data['isFavorite']),
'width' => (int) $data['width'], 'width' => (int) $data['width'],
'height' => (int) $data['height'], 'height' => (int) $data['height'],
]; ];
} }
private function extractGroupId(array $data): number private function extractGroupId(array $data): int
{ {
if (isset($data['id_group']) === true if (isset($data['id_group']) === true
&& \is_numeric($data['id_group']) === true && \is_numeric($data['id_group']) === true
&& $data['id_group'] >= 0
) { ) {
return $data['id_group']; return $data['id_group'];
} else if (isset($data['groupId']) === true } else if (isset($data['groupId']) === true
&& \is_numeric($data['groupId']) === true && \is_numeric($data['groupId']) === true
&& $data['groupId'] >= 0
) { ) {
return $data['groupId']; return $data['groupId'];
} }
@ -84,35 +91,33 @@ final class Container extends Model
} }
private function extractBackgroundUrl(array $data): mixed private function extractBackgroundUrl(array $data)
{ {
$backgroundUrl = Model::notEmptyStringOr($data['background'], null); $background = Model::notEmptyStringOr(
if ($backgroundUrl !== null) { Model::issetInArray($data, ['background', 'backgroundURL']),
return $backgroundUrl; null
} );
return $background;
$backgroundUrl = Model::notEmptyStringOr($data['backgroundURL'], null);
if ($backgroundUrl !== null) {
return $backgroundUrl;
}
return null;
} }
private function extractBackgroundColor(array $data): mixed private function extractBackgroundColor(array $data)
{ {
$backgroundColor = Model::notEmptyStringOr($data['background_color'], null); $backgroundColor = Model::notEmptyStringOr(
if ($backgroundColor !== null) { Model::issetInArray($data, ['backgroundColor', 'background_color']),
return $backgroundColor; null
} );
return $backgroundColor;
}
$backgroundColor = Model::notEmptyStringOr($data['backgroundColor'], null);
if ($backgroundColor !== null) {
return $backgroundColor;
}
return null; private function extractFavorite(array $data): bool
{
$favorite = Model::parseBool(
Model::issetInArray($data, ['is_favourite', 'isFavorite']),
null
);
return $favorite;
} }

View File

@ -18,8 +18,42 @@ class ContainerTest extends TestCase
VisualConsole::class, VisualConsole::class,
VisualConsole::fromArray( VisualConsole::fromArray(
[ [
'id' => 1, 'id' => 1,
'name' => 'foo', 'name' => 'foo',
'groupId' => 1,
'backgroundURL' => 'aaa',
'backgroundColor' => 'bbb',
'width' => 800,
'height' => 800,
]
)
);
$this->assertInstanceOf(
VisualConsole::class,
VisualConsole::fromArray(
[
'id' => 69,
'name' => 'New visual console',
'groupId' => 0,
'background' => 'globalmap.jpg',
'background_color' => 'white',
'is_favourite' => 1,
'width' => 100,
'height' => 200,
]
)
);
$this->assertInstanceOf(
VisualConsole::class,
VisualConsole::fromArray(
[
'id' => 1030,
'name' => 'console2',
'groupId' => 12,
'width' => 1024,
'height' => 768,
] ]
) )
); );
@ -32,13 +66,22 @@ class ContainerTest extends TestCase
// Invalid id. // Invalid id.
VisualConsole::fromArray( VisualConsole::fromArray(
[ [
'id' => 'bar', 'id' => 'bar',
'name' => 'foo', 'name' => 'foo',
'groupId' => 0,
'is_favourite' => 1,
'width' => 1024,
'height' => 768,
] ]
); );
// Missing id. // Missing id.
VisualConsole::fromArray( VisualConsole::fromArray(
['name' => 'foo'] [
'name' => 'foo',
'groupId' => 0,
'width' => 1024,
'height' => 768,
]
); );
} }
@ -46,28 +89,117 @@ class ContainerTest extends TestCase
public function testCannotBeCreatedWithInvalidName(): void public function testCannotBeCreatedWithInvalidName(): void
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
// Invalid name. // Empty name.
VisualConsole::fromArray( VisualConsole::fromArray(
[ [
'id' => 1, 'id' => 1,
'name' => '', 'name' => '',
'groupId' => 0,
'width' => 1024,
'height' => 768,
] ]
); );
// Missing name. // Missing name.
VisualConsole::fromArray( VisualConsole::fromArray(
['id' => 1] [
'id' => 1,
'groupId' => 8,
'width' => 1024,
'height' => 768,
]
); );
} }
public function testUserIsRepresentedAsJson(): void public function testCannotBeCreatedWithInvalidGroupId(): void
{
$this->expectException(InvalidArgumentException::class);
// invalid group id.
VisualConsole::fromArray(
[
'id' => 1,
'name' => 'test',
'groupId' => 'Hi',
'width' => 1024,
'height' => 768,
]
);
// Missing group id.
VisualConsole::fromArray(
[
'id' => 1,
'name' => 'test',
'width' => 1024,
'height' => 768,
]
);
}
public function testCannotBeCreatedWithInvalidWidth(): void
{
$this->expectException(InvalidArgumentException::class);
// invalid width.
VisualConsole::fromArray(
[
'id' => 1,
'name' => 'test',
'groupId' => 10,
'width' => 0,
'height' => 768,
]
);
// Missing width.
VisualConsole::fromArray(
[
'id' => 1,
'name' => 'test',
'groupId' => 10,
'height' => 768,
]
);
}
public function testCannotBeCreatedWithInvalidHeigth(): void
{
$this->expectException(InvalidArgumentException::class);
// invalid height.
VisualConsole::fromArray(
[
'id' => 1,
'name' => 'test',
'groupId' => 10,
'width' => 1024,
'height' => -1,
]
);
// Missing height.
VisualConsole::fromArray(
[
'id' => 1,
'name' => 'test',
'groupId' => 10,
'width' => 1024,
]
);
}
public function testContainerIsRepresentedAsJson(): void
{ {
$this->assertEquals( $this->assertEquals(
'{"id":1,"name":"foo"}', '{"id":1,"name":"foo","groupId":0,"backgroundURL":null,"backgroundColor":null,"isFavorite":false,"width":1024,"height":768}',
VisualConsole::fromArray( VisualConsole::fromArray(
[ [
'id' => 1, 'id' => 1,
'name' => 'foo', 'name' => 'foo',
'groupId' => 0,
'width' => 1024,
'height' => 768,
] ]
) )
); );