-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SeoBundle] Added event to populate robots.txt
- Loading branch information
Patrick Berenschot
committed
Sep 9, 2021
1 parent
d5d7cf1
commit 00daad2
Showing
10 changed files
with
339 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\Event; | ||
|
||
class RobotsEvent | ||
{ | ||
private $content; | ||
|
||
public function __construct(string $content = "") | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function setContent(string $content): void | ||
{ | ||
$this->content = $content; | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Kunstmaan/SeoBundle/EventListener/AdminRobotsTxtListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\EventListener; | ||
|
||
use Doctrine\Persistence\ObjectRepository; | ||
use Kunstmaan\SeoBundle\Entity\Robots; | ||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class AdminRobotsTxtListener implements EventSubscriberInterface | ||
{ | ||
private $repository; | ||
|
||
public function __construct(ObjectRepository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RobotsEvent::class => ["__invoke", 100] | ||
]; | ||
} | ||
|
||
public function __invoke(RobotsEvent $event): void | ||
{ | ||
$entity = $this->repository->findOneBy([]); | ||
if (!$entity instanceof Robots) { | ||
return; | ||
} | ||
|
||
$content = $entity->getRobotsTxt(); | ||
if ($content === null) { | ||
return; | ||
} | ||
|
||
$event->setContent($content); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Kunstmaan/SeoBundle/EventListener/FileRobotsTxtListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\EventListener; | ||
|
||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class FileRobotsTxtListener implements EventSubscriberInterface | ||
{ | ||
private $path; | ||
|
||
public function __construct(string $path) | ||
{ | ||
$this->path = $path; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RobotsEvent::class => ["__invoke", 0] | ||
]; | ||
} | ||
|
||
public function __invoke(RobotsEvent $event): void | ||
{ | ||
if (empty($event->getContent()) && file_exists($this->path)) { | ||
$event->setContent(file_get_contents($this->path)); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Kunstmaan/SeoBundle/EventListener/ParameterRobotsTxtListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\EventListener; | ||
|
||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class ParameterRobotsTxtListener implements EventSubscriberInterface | ||
{ | ||
private $fallback; | ||
|
||
public function __construct(string $fallback) | ||
{ | ||
$this->fallback = $fallback; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RobotsEvent::class => ["__invoke", -100] | ||
]; | ||
} | ||
|
||
public function __invoke(RobotsEvent $event): void | ||
{ | ||
if (empty($event->getContent())) { | ||
$event->setContent($this->fallback); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\Tests\Event; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
|
||
class RobotsEventTest extends TestCase | ||
{ | ||
public function testShouldAllowSettingContext(): void | ||
{ | ||
$initialContent = "Current content"; | ||
$object = new RobotsEvent($initialContent); | ||
|
||
$result = $object->getContent(); | ||
$this->assertEquals($initialContent, $result); | ||
|
||
$newContent = "$result\nAdded"; | ||
$object->setContent($newContent); | ||
|
||
$this->assertEquals($newContent, $object->getContent()); | ||
} | ||
|
||
public function testShouldDefaultToEmptyContent(): void | ||
{ | ||
$object = new RobotsEvent(); | ||
|
||
$result = $object->getContent(); | ||
$this->assertEquals("", $result); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\Tests\EventListener; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Kunstmaan\SeoBundle\Entity\Robots; | ||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
use PHPUnit\Framework\TestCase; | ||
use Kunstmaan\SeoBundle\EventListener\AdminRobotsTxtListener; | ||
|
||
class AdminRobotsTxtListenerTest extends TestCase | ||
{ | ||
private $repoMock; | ||
private const CONTENT = "User-agent: * | ||
Allow: /"; | ||
|
||
public function testShouldSetContentWhenEntityExists() | ||
{ | ||
$filled = new Robots(); | ||
$filled->setRobotsTxt(self::CONTENT); | ||
|
||
$this->repoMock = $this->createMock(EntityRepository::class); | ||
$this->repoMock->expects($this->any()) | ||
->method("findOneBy") | ||
->with([]) | ||
->willReturn($filled); | ||
|
||
$event = new RobotsEvent(); | ||
$listener = new AdminRobotsTxtListener($this->repoMock); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals(self::CONTENT, $event->getContent()); | ||
} | ||
|
||
public function testShouldDoNothingWhenEntityMissing() | ||
{ | ||
$this->repoMock = $this->createMock(EntityRepository::class); | ||
$this->repoMock->expects($this->any()) | ||
->method("findOneBy") | ||
->with([]) | ||
->willReturn(null); | ||
|
||
$event = new RobotsEvent("untouched"); | ||
$listener = new AdminRobotsTxtListener($this->repoMock); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals("untouched", $event->getContent()); | ||
} | ||
|
||
public function testShouldDoNothingWhenEntityEmpty() | ||
{ | ||
$empty = new Robots(); | ||
|
||
$this->repoMock = $this->createMock(EntityRepository::class); | ||
$this->repoMock->expects($this->any()) | ||
->method("findOneBy") | ||
->with([]) | ||
->willReturn($empty); | ||
|
||
$event = new RobotsEvent("untouched"); | ||
$listener = new AdminRobotsTxtListener($this->repoMock); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals("untouched", $event->getContent()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\Tests\EventListener; | ||
|
||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
use PHPUnit\Framework\TestCase; | ||
use Kunstmaan\SeoBundle\EventListener\FileRobotsTxtListener; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class FileRobotsTxtListenerTest extends TestCase | ||
{ | ||
private const CONTENT = "User-agent: * | ||
Allow: /"; | ||
|
||
public function testShouldDoNothingWhenFilled(): void | ||
{ | ||
$event = new RobotsEvent(); | ||
$event->setContent(self::CONTENT); | ||
$listener = new FileRobotsTxtListener(__FILE__); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals(self::CONTENT, $event->getContent()); | ||
} | ||
|
||
public function testShouldSetContentFromFileWhenEmpty(): void | ||
{ | ||
$event = new RobotsEvent(); | ||
$listener = new FileRobotsTxtListener(__FILE__); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals(file_get_contents(__FILE__), $event->getContent()); | ||
} | ||
|
||
public function testShouldDoNothingWhenFileDoesNotExists(): void { | ||
|
||
$event = new RobotsEvent(); | ||
$listener = new FileRobotsTxtListener("/some/none/existing/file"); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals("", $event->getContent()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Kunstmaan/SeoBundle/Tests/EventListener/ParameterRobotsTxtListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kunstmaan\SeoBundle\Tests\EventListener; | ||
|
||
use Kunstmaan\SeoBundle\Event\RobotsEvent; | ||
use PHPUnit\Framework\TestCase; | ||
use Kunstmaan\SeoBundle\EventListener\ParameterRobotsTxtListener; | ||
|
||
class ParameterRobotsTxtListenerTest extends TestCase | ||
{ | ||
private const CONTENT = "User-agent: * | ||
Allow: /"; | ||
|
||
public function testShouldSetContentWhenEmpty() | ||
{ | ||
$event = new RobotsEvent(); | ||
$listener = new ParameterRobotsTxtListener("fallback content"); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals("fallback content", $event->getContent()); | ||
} | ||
|
||
public function testShouldSetDoNothingWhenFilled() | ||
{ | ||
$event = new RobotsEvent(self::CONTENT); | ||
$listener = new ParameterRobotsTxtListener("fallback content"); | ||
$listener->__invoke($event); | ||
|
||
$this->assertEquals(self::CONTENT, $event->getContent()); | ||
} | ||
} |