Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add publication fields class #681

Merged
merged 34 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ea76fe8
Create PublicationField.php
caendesilva Nov 21, 2022
9ab9899
Create PublicationFieldTest.php
caendesilva Nov 21, 2022
6b26cc6
Add schema keys as class properties
caendesilva Nov 21, 2022
e1ffebf
Make properties readonly as they can't be persisted directly
caendesilva Nov 21, 2022
dcb54dc
Add missing strict types declaration to test
caendesilva Nov 21, 2022
a5b18bd
Generate constructor
caendesilva Nov 21, 2022
8eb1709
Implement JsonSerializable, Arrayable
caendesilva Nov 21, 2022
2e404ff
Reorder properties to put type first
caendesilva Nov 21, 2022
a525a0d
Extract helper method
caendesilva Nov 21, 2022
d774ca6
Internally represent min/max values as integers
caendesilva Nov 21, 2022
bdda37b
Update expectation order
caendesilva Nov 21, 2022
41ec356
Refactor to force output be string via magic method
caendesilva Nov 21, 2022
872fb02
Revert overengineered code
caendesilva Nov 21, 2022
94b2b01
Apply fixes from StyleCI
StyleCIBot Nov 21, 2022
746c3ae
Fully use int type for what is actually integers
caendesilva Nov 21, 2022
fc06508
Allow range values to be null as they are not applicable to all types
caendesilva Nov 21, 2022
1a050d0
Ensure max value can never be less than the min value when both are set
caendesilva Nov 21, 2022
9405c80
Add validation method placeholder
caendesilva Nov 21, 2022
a19151d
Import class
caendesilva Nov 21, 2022
0060624
Add class constant for the accepted types
caendesilva Nov 21, 2022
1c01569
Throw exception for invalid type
caendesilva Nov 21, 2022
d7d9e3d
Apply fixes from StyleCI
StyleCIBot Nov 21, 2022
b568190
Promote class properties
caendesilva Nov 21, 2022
33da723
Update output hint to match code
caendesilva Nov 21, 2022
6954258
Flip '>' (does not change semantics)
caendesilva Nov 21, 2022
f1116b2
Add todo
caendesilva Nov 21, 2022
a5fcf05
Add TYPE_LABELS constant
caendesilva Nov 21, 2022
7931225
Revert "Add TYPE_LABELS constant"
caendesilva Nov 21, 2022
fcc3fb6
Force type to be lowercase internally
caendesilva Nov 21, 2022
cd2f90d
Apply fixes from StyleCI
StyleCIBot Nov 21, 2022
94c2865
Update test stub to use actual fields output
caendesilva Nov 21, 2022
e1f89df
Apply fixes from StyleCI
StyleCIBot Nov 21, 2022
b00a3e4
Add getFields method to get more dynamic data
caendesilva Nov 21, 2022
4296a74
Apply fixes from StyleCI
StyleCIBot Nov 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
]
}