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

Updated Title Entity #59

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
25 changes: 21 additions & 4 deletions src/MetaTags/Entities/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function setSeparator(string $separator): self
protected function makeTitle(): string
{
$separator = sprintf(' %s ', $this->separator);
$title = '';
$title = '';

if (!empty($this->prepend)) {
$parts = $this->rtl ? $this->prepend : array_reverse($this->prepend);
Expand All @@ -78,8 +78,7 @@ protected function makeTitle(): string
} else {
$title .= $separator . $this->title;
}

} else if (!empty($this->title)) {
} elseif (!empty($this->title)) {
$title = $this->title;
}

Expand All @@ -99,7 +98,7 @@ public function __toString(): string
public function toArray(): array
{
return [
'tag' => 'title',
'tag' => 'title',
'content' => $this->makeTitle()
];
}
Expand All @@ -108,4 +107,22 @@ public function getTitle(): string
{
return $this->makeTitle();
}

/**
* Get prepends title
* @return array
*/
public function getPrepends(): array
{
return $this->prepend;
}

/**
* Get prepend title
* @return string
*/
public function getPrepend($index = 0): ?string
{
return $this->prepend[$index] ?? null;
}
}
95 changes: 80 additions & 15 deletions tests/MetaTags/TitleMetaTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TitleMetaTagsTest extends TestCase
{
function test_title_can_be_set()
public function test_title_can_be_set()
{
$meta = $this->makeMetaTags()
->setTitle('test title');
Expand All @@ -17,7 +17,7 @@ function test_title_can_be_set()
);
}

function test_get_title()
public function test_get_title()
{
$meta = $this->makeMetaTags()
->setTitle('test title');
Expand All @@ -28,7 +28,7 @@ function test_get_title()
);
}

function test_title_can_be_null()
public function test_title_can_be_null()
{
$meta = $this->makeMetaTags()
->setTitle(null);
Expand All @@ -39,7 +39,7 @@ function test_title_can_be_null()
);
}

function test_title_can_be_prepend()
public function test_title_can_be_prepend()
{
$meta = $this->makeMetaTags()
->setTitle('test title')
Expand All @@ -51,7 +51,7 @@ function test_title_can_be_prepend()
);
}

function test_title_can_be_prepend_as_null_value()
public function test_title_can_be_prepend_as_null_value()
{
$meta = $this->makeMetaTags()
->setTitle('test title')
Expand All @@ -63,7 +63,7 @@ function test_title_can_be_prepend_as_null_value()
);
}

function test_prepend_title_should_be_cleaned()
public function test_prepend_title_should_be_cleaned()
{
$meta = $this->makeMetaTags()
->setTitle('test title')
Expand All @@ -75,7 +75,7 @@ function test_prepend_title_should_be_cleaned()
);
}

function test_title_can_be_prepend_if_title_not_set()
public function test_title_can_be_prepend_if_title_not_set()
{
$meta = $this->makeMetaTags()
->prependTitle('prepend part');
Expand All @@ -86,7 +86,7 @@ function test_title_can_be_prepend_if_title_not_set()
);
}

function test_prepend_separator_can_be_changed()
public function test_prepend_separator_can_be_changed()
{
$meta = $this->makeMetaTags()
->setTitle('test title')
Expand All @@ -99,14 +99,14 @@ function test_prepend_separator_can_be_changed()
);
}

function test_set_title_method_should_be_fluent()
public function test_set_title_method_should_be_fluent()
{
$meta = $this->makeMetaTags();

$this->assertEquals($meta, $meta->setTitle('test title'));
}

function test_title_string_should_be_cleaned()
public function test_title_string_should_be_cleaned()
{
$meta = $this->makeMetaTags()
->setTitle('<h5>test title</h5>');
Expand All @@ -117,15 +117,15 @@ function test_title_string_should_be_cleaned()
);
}

function test_title_can_be_limited()
public function test_title_can_be_limited()
{
$this->assertHtmlableContains(
'<title>test...</title>',
$this->makeMetaTags()->setTitle('test title', 4)
);
}

function test_gets_max_title_length_from_config_by_default()
public function test_gets_max_title_length_from_config_by_default()
{
$config = $this->makeConfig();
$config->shouldReceive('get')->once()->with('meta_tags.title.max_length', null)->andReturn(4);
Expand All @@ -137,7 +137,7 @@ function test_gets_max_title_length_from_config_by_default()
);
}

function test_gets_default_title_separator_from_config_by_default()
public function test_gets_default_title_separator_from_config_by_default()
{
$config = $this->makeConfig();
$config->shouldReceive('get')->twice()->with('meta_tags.title.max_length', null)->andReturn(null);
Expand All @@ -155,14 +155,79 @@ function test_gets_default_title_separator_from_config_by_default()
);
}

function test_converts_to_array()
public function test_converts_to_array()
{
$this->assertEquals(
[
'content' => 'test title',
'tag' => 'title',
'tag' => 'title',
],
$this->makeMetaTags()->setTitle('test title')->getTitle()->toArray()
);
}

public function test_get_prepends()
{
$this->assertEquals(
[
'Pre1',
'Pre2',
],
$this->makeMetaTags()
->setTitle('test title')
->prependTitle('Pre1')
->prependTitle('Pre2')
->getTitle()
->getPrepends()
);
}

public function test_get_prepend()
{
$this->assertEquals(
'Pre1',
$this->makeMetaTags()
->setTitle('test title')
->prependTitle('Pre1')
->prependTitle('Pre2')
->getTitle()
->getPrepend(0)
);

$this->assertEquals(
'Pre2',
$this->makeMetaTags()
->setTitle('test title')
->prependTitle('Pre1')
->prependTitle('Pre2')
->getTitle()
->getPrepend(1)
);

$this->assertNull(
$this->makeMetaTags()
->setTitle('test title')
->prependTitle('Pre1')
->prependTitle('Pre2')
->getTitle()
->getPrepend(2)
);

$this->assertEquals(
'Pre1',
$this->makeMetaTags()
->setTitle('test title')
->prependTitle('Pre1')
->prependTitle('Pre2')
->getTitle()
->getPrepend()
);

$this->assertNull(
$this->makeMetaTags()
->setTitle('test title')
->getTitle()
->getPrepend()
);
}
}