Skip to content

Commit

Permalink
Merge pull request #681 from hydephp/add-publication-fields-class
Browse files Browse the repository at this point in the history
Add publication fields class
  • Loading branch information
caendesilva authored Nov 21, 2022
2 parents 4fe2bb8 + 4296a74 commit 2a02699
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ private function captureFieldsDefinitions(): Collection
$this->line(' 7 - Array');
$this->line(' 8 - Text');
$this->line(' 9 - Local Image');
$type = (int) PublicationHelper::askWithValidation($this, 'type', 'Field type (1-7)', ['required', 'integer', 'between:1,9'], 1);
$type = (int) PublicationHelper::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1);
do {
// TODO This should only be done for types that can have length restrictions right?
$field->min = PublicationHelper::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0);
$field->max = PublicationHelper::askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0);
$lengthsValid = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\Publications\Models;

use Hyde\Support\Concerns\JsonSerializesArrayable;
use Illuminate\Contracts\Support\Arrayable;
use InvalidArgumentException;
use JsonSerializable;
use function strtolower;

/**
* @see \Hyde\Framework\Testing\Feature\PublicationFieldTest
*/
class PublicationField implements JsonSerializable, Arrayable
{
use JsonSerializesArrayable;

public final const TYPES = ['string', 'boolean', 'integer', 'float', 'datetime', 'url', 'array', 'text', 'image'];

public readonly string $type;

public function __construct(string $type, public readonly string $name, public readonly ?int $min, public readonly ?int $max)
{
$this->type = strtolower($type);

if (! in_array(strtolower($type), self::TYPES)) {
throw new InvalidArgumentException(sprintf("The type '$type' is not a valid type. Valid types are: %s.", implode(', ', self::TYPES)));
}

if (($min !== null) && ($max !== null) && $max < $min) {
throw new InvalidArgumentException("The 'max' value cannot be less than the 'min' value.");
}
}

public function toArray(): array
{
return [
'type' => $this->type,
'name' => $this->name,
'min' => $this->min,
'max' => $this->max,
];
}

public function validateInputAgainstRules(string $input): bool
{
// TODO: Implement this method.

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Hyde\Support\Concerns\JsonSerializesArrayable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use function json_decode;
use JsonSerializable;
Expand Down Expand Up @@ -106,6 +107,14 @@ public function getDirectory(): string
return $this->directory;
}

/** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Publications\Models\PublicationField */
public function getFields(): Collection
{
return collect($this->fields)->map(function (array $data) {
return new PublicationField(...$data);
});
}

public function save(?string $path = null): void
{
$path ??= $this->getSchemaFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_command_creates_publication()
$this->artisan('make:publicationType')
->expectsQuestion('Publication type name', 'Test Publication')
->expectsQuestion('Field name', 'Title')
->expectsQuestion('Field type (1-7)', 1)
->expectsQuestion('Field type (1-9)', 1)
->expectsQuestion('Min value (for strings, this refers to string length)', 'default')
->expectsQuestion('Max value (for strings, this refers to string length)', 'default')
->expectsQuestion('Add another field (y/n)', 'n')
Expand Down
95 changes: 95 additions & 0 deletions packages/framework/tests/Feature/PublicationFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Features\Publications\Models\PublicationField;
use Hyde\Testing\TestCase;
use InvalidArgumentException;

/**
* @covers \Hyde\Framework\Features\Publications\Models\PublicationField
*/
class PublicationFieldTest extends TestCase
{
public function test_can_instantiate_class()
{
$field = $this->makeField();
$this->assertInstanceOf(PublicationField::class, $field);

$this->assertSame('string', $field->type);
$this->assertSame('test', $field->name);
$this->assertSame(1, $field->min);
$this->assertSame(10, $field->max);
}

public function test_can_get_field_as_array()
{
$this->assertSame([
'type' => 'string',
'name' => 'test',
'min' => 1,
'max' => 10,
], $this->makeField()->toArray());
}

public function test_can_encode_field_as_json()
{
$this->assertSame('{"type":"string","name":"test","min":1,"max":10}', json_encode($this->makeField()));
}

public function test_range_values_can_be_null()
{
$field = new PublicationField('string', 'test', null, null);
$this->assertNull($field->min);
$this->assertNull($field->max);
}

public function test_max_value_cannot_be_less_than_min_value()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("The 'max' value cannot be less than the 'min' value.");

new PublicationField('string', 'test', 10, 1);
}

public function test_types_constant()
{
$this->assertSame([
'string',
'boolean',
'integer',
'float',
'datetime',
'url',
'array',
'text',
'image',
], PublicationField::TYPES);
}

public function test_type_must_be_valid()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("The type 'invalid' is not a valid type. Valid types are: string, boolean, integer, float, datetime, url, array, text, image.");

new PublicationField('invalid', 'test', 1, 10);
}

public function test_type_input_is_case_insensitive()
{
$field = new PublicationField('STRING', 'test', 1, 10);
$this->assertSame('string', $field->type);
}

public function test_validate_input_against_rules()
{
$this->markTestIncomplete('TODO: Implement this method.');
}

protected function makeField(): PublicationField
{
return new PublicationField('string', 'test', 1, 10);
}
}
19 changes: 18 additions & 1 deletion packages/framework/tests/Feature/PublicationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Hyde\Framework\Testing\Feature;

use function array_merge;
use Hyde\Framework\Features\Publications\Models\PublicationField;
use Hyde\Framework\Features\Publications\Models\PublicationType;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;

/**
Expand Down Expand Up @@ -91,6 +93,16 @@ public function test_can_load_from_json_file()
$this->assertEquals($publicationType, PublicationType::fromFile(Hyde::path('tests/fixtures/test-publication-schema.json')));
}

public function test_get_fields_method_returns_collection_of_field_objects()
{
$publicationType = new PublicationType(...$this->getTestDataWithPathInformation());
$collection = $publicationType->getFields();
$this->assertCount(1, $collection);
$this->assertInstanceOf(Collection::class, $collection);
$this->assertInstanceOf(PublicationField::class, $collection->first());
$this->assertEquals(new Collection([new PublicationField('string', 'test', 0, 128)]), $collection);
}

protected function getTestData(): array
{
return [
Expand All @@ -103,7 +115,12 @@ protected function getTestData(): array
'detailTemplate' => 'detail',
'listTemplate' => 'list',
'fields' => [
'foo' => 'bar',
[
'type' => 'string',
'name' => 'test',
'min' => 0,
'max' => 128,
],
],
];
}
Expand Down
11 changes: 8 additions & 3 deletions tests/fixtures/test-publication-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
"prevNextLinks": true,
"detailTemplate": "detail",
"listTemplate": "list",
"fields": {
"foo": "bar"
}
"fields": [
{
"type": "string",
"name": "test",
"min": "0",
"max": "128"
}
]
}

0 comments on commit 2a02699

Please sign in to comment.