Skip to content

Commit

Permalink
Add test and clean up docs for HasMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Apr 30, 2022
1 parent 2a7ddaa commit 976cb6c
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/Concerns/HasMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Metadata is used to create meta SEO tags.
*
* @see \Hyde\Framework\Models\Metadata
* @see \Tests\Feature\Concerns\HasMetadataTest
*/
trait HasMetadata
{
Expand All @@ -33,7 +34,7 @@ public function getMetadata(): array
return $this->metadata->metadata;
}

#[ArrayShape(['property' => "\content"])]
#[ArrayShape(['property' => "content"])]
public function getMetaProperties(): array
{
if (! isset($this->metadata)) {
Expand All @@ -43,23 +44,28 @@ public function getMetaProperties(): array
return $this->metadata->properties;
}

/**
* Generate metadata from the front matter that can be used in standard <meta> tags.
*/
protected function makeMetadata(): void
{
if (isset($this->matter['description'])) {
$this->metadata->add('description', $this->matter['description']);
}

// Add author if it exists
if (isset($this->matter['author'])) {
$this->metadata->add('author', $this->getAuthor($this->matter['author']));
}

// Add keywords if it exists
if (isset($this->matter['category'])) {
$this->metadata->add('keywords', $this->matter['category']);
}
}

/**
* Generate metadata from the front matter that can be used for og:type <meta> tags.
* Note that this currently assumes that the object using it is a Blog Post.
*/
protected function makeMetaProperties(): void
{
$this->metadata->addProperty('og:type', 'article');
Expand All @@ -68,12 +74,10 @@ protected function makeMetaProperties(): void
$this->metadata->addProperty('og:url', Hyde::uriPath('posts/'.$this->slug));
}

// Add title if it exists
if (isset($this->matter['title'])) {
$this->metadata->addProperty('og:title', $this->matter['title']);
}

// Add date if it exists
if (isset($this->matter['date'])) {
$date = date('c', strtotime($this->matter['date']));
$this->metadata->addProperty('og:article:published_time', $date);
Expand All @@ -93,6 +97,12 @@ protected function makeMetaProperties(): void
}
}

/**
* Parse the author string from the front matter with support for both flat and array notation.
*
* @param string|array $author
* @return string
*/
protected function getAuthor(string|array $author): string
{
if (is_string($author)) {
Expand Down
189 changes: 189 additions & 0 deletions tests/Feature/Concerns/HasMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace Tests\Feature\Concerns;

use Hyde\Framework\Concerns\HasMetadata;
use Hyde\Framework\Models\Metadata;
use Illuminate\Support\Facades\Config;
use Tests\TestCase;

/**
* @covers \Hyde\Framework\Concerns\HasMetadata
* @see \Hyde\Framework\Models\Metadata
*/
class HasMetadataTest extends TestCase
{
use HasMetadata;

public array $matter;
private string $slug;

protected function tearDown(): void
{
unset($this->metadata);
unset($this->matter);

parent::tearDown();
}

public function test_metadata_object_can_be_constructed()
{
$this->constructMetadata();
$this->assertInstanceOf(Metadata::class, $this->metadata);
}

public function test_get_metadata_returns_empty_array_when_uninitialized()
{
$this->matter = ['description' => 'foo'];
$this->assertEquals([], $this->getMetadata());
}

public function test_get_metadata_returns_valid_array_when_initialized()
{
$this->matter = [
'description' => 'foo',
'author' => 'bar',
'category' => 'cat',
];
$this->constructMetadata();
$this->assertEquals([
'description' => 'foo',
'author' => 'bar',
'keywords' => 'cat',
], $this->getMetadata());
}

public function test_get_meta_properties_returns_empty_array_when_uninitialized()
{
$this->assertEquals([], $this->getMetaProperties());
}

public function test_get_meta_properties_returns_base_array_when_initialized_with_empty_front_matter()
{
$this->constructMetadata();

$this->assertEquals(['og:type' => 'article'], $this->getMetaProperties());
}

// Note that this currently assumes that the object using it is a Blog Post.
public function test_get_meta_properties_contains_og_url_when_uri_path_set()
{
Config::set('hyde.site_url', 'https://example.com/foo');
$this->slug = 'bar';
$this->constructMetadata();

$this->assertEquals([
'og:type' => 'article',
'og:url' => 'https://example.com/foo/posts/bar'
], $this->getMetaProperties());
}

public function test_get_meta_properties_contains_og_title_when_title_set()
{
$this->matter = [
'title' => 'foo'
];
$this->constructMetadata();

$this->assertEquals([
'og:type' => 'article',
'og:title' => 'foo'
], $this->getMetaProperties());
}

public function test_get_meta_properties_contains_og_article_date_published_when_date_set()
{
$this->matter = [
'date' => '2022-01-01 12:00'
];
$this->constructMetadata();

$this->assertEquals([
'og:type' => 'article',
'og:article:published_time' => '2022-01-01T12:00:00+00:00'
], $this->getMetaProperties());
}

public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_string()
{
$this->matter = [
'image' => 'foo.jpg'
];
$this->constructMetadata();

$this->assertEquals([
'og:type' => 'article',
'og:image' => 'foo.jpg',
], $this->getMetaProperties());
}

public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_array_with_path()
{
$this->matter = [
'image' => [
'path' => 'foo.jpg'
]
];
$this->constructMetadata();

$this->assertEquals([
'og:type' => 'article',
'og:image' => 'foo.jpg',
], $this->getMetaProperties());
}

public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_array_with_uri()
{
$this->matter = [
'image' => [
'uri' => 'foo.jpg'
]
];
$this->constructMetadata();

$this->assertEquals([
'og:type' => 'article',
'og:image' => 'foo.jpg',
], $this->getMetaProperties());
}

public function test_get_author_returns_author_name_when_author_set_to_array_using_username()
{
$this->matter = [
'author' => [
'username' => 'foo',
]
];
$this->constructMetadata();

$this->assertEquals([
'author' => 'foo',
], $this->getMetadata());
}

public function test_get_author_returns_author_name_when_author_set_to_array_using_name()
{
$this->matter = [
'author' => [
'name' => 'foo',
]
];
$this->constructMetadata();

$this->assertEquals([
'author' => 'foo',
], $this->getMetadata());
}

public function test_get_author_returns_guest_when_author_set_to_array_without_name_or_username()
{
$this->matter = [
'author' => []
];
$this->constructMetadata();

$this->assertEquals([
'author' => 'Guest',
], $this->getMetadata());
}
}

0 comments on commit 976cb6c

Please sign in to comment.