diff --git a/src/Traits/Seoable.php b/src/Traits/Seoable.php index 751213c..ebad09d 100644 --- a/src/Traits/Seoable.php +++ b/src/Traits/Seoable.php @@ -8,5 +8,89 @@ */ trait Seoable { - // + /* ------------------------------------------------------------------------------------------------ + | Helpers Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Get the SeoHelper instance. + * + * @return \Arcanedev\SeoHelper\Contracts\SeoHelper + */ + public function seo() + { + return seo_helper(); + } + + /** + * Get the SeoMeta instance. + * + * @return \Arcanedev\SeoHelper\Contracts\SeoMeta + */ + public function seoMeta() + { + return $this->seo()->meta(); + } + + /** + * Get the SeoOpenGraph instance. + * + * @return \Arcanedev\SeoHelper\Contracts\SeoOpenGraph + */ + public function seoGraph() + { + return $this->seo()->openGraph(); + } + + /** + * Get the SeoTwitter instance. + * + * @return \Arcanedev\SeoHelper\Contracts\SeoTwitter + */ + public function seoCard() + { + return $this->seo()->twitter(); + } + + /* ------------------------------------------------------------------------------------------------ + | Getters & Setters + | ------------------------------------------------------------------------------------------------ + */ + /** + * Set title. + * + * @param string $title + * @param string|null $siteName + * @param string|null $separator + * + * @return \Arcanedev\SeoHelper\Contracts\SeoHelper + */ + public function setTitle($title, $siteName = null, $separator = null) + { + return $this->seo()->setTitle($title, $siteName, $separator); + } + + /** + * Set description. + * + * @param string $description + * + * @return \Arcanedev\SeoHelper\Contracts\SeoHelper + */ + public function setDescription($description) + { + return $this->seo()->setDescription($description); + } + + /** + * Set keywords. + * + * @param array|string $keywords + * + * @return \Arcanedev\SeoHelper\Contracts\SeoHelper + */ + public function setKeywords($keywords) + { + return $this->seo()->setKeywords($keywords); + } } diff --git a/tests/Stubs/Dummy.php b/tests/Stubs/Dummy.php new file mode 100644 index 0000000..0b48b0e --- /dev/null +++ b/tests/Stubs/Dummy.php @@ -0,0 +1,18 @@ + + */ +class Dummy +{ + /* ------------------------------------------------------------------------------------------------ + | Traits + | ------------------------------------------------------------------------------------------------ + */ + use Seoable; +} diff --git a/tests/Traits/SeoableTest.php b/tests/Traits/SeoableTest.php new file mode 100644 index 0000000..5f3987e --- /dev/null +++ b/tests/Traits/SeoableTest.php @@ -0,0 +1,174 @@ + + */ +class SeoableTest extends TestCase +{ + /* ------------------------------------------------------------------------------------------------ + | Properties + | ------------------------------------------------------------------------------------------------ + */ + /** + * @var Dummy + */ + private $dummy; + + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + public function setUp() + { + parent::setUp(); + + $this->dummy = new Dummy; + } + + public function tearDown() + { + parent::tearDown(); + + unset($this->dummy); + } + + /* ------------------------------------------------------------------------------------------------ + | Test Functions + | ------------------------------------------------------------------------------------------------ + */ + /** @test */ + public function it_can_get_main_helper() + { + $seoHelper = $this->dummy->seo(); + + $expectations = [ + \Arcanedev\SeoHelper\Contracts\SeoHelper::class, + \Arcanedev\SeoHelper\Contracts\Renderable::class, + \Arcanedev\SeoHelper\SeoHelper::class, + ]; + + foreach ($expectations as $expected) { + $this->assertInstanceOf($expected, $seoHelper); + } + } + + /** @test */ + public function it_can_get_meta_helper() + { + $seoMeta = $this->dummy->seoMeta(); + + $expectations = [ + \Arcanedev\SeoHelper\Contracts\SeoMeta::class, + \Arcanedev\SeoHelper\Contracts\Renderable::class, + \Arcanedev\SeoHelper\SeoMeta::class, + ]; + + foreach ($expectations as $expected) { + $this->assertInstanceOf($expected, $seoMeta); + } + } + + /** @test */ + public function it_can_get_open_graph_helper() + { + $seoOpenGraph = $this->dummy->seoGraph(); + + $expectations = [ + \Arcanedev\SeoHelper\Contracts\SeoOpenGraph::class, + \Arcanedev\SeoHelper\Contracts\Renderable::class, + \Arcanedev\SeoHelper\SeoOpenGraph::class, + ]; + + foreach ($expectations as $expected) { + $this->assertInstanceOf($expected, $seoOpenGraph); + } + } + + /** @test */ + public function it_can_get_twitter_card_helper() + { + $seoTwitter = $this->dummy->seoCard(); + $expectations = [ + \Arcanedev\SeoHelper\Contracts\SeoTwitter::class, + \Arcanedev\SeoHelper\Contracts\Renderable::class, + \Arcanedev\SeoHelper\SeoTwitter::class, + ]; + + foreach ($expectations as $expected) { + $this->assertInstanceOf($expected, $seoTwitter); + } + } + + /** @test */ + public function it_can_set_and_render_title() + { + $title = 'Hello World'; + $siteName = 'ARCANEDEV'; + $separator = '|'; + $expectations = [ + "$title $separator $siteName", + '', + '', + ]; + + $this->dummy->setTitle($title, $siteName, $separator); + + foreach ($expectations as $expected) { + $this->assertContains($expected, $this->dummy->seo()->render()); + $this->assertContains($expected, (string) $this->dummy->seo()); + + $this->assertContains($expected, seo_helper()->render()); + $this->assertContains($expected, (string) seo_helper()); + } + } + + /** @test */ + public function it_can_set_and_render_description() + { + $description = 'ARCANEDEV super description'; + $expectations = [ + '', + '', + '', + ]; + + $this->dummy->setDescription($description); + + foreach ($expectations as $expected) { + $this->assertContains($expected, $this->dummy->seo()->render()); + $this->assertContains($expected, (string) $this->dummy->seo()); + + $this->assertContains($expected, seo_helper()->render()); + $this->assertContains($expected, (string) seo_helper()); + } + } + + /** @test */ + public function it_can_set_and_render_keywords() + { + $keywords = $this->getSeoHelperConfig('keywords.default'); + $expected = ''; + + $this->dummy->setKeywords($keywords); // Array + + $this->assertContains($expected, $this->dummy->seo()->render()); + $this->assertContains($expected, (string) $this->dummy->seo()); + + $this->assertContains($expected, seo_helper()->render()); + $this->assertContains($expected, (string) seo_helper()); + + $this->dummy->setKeywords(implode(',', $keywords)); // String + + $this->assertContains($expected, $this->dummy->seo()->render()); + $this->assertContains($expected, (string) $this->dummy->seo()); + + $this->assertContains($expected, seo_helper()->render()); + $this->assertContains($expected, (string) seo_helper()); + } +}