diff --git a/packages/publications/src/Commands/MakePublicationCommand.php b/packages/publications/src/Commands/MakePublicationCommand.php index 71b56bbdd66..dff37aa5bf5 100644 --- a/packages/publications/src/Commands/MakePublicationCommand.php +++ b/packages/publications/src/Commands/MakePublicationCommand.php @@ -159,14 +159,14 @@ protected function captureTagFieldInput(PublicationFieldDefinition $field): ?Pub $options = PublicationService::getValuesForTagName($tagGroup); if ($options->isEmpty()) { - return $this->handleEmptyOptionsCollection($field, 'tag', 'No tags for this publication type found in tags.json'); + return $this->handleEmptyOptionsCollection($field, 'tag', 'No tags for this publication type found in tags.yml'); } $this->tip('You can enter multiple tags separated by commas'); $choice = $this->reloadableChoice($this->getReloadableTagValuesArrayClosure($tagGroup), 'Which tag would you like to use?', - 'Reload tags.json', + 'Reload tags.yml', true ); diff --git a/packages/publications/src/Commands/MakePublicationTagCommand.php b/packages/publications/src/Commands/MakePublicationTagCommand.php index f90c88eb847..8dd10e02fd3 100644 --- a/packages/publications/src/Commands/MakePublicationTagCommand.php +++ b/packages/publications/src/Commands/MakePublicationTagCommand.php @@ -90,7 +90,7 @@ protected function printSelectionInformation(): void protected function saveTagsToDisk(): void { $this->infoComment('Saving tag data to', - DiscoveryService::createClickableFilepath(Hyde::path('tags.json')) + DiscoveryService::createClickableFilepath(Hyde::path('tags.yml')) ); app(PublicationTags::class)->addTagGroups($this->tags)->save(); diff --git a/packages/publications/src/Commands/MakePublicationTypeCommand.php b/packages/publications/src/Commands/MakePublicationTypeCommand.php index 63fd82ad496..744b77c8677 100644 --- a/packages/publications/src/Commands/MakePublicationTypeCommand.php +++ b/packages/publications/src/Commands/MakePublicationTypeCommand.php @@ -139,14 +139,14 @@ protected function getFieldType(): PublicationFieldTypes protected function getTagGroup(): string { if (empty(PublicationTags::getTagGroups())) { - $this->error('No tag groups have been added to tags.json'); + $this->error('No tag groups have been added to tags.yml'); if ($this->confirm('Would you like to add some tags now?')) { $this->call('make:publicationTag'); $this->newLine(); $this->comment("Okay, we're back on track!"); } else { - throw new InvalidArgumentException('Can not create a tag field without any tag groups defined in tags.json'); + throw new InvalidArgumentException('Can not create a tag field without any tag groups defined in tags.yml'); } } diff --git a/packages/publications/src/Models/PublicationTags.php b/packages/publications/src/Models/PublicationTags.php index 4396100a223..a9d1da62307 100644 --- a/packages/publications/src/Models/PublicationTags.php +++ b/packages/publications/src/Models/PublicationTags.php @@ -5,17 +5,15 @@ namespace Hyde\Publications\Models; use function file_exists; -use function file_get_contents; use Hyde\Facades\Filesystem; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Hyde; use Illuminate\Support\Collection; -use function json_decode; -use function json_encode; -use JsonException; +use Symfony\Component\Yaml\Exception\ParseException; +use Symfony\Component\Yaml\Yaml; /** - * Object representation for the tags.json file. + * Object representation for the tags.yml file. * * @see \Hyde\Publications\Testing\Feature\PublicationTagsTest */ @@ -83,7 +81,7 @@ public function addTagsToGroup(string $name, array|string $values): self */ public function save(): self { - Filesystem::putContents('tags.json', json_encode($this->tags, JSON_PRETTY_PRINT)); + Filesystem::putContents('tags.yml', Yaml::dump($this->tags->toArray())); return $this; } @@ -119,20 +117,22 @@ public static function getTagGroups(): array } /** - * Validate the tags.json file is valid. + * Validate the tags.yml file is valid. * * @internal This method is experimental and may be removed without notice + * + * @todo Support Yaml */ public static function validateTagsFile(): void { - if (! file_exists(Hyde::path('tags.json'))) { - throw new FileNotFoundException('tags.json'); + if (! file_exists(Hyde::path('tags.yml'))) { + throw new FileNotFoundException('tags.yml'); } - $tags = json_decode(file_get_contents(Hyde::path('tags.json')), true); + $tags = Yaml::parseFile(Hyde::path('tags.yml')); if (! is_array($tags) || empty($tags)) { - throw new JsonException('Could not decode tags.json'); + throw new ParseException('Could not decode tags.yml'); } foreach ($tags as $name => $values) { @@ -148,8 +148,8 @@ public static function validateTagsFile(): void /** @return array> */ protected function parseTagsFile(): array { - if (file_exists(Hyde::path('tags.json'))) { - return json_decode(file_get_contents(Hyde::path('tags.json')), true); + if (file_exists(Hyde::path('tags.yml'))) { + return Yaml::parseFile(Hyde::path('tags.yml')); } return []; diff --git a/packages/publications/tests/Feature/MakePublicationCommandTest.php b/packages/publications/tests/Feature/MakePublicationCommandTest.php index f33356d9d93..2b21d230964 100644 --- a/packages/publications/tests/Feature/MakePublicationCommandTest.php +++ b/packages/publications/tests/Feature/MakePublicationCommandTest.php @@ -326,7 +326,7 @@ public function test_media_input_selects_the_right_file() public function test_command_with_single_tag_input() { - $this->file('tags.json', json_encode([ + $this->file('tags.yml', json_encode([ 'test-publication' => ['foo', 'bar', 'baz'], ])); $this->makeSchemaFile([ @@ -350,7 +350,7 @@ public function test_command_with_single_tag_input() public function test_command_with_multiple_tag_inputs() { - $this->file('tags.json', json_encode([ + $this->file('tags.yml', json_encode([ 'test-publication' => ['foo', 'bar', 'baz'], ])); $this->makeSchemaFile([ @@ -439,7 +439,7 @@ public function test_tag_input_with_no_tags() ]); $this->artisan('make:publication test-publication') - ->expectsOutput('Warning: No tags for this publication type found in tags.json') + ->expectsOutput('Warning: No tags for this publication type found in tags.yml') ->expectsConfirmation('Would you like to skip this field?') ->expectsOutput('Error: Unable to locate any tags for this publication type') ->assertExitCode(1); @@ -460,7 +460,7 @@ public function test_tag_input_with_no_tags_but_skips() ]); $this->artisan('make:publication test-publication') - ->expectsOutput('Warning: No tags for this publication type found in tags.json') + ->expectsOutput('Warning: No tags for this publication type found in tags.yml') ->expectsConfirmation('Would you like to skip this field?', 'yes') ->doesntExpectOutput('Error: Unable to locate any tags for this publication type') ->assertExitCode(0); @@ -512,8 +512,8 @@ public function test_handleEmptyOptionsCollection_for_required_field() ]); $this->artisan('make:publication test-publication') - ->doesntExpectOutput('Warning: No tags for this publication type found in tags.json') - ->expectsOutput('Error: Unable to create publication: No tags for this publication type found in tags.json') + ->doesntExpectOutput('Warning: No tags for this publication type found in tags.yml') + ->expectsOutput('Error: Unable to create publication: No tags for this publication type found in tags.yml') ->assertExitCode(1); } diff --git a/packages/publications/tests/Feature/MakePublicationTagCommandTest.php b/packages/publications/tests/Feature/MakePublicationTagCommandTest.php index 57f3cbe545a..215b28255c4 100644 --- a/packages/publications/tests/Feature/MakePublicationTagCommandTest.php +++ b/packages/publications/tests/Feature/MakePublicationTagCommandTest.php @@ -17,7 +17,7 @@ class MakePublicationTagCommandTest extends TestCase { protected function tearDown(): void { - unlink(Hyde::path('tags.json')); + unlink(Hyde::path('tags.yml')); parent::tearDown(); } @@ -31,13 +31,19 @@ public function testCanCreateNewPublicationTag() ->expectsOutput('Enter the tag values: (end with an empty line)') ->expectsOutput('Adding the following tags:') ->expectsOutput(' foo: foo, bar, baz') - ->expectsOutput('Saving tag data to ['.Hyde::path('tags.json').']') + ->expectsOutput('Saving tag data to ['.Hyde::path('tags.yml').']') ->assertExitCode(0); - $this->assertFileExists(Hyde::path('tags.json')); + $this->assertFileExists(Hyde::path('tags.yml')); $this->assertSame( - json_encode(['foo' => ['foo', 'bar', 'baz']], 128), - file_get_contents(Hyde::path('tags.json')) + <<<'YAML' + foo: + - foo + - bar + - baz + + YAML, + file_get_contents(Hyde::path('tags.yml')) ); } @@ -50,13 +56,19 @@ public function testCanCreateNewPublicationTagWithTagNameArgument() ->expectsOutput('Enter the tag values: (end with an empty line)') ->expectsOutput('Adding the following tags:') ->expectsOutput(' foo: foo, bar, baz') - ->expectsOutput('Saving tag data to ['.Hyde::path('tags.json').']') + ->expectsOutput('Saving tag data to ['.Hyde::path('tags.yml').']') ->assertExitCode(0); - $this->assertFileExists(Hyde::path('tags.json')); + $this->assertFileExists(Hyde::path('tags.yml')); $this->assertSame( - json_encode(['foo' => ['foo', 'bar', 'baz']], 128), - file_get_contents(Hyde::path('tags.json')) + <<<'YAML' + foo: + - foo + - bar + - baz + + YAML, + file_get_contents(Hyde::path('tags.yml')) ); } diff --git a/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php b/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php index 3c9aa03d17c..2e6cfa7821d 100644 --- a/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php +++ b/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php @@ -237,7 +237,7 @@ public function testWithTagFieldInput() JSON, 'test-publication/schema.json'); - unlink(Hyde::path('tags.json')); + unlink(Hyde::path('tags.yml')); } public function testWithTagFieldInputButNoTags() @@ -249,9 +249,9 @@ public function testWithTagFieldInputButNoTags() ->expectsQuestion('Enter name for field #1', 'MyTag') ->expectsChoice('Enter type for field #1', 'Tag', self::expectedEnumCases, true) - ->expectsOutput('No tag groups have been added to tags.json') + ->expectsOutput('No tag groups have been added to tags.yml') ->expectsConfirmation('Would you like to add some tags now?') - ->expectsOutput('Error: Can not create a tag field without any tag groups defined in tags.json') + ->expectsOutput('Error: Can not create a tag field without any tag groups defined in tags.yml') ->assertExitCode(1); $this->assertFileDoesNotExist(Hyde::path('test-publication/schema.json')); @@ -260,14 +260,14 @@ public function testWithTagFieldInputButNoTags() public function testWithTagFieldInputButNoTagsCanPromptToCreateTags() { $this->directory('test-publication'); - $this->cleanUpWhenDone('tags.json'); + $this->cleanUpWhenDone('tags.yml'); InputStreamHandler::mockInput("foo\nbar\nbaz\n"); $this->artisan('make:publicationType "Test Publication"') ->expectsQuestion('Enter name for field #1', 'MyTag') ->expectsChoice('Enter type for field #1', 'Tag', self::expectedEnumCases) - ->expectsOutput('No tag groups have been added to tags.json') + ->expectsOutput('No tag groups have been added to tags.yml') ->expectsConfirmation('Would you like to add some tags now?', 'yes') ->expectsQuestion('Tag name', 'foo') ->expectsOutput("Okay, we're back on track!") @@ -280,14 +280,14 @@ public function testWithTagFieldInputButNoTagsCanPromptToCreateTags() ->expectsChoice('Choose the sort direction', 'Ascending', ['Ascending', 'Descending']) ->expectsQuestion(self::selectPageSizeQuestion, 0) - ->doesntExpectOutput('Error: Can not create a tag field without any tag groups defined in tags.json') + ->doesntExpectOutput('Error: Can not create a tag field without any tag groups defined in tags.yml') ->assertSuccessful(); $this->assertCommandCalled('make:publicationTag'); - $this->assertFileExists(Hyde::path('tags.json')); + $this->assertFileExists(Hyde::path('tags.yml')); $this->assertSame( - json_encode(['foo' => ['foo', 'bar', 'baz']], 128), - file_get_contents(Hyde::path('tags.json')) + "foo:\n - foo\n - bar\n - baz\n", + file_get_contents(Hyde::path('tags.yml')) ); } } diff --git a/packages/publications/tests/Feature/PublicationServiceTest.php b/packages/publications/tests/Feature/PublicationServiceTest.php index 09f4d7deede..5eec530f549 100644 --- a/packages/publications/tests/Feature/PublicationServiceTest.php +++ b/packages/publications/tests/Feature/PublicationServiceTest.php @@ -192,7 +192,7 @@ public function testGetAllTags() 'baz', ], ]; - $this->file('tags.json', json_encode($tags)); + $this->file('tags.yml', json_encode($tags)); $this->assertSame($tags, PublicationService::getAllTags()->toArray()); } @@ -209,7 +209,7 @@ public function testGetValuesForTagName() ], ]; - $this->file('tags.json', json_encode($tags)); + $this->file('tags.yml', json_encode($tags)); $this->assertSame(['bar', 'baz'], PublicationService::getValuesForTagName('foo')->toArray()); } diff --git a/packages/publications/tests/Feature/PublicationTagsTest.php b/packages/publications/tests/Feature/PublicationTagsTest.php index 2876d1cf3a3..637e919b07e 100644 --- a/packages/publications/tests/Feature/PublicationTagsTest.php +++ b/packages/publications/tests/Feature/PublicationTagsTest.php @@ -10,7 +10,8 @@ use Hyde\Testing\TestCase; use Illuminate\Support\Collection; use function json_encode; -use JsonException; +use Symfony\Component\Yaml\Exception\ParseException; +use Symfony\Component\Yaml\Yaml; /** * @covers \Hyde\Publications\Models\PublicationTags @@ -24,7 +25,7 @@ public function canConstructNewTagsInstance() public function testConstructorAutomaticallyLoadsTagsFile() { - $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + $this->file('tags.yml', json_encode(['foo' => ['bar', 'baz']])); $this->assertSame(['foo' => ['bar', 'baz']], (new PublicationTags())->getTags()->toArray()); } @@ -36,21 +37,21 @@ public function testConstructorAddsEmptyArrayWhenThereIsNoTagsFile() public function testGetTags() { - $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + $this->file('tags.yml', json_encode(['foo' => ['bar', 'baz']])); $this->assertEquals(new Collection(['foo' => ['bar', 'baz']]), (new PublicationTags())->getTags()); } public function testGetTagsInGroup() { - $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + $this->file('tags.yml', json_encode(['foo' => ['bar', 'baz']])); $this->assertEquals(['bar', 'baz'], (new PublicationTags())->getTagsInGroup('foo')); } public function testGetTagsInGroupOnlyReturnTagsForTheSpecifiedGroup() { - $this->file('tags.json', json_encode([ + $this->file('tags.yml', json_encode([ 'foo' => ['bar', 'baz'], 'bar' => ['foo', 'baz'], ])); @@ -60,7 +61,7 @@ public function testGetTagsInGroupOnlyReturnTagsForTheSpecifiedGroup() public function testGetTagsInGroupReturnsEmptyArrayWhenGroupDoesNotExist() { - $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + $this->file('tags.yml', json_encode(['foo' => ['bar', 'baz']])); $this->assertEquals([], (new PublicationTags())->getTagsInGroup('bar')); } @@ -134,7 +135,7 @@ public function testGetTagGroups() $tags->save(); $this->assertSame(['test', 'test2'], PublicationTags::getTagGroups()); - unlink(Hyde::path('tags.json')); + unlink(Hyde::path('tags.yml')); } public function testGetTagGroupsWithNoTags() @@ -149,17 +150,59 @@ public function testCanSaveTagsToDisk() $tags->save(); $this->assertSame( - <<<'JSON' + <<<'YAML' + test: + - test1 + - test2 + + YAML, file_get_contents(Hyde::path('tags.yml')) + ); + + unlink(Hyde::path('tags.yml')); + } + + public function testCanLoadTagsFromJsonFile() + { + $this->file('tags.yml', <<<'JSON' { - "test": [ - "test1", - "test2" + "Foo": [ + "one", + "two", + "three" + ], + "Second": [ + "foo", + "bar", + "baz" ] } - JSON, file_get_contents(Hyde::path('tags.json')) + JSON ); - unlink(Hyde::path('tags.json')); + $this->assertSame([ + 'Foo' => ['one', 'two', 'three'], + 'Second' => ['foo', 'bar', 'baz'], + ], PublicationTags::getAllTags()->toArray()); + } + + public function testCanLoadTagsFromYamlFile() + { + $this->file('tags.yml', <<<'YAML' + Foo: + - one + - two + - three + Second: + - foo + - bar + - baz + YAML + ); + + $this->assertSame([ + 'Foo' => ['one', 'two', 'three'], + 'Second' => ['foo', 'bar', 'baz'], + ], PublicationTags::getAllTags()->toArray()); } public function testGetAllTags() @@ -170,7 +213,7 @@ public function testGetAllTags() 'baz', ], ]; - $this->file('tags.json', json_encode($tags)); + $this->file('tags.yml', json_encode($tags)); $this->assertSame($tags, PublicationTags::getAllTags()->toArray()); } @@ -192,7 +235,7 @@ public function testGetValuesForTagName() ], ]; - $this->file('tags.json', json_encode($tags)); + $this->file('tags.yml', json_encode($tags)); $this->assertSame(['bar', 'baz'], PublicationTags::getValuesForTagName('foo')); } @@ -206,7 +249,7 @@ public function testGetValuesForTagNameWithMissingTagName() ], ]; - $this->file('tags.json', json_encode($tags)); + $this->file('tags.yml', json_encode($tags)); $this->assertSame([], PublicationTags::getValuesForTagName('bar')); } @@ -218,7 +261,7 @@ public function testGetValuesForTagNameWithNoTags() public function testValidateTagsFileWithValidFile() { - $this->file('tags.json', json_encode(['foo' => ['bar', 'baz']])); + $this->file('tags.yml', json_encode(['foo' => ['bar', 'baz']])); PublicationTags::validateTagsFile(); $this->assertTrue(true); @@ -226,9 +269,9 @@ public function testValidateTagsFileWithValidFile() public function testValidateTagsFileWithInvalidFile() { - $this->file('tags.json', 'invalid json'); + $this->file('tags.yml', 'invalid yaml'); - $this->expectException(JsonException::class); + $this->expectException(ParseException::class); PublicationTags::validateTagsFile(); } @@ -238,11 +281,11 @@ public function testValidateTagsFileWithNoFile() PublicationTags::validateTagsFile(); } - public function testValidateTagsFileWithEmptyJsonFile() + public function testValidateTagsFileWithEmptyYamlFile() { - $this->file('tags.json', json_encode([])); + $this->file('tags.yml', Yaml::dump([])); - $this->expectException(JsonException::class); + $this->expectException(ParseException::class); PublicationTags::validateTagsFile(); } } diff --git a/packages/publications/tests/Feature/SeedsPublicationFilesTest.php b/packages/publications/tests/Feature/SeedsPublicationFilesTest.php index f3fcc02c793..dbacf1f5c42 100644 --- a/packages/publications/tests/Feature/SeedsPublicationFilesTest.php +++ b/packages/publications/tests/Feature/SeedsPublicationFilesTest.php @@ -131,7 +131,7 @@ public function testWithStringType() public function testWithTagType() { $tags = ['test-publication' => ['foo', 'bar', 'baz']]; - $this->file('tags.json', json_encode($tags)); + $this->file('tags.yml', json_encode($tags)); $this->pubType->fields = collect([ (new PublicationFieldDefinition('tag', 'tag', tagGroup: 'test-publication')), ]);