Skip to content

Commit

Permalink
Add test for, and improve Meta helper
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 17, 2022
1 parent 2c4f28a commit 15ccd27
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 20 deletions.
10 changes: 5 additions & 5 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@
| meta tag, use Meta::name() helper. To add an Open Graph property, use
| Meta::property() helper which also adds the `og:` prefix for you.
|
| Note that these global tags may be overridden by a page's meta tags.
| Please note that these tags might conflict with blog post tags.
|
*/

'meta' => [
Meta::name('author', 'Mr. Hyde'),
Meta::name('twitter:creator', '@hyde_php'),
Meta::name('description', 'My Hyde Blog'),
Meta::name('keywords', 'Static Sites, Blogs, Documentation'),
// Meta::name('author', 'Mr. Hyde'),
// Meta::name('twitter:creator', '@hyde_php'),
// Meta::name('description', 'My Hyde Blog'),
// Meta::name('keywords', 'Static Sites, Blogs, Documentation'),
Meta::name('generator', 'HydePHP '.Hyde\Framework\Hyde::version()),
Meta::property('site_name', $siteName),
],
Expand Down
44 changes: 29 additions & 15 deletions src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,52 @@

namespace Hyde\Framework;

/**
* Helpers to fluently declare HTML meta tags
* @see \Tests\Feature\MetadataHelperTest
*/
class Meta
{
public static function name(string $name, string $content, bool $ifConditionIsMet = true): ?string
public static function name(string $name, string $content, bool $ifConditionIsMet = true): string
{
if ($ifConditionIsMet) {
return '<meta name="'.e($name).'" content="'.e($content).'">';
}

return null;
return '<meta name="'.e($name).'" content="'.e($content).'">';
}

public static function property(string $property, string $content, bool $ifConditionIsMet = true): ?string
public static function property(string $property, string $content, bool $ifConditionIsMet = true): string
{
if ($ifConditionIsMet) {
$property = static::formatOpenGraphProperty($property);

return '<meta property="'.e($property).'" content="'.e($content).'">';
}
$property = static::formatOpenGraphProperty($property);

return null;
return '<meta property="'.e($property).'" content="'.e($content).'">';
}

public static function render(array $overridesGlobalMeta = []): string
{
return implode("\n",
array_unique(
static::filterUnique(
array_merge(
static::getGlobalMeta(),
$overridesGlobalMeta
)
)
);

}

protected static function filterUnique(array $meta): array
{
$array = [];
$existing = [];

foreach ($meta as $metaItem) {
$substring = substr($metaItem, 6, strpos($metaItem, ' content="') - 6);

if (!in_array($substring, $existing)) {
$array[] = $metaItem;
$existing[] = $substring;
}
}

return $array;
}

public static function getGlobalMeta(): array
Expand All @@ -43,6 +57,6 @@ public static function getGlobalMeta(): array

protected static function formatOpenGraphProperty(string $property): string
{
return str_starts_with('og:', $property) ? $property : 'og:'.$property;
return str_starts_with($property, 'og:') ? $property : 'og:'.$property;
}
}
142 changes: 142 additions & 0 deletions tests/Feature/MetadataHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Tests\Feature;

use Hyde\Framework\Meta;
use Tests\TestCase;

/**
* @covers \Hyde\Framework\Meta
*/
class MetadataHelperTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

config(['hyde.meta' => []]);
}

// Test name method returns a valid HTML meta string
public function test_name_method_returns_a_valid_html_meta_string()
{
$this->assertEquals(
'<meta name="foo" content="bar">',
Meta::name('foo', 'bar')
);
}

// Test property method returns a valid HTML meta string
public function test_property_method_returns_a_valid_html_meta_string()
{
$this->assertEquals(
'<meta property="og:foo" content="bar">',
Meta::property('foo', 'bar')
);
}

// Test property method accepts property with og prefix
public function test_property_method_accepts_property_with_og_prefix()
{
$this->assertEquals(
'<meta property="og:foo" content="bar">',
Meta::property('og:foo', 'bar')
);
}

// Test property method accepts property without og prefix
public function test_property_method_accepts_property_without_og_prefix()
{
$this->assertEquals(
'<meta property="og:foo" content="bar">',
Meta::property('foo', 'bar')
);
}

// Test render method implodes an array of meta tags into a formatted string
public function test_render_method_implodes_an_array_of_meta_tags_into_a_formatted_string()
{
$this->assertEquals(
'<meta name="foo" content="bar">'
."\n".'<meta property="og:foo" content="bar">',

Meta::render([
Meta::name('foo', 'bar'),
Meta::property('og:foo', 'bar')
])
);
}

// Test render method returns an empty string if no meta tags are supplied
public function test_render_method_returns_an_empty_string_if_no_meta_tags_are_supplied()
{
$this->assertEquals(
'',
Meta::render([])
);
}

// Test render method returns config defined tags if no meta tags are supplied
public function test_render_method_returns_config_defined_tags_if_no_meta_tags_are_supplied()
{
config(['hyde.meta' => [
Meta::name('foo', 'bar'),
Meta::property('og:foo', 'bar')
]]);

$this->assertEquals(
'<meta name="foo" content="bar">'
."\n".'<meta property="og:foo" content="bar">',

Meta::render([])
);
}

// Test render method merges config defined tags with supplied meta tags
public function test_render_method_merges_config_defined_tags_with_supplied_meta_tags()
{
config(['hyde.meta' => [
Meta::name('foo', 'bar'),
]]);

$this->assertEquals(
'<meta name="foo" content="bar">'
."\n".'<meta property="og:foo" content="bar">',

Meta::render([
Meta::property('foo', 'bar')
])
);
}

// Test render method returns unique meta tags
public function test_render_method_returns_unique_meta_tags()
{
config(['hyde.meta' => [
Meta::name('foo', 'bar'),
]]);

$this->assertEquals(
'<meta name="foo" content="bar">',
Meta::render([
Meta::name('foo', 'bar'),
])
);
}

// Test render method gives precedence to supplied meta tags
public function test_render_method_gives_precedence_to_supplied_meta_tags()
{
config(['hyde.meta' => [
Meta::name('foo', 'bar'),
]]);

$this->assertEquals(
'<meta name="foo" content="bar">',

Meta::render([
Meta::name('foo', 'baz'),
])
);
}
}

0 comments on commit 15ccd27

Please sign in to comment.