Skip to content

Commit

Permalink
Merge pull request #46 from ARCANEDEV/develop
Browse files Browse the repository at this point in the history
Using the php-html package
  • Loading branch information
arcanedev-maroc authored Feb 27, 2019
2 parents ddf8d3f + 1cf7b30 commit 5340d09
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 89 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1.3",
"arcanedev/support": "~4.4.0"
"php": ">=7.1.3",
"arcanedev/php-html": "~1.0",
"arcanedev/support": "~4.4.0"
},
"require-dev": {
"ext-dom": "*",
"orchestra/testbench": "~3.7.0",
"phpunit/phpunit": "~7.0",
"phpunit/phpcov": "~5.0"
Expand All @@ -42,5 +44,7 @@
"Arcanedev\\SeoHelper\\SeoHelperServiceProvider"
]
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
16 changes: 8 additions & 8 deletions src/Bases/MetaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ protected function addMeta($name, $content)
*/
public function remove($names)
{
$names = $this->prepareName($names);

return $this->forget($names);
return $this->forget(
$this->prepareName($names)
);
}

/**
Expand All @@ -140,11 +140,11 @@ public function remove($names)
*/
public function render()
{
$output = $this->map(function (Renderable $meta) {
return $meta->render();
})->toArray();

return implode(PHP_EOL, array_filter($output));
return $this->map(function (Renderable $meta) {
return $meta->render();
})
->filter()
->implode(PHP_EOL);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Entities/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public function __toString()
*/
protected function renderGoogleScript()
{
if (empty($this->google)) return '';
if (empty($this->google))
return '';

return <<<EOT
<script>
Expand Down
11 changes: 7 additions & 4 deletions src/Entities/Description.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace Arcanedev\SeoHelper\Entities;

use Arcanedev\Html\Elements\Meta;
use Arcanedev\SeoHelper\Contracts\Entities\Description as DescriptionContract;
use Arcanedev\SeoHelper\Exceptions\InvalidArgumentException;
use Arcanedev\SeoHelper\Helpers\Meta;
use Arcanedev\Support\Traits\Configurable;

/**
Expand Down Expand Up @@ -153,9 +153,12 @@ public static function make($content, $max = 155)
*/
public function render()
{
return $this->hasContent()
? Meta::make($this->name, $this->get())->render()
: '';
if ( ! $this->hasContent())
return '';

return Meta::make()
->attributes(['name' => $this->name, 'content' => $this->get()])
->toHtml();
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Entities/Keywords.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Arcanedev\SeoHelper\Entities;

use Arcanedev\Html\Elements\Meta;
use Arcanedev\SeoHelper\Contracts\Entities\Keywords as KeywordsContract;
use Arcanedev\SeoHelper\Helpers\Meta;
use Arcanedev\Support\Traits\Configurable;

/**
Expand Down Expand Up @@ -155,9 +155,12 @@ public function addMany(array $keywords)
*/
public function render()
{
return $this->hasContent()
? Meta::make($this->name, $this->get())->render()
: '';
if ( ! $this->hasContent())
return '';

return Meta::make()
->attributes(['name' => $this->name, 'content' => $this->get()])
->toHtml();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Entities/MetaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MetaCollection extends BaseMetaCollection
* @var array
*/
protected $ignored = [
'description', 'keywords'
'description',
'keywords'
];

/* -----------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion src/Entities/Title.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Arcanedev\SeoHelper\Entities;

use Arcanedev\Html\Elements\Element;
use Arcanedev\SeoHelper\Contracts\Entities\Title as TitleContract;
use Arcanedev\SeoHelper\Exceptions\InvalidArgumentException;
use Arcanedev\Support\Traits\Configurable;
Expand Down Expand Up @@ -317,7 +318,9 @@ public function render()
? $this->renderTitleFirst($separator)
: $this->renderTitleLast($separator);

return '<title>' . $this->prepareTitleOutput($output) . '</title>';
return Element::withTag('title')->html(
$this->prepareTitleOutput($output)
)->toHtml();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Entities/Twitter/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ private function checkSite(&$site)
*/
private function prepareUsername($username)
{
return starts_with($username, '@') ? $username : "@{$username}";
return starts_with($username, '@')
? $username :
"@{$username}";
}
}
22 changes: 17 additions & 5 deletions src/Helpers/Meta.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Arcanedev\SeoHelper\Helpers;

use Arcanedev\Html\Elements\Element;
use Arcanedev\Html\Elements\Meta as HtmlMeta;
use Arcanedev\SeoHelper\Contracts\Helpers\Meta as MetaContract;
use Arcanedev\SeoHelper\Exceptions\InvalidArgumentException;
use Illuminate\Support\Arr;
Expand All @@ -22,7 +24,7 @@ class Meta implements MetaContract
*
* @var string
*/
protected $prefix = '';
protected $prefix = '';

/**
* The meta name property.
Expand All @@ -36,7 +38,7 @@ class Meta implements MetaContract
*
* @var string
*/
protected $name = '';
protected $name = '';

/**
* Meta content.
Expand Down Expand Up @@ -194,7 +196,9 @@ public static function make($name, $content, $propertyName = 'name', $prefix = '
*/
public function render()
{
return $this->isLink() ? $this->renderLink() : $this->renderMeta();
return $this->isLink()
? $this->renderLink()
: $this->renderMeta();
}

/**
Expand All @@ -204,7 +208,12 @@ public function render()
*/
private function renderLink()
{
return '<link rel="'.$this->getName(false).'" href="'.$this->getContent().'">';
return Element::withTag('link')
->attributes([
'rel' => $this->getName(false),
'href' => $this->getContent(),
])
->toHtml();
}

/**
Expand All @@ -217,7 +226,10 @@ private function renderMeta()
$content = Arr::wrap($this->getContent());

return implode(PHP_EOL, array_map(function ($content) {
return "<meta {$this->nameProperty}=\"{$this->getName()}\" content=\"{$content}\">";
return HtmlMeta::make()->attributes([
$this->nameProperty => $this->getName(),
'content' => $content,
])->toHtml();
}, $content));
}

Expand Down
57 changes: 57 additions & 0 deletions tests/Asserts/AssertsHtmlStrings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php namespace Arcanedev\SeoHelper\Tests\Asserts;

use DOMDocument;

/**
* Trait AssertsHtmlStrings
*
* @package Arcanedev\SeoHelper\Tests\Asserts
* @author ARCANEDEV <[email protected]>
*/
trait AssertsHtmlStrings
{
/* -----------------------------------------------------------------
| Custom Assertions
| -----------------------------------------------------------------
*/

/**
* Assert two Html strings are equals.
*
* @param string $expected
* @param string $actual
* @param string $message
*/
public static function assertHtmlStringEqualsHtmlString(string $expected, string $actual, string $message = '')
{
static::assertEquals(
static::convertToDomDocument($expected),
static::convertToDomDocument($actual),
$message,
0.0,
10,
true
);
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/

/**
* Convert string to DOMDocument.
*
* @param string $html
*
* @return \DOMDocument
*/
protected static function convertToDomDocument($html)
{
return tap(new DOMDocument, function (DOMDocument $dom) use ($html) {
$dom->loadHTML(preg_replace('/>\s+</', '><', $html));
$dom->preserveWhiteSpace = false;
});
}
}

8 changes: 4 additions & 4 deletions tests/Entities/DescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public function it_can_render()

$expected = '<meta name="description" content="'.$description.'">';

static::assertSame($expected, $this->description->render());
static::assertSame($expected, (string) $this->description);
static::assertHtmlStringEqualsHtmlString($expected, $this->description);
static::assertHtmlStringEqualsHtmlString($expected, $this->description->render());
}

/** @test */
Expand All @@ -156,8 +156,8 @@ public function it_can_render_long_title()

$expected = '<meta name="description" content="'.str_limit($content, $max).'">';

static::assertSame($expected, $this->description->render());
static::assertSame($expected, (string) $this->description);
static::assertHtmlStringEqualsHtmlString($expected, $this->description);
static::assertHtmlStringEqualsHtmlString($expected, $this->description->render());
}

/* -----------------------------------------------------------------
Expand Down
29 changes: 15 additions & 14 deletions tests/Entities/KeywordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,33 @@ public function it_can_render()
$content = $this->getDefaultContent();
$expected = '<meta name="keywords" content="'.implode(', ', $content).'">';

static::assertSame($expected, $this->keywords->render());
static::assertSame($expected, (string) $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords->render());

$this->keywords->set(implode(',', $content));

static::assertSame($expected, $this->keywords->render());
static::assertSame($expected, (string) $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords->render());

$this->keywords->set(implode(' ,', $content));

static::assertSame($expected, $this->keywords->render());
static::assertSame($expected, (string) $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords->render());

$this->keywords->set(implode(', ', $content));

static::assertSame($expected, $this->keywords->render());
static::assertSame($expected, (string) $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords->render());

$this->keywords->set(implode(' , ', $content));

static::assertSame($expected, $this->keywords->render());
static::assertSame($expected, (string) $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords->render());

$this->keywords->set(null);

static::assertEmpty($this->keywords->render());
static::assertEmpty((string) $this->keywords);
static::assertEmpty($this->keywords->render());
}

/** @test */
Expand All @@ -171,11 +171,12 @@ public function it_can_make()
$keywords = $this->getDefaultContent();
$this->keywords = Keywords::make($keywords);

static::assertSame($keywords, $this->keywords->getContent());

$expected = '<meta name="keywords" content="'.implode(', ', $keywords).'">';

static::assertSame($keywords, $this->keywords->getContent());
static::assertSame($expected, $this->keywords->render());
static::assertSame($expected, (string) $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords);
static::assertHtmlStringEqualsHtmlString($expected, $this->keywords->render());
}

/* -----------------------------------------------------------------
Expand Down
14 changes: 5 additions & 9 deletions tests/Entities/OpenGraph/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,12 @@ public function it_can_be_instantiated()
/** @test */
public function it_can_render_defaults()
{
$output = $this->og->render();
$expectations = [
'<meta property="og:type" content="website">',
'<meta property="og:title" content="Default Open Graph title">',
'<meta property="og:description" content="Default Open Graph description">',
];
$output = $this->og->render();
$expected = '<meta property="og:type" content="website">'.
'<meta property="og:title" content="Default Open Graph title">'.
'<meta property="og:description" content="Default Open Graph description">';

foreach ($expectations as $expected) {
static::assertContains($expected, $output);
}
static::assertHtmlStringEqualsHtmlString($expected, $output);
}

/** @test */
Expand Down
Loading

0 comments on commit 5340d09

Please sign in to comment.