This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added daily archive * Added test
- Loading branch information
Showing
6 changed files
with
201 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\NewsBundle\Action; | ||
|
||
use Sonata\IntlBundle\Templating\Helper\DateTimeHelper; | ||
use Sonata\NewsBundle\Model\BlogInterface; | ||
use Sonata\NewsBundle\Model\PostManagerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
|
||
final class DailyPostArchiveAction extends AbstractPostArchiveAction | ||
{ | ||
/** | ||
* @var DateTimeHelper | ||
*/ | ||
private $dateTimeHelper; | ||
|
||
public function __construct( | ||
BlogInterface $blog, | ||
PostManagerInterface $postManager, | ||
TranslatorInterface $translator, | ||
DateTimeHelper $dateTimeHelper | ||
) { | ||
parent::__construct($blog, $postManager, $translator); | ||
|
||
$this->dateTimeHelper = $dateTimeHelper; | ||
} | ||
|
||
/** | ||
* @param string $year | ||
* @param string $month | ||
* @param string $day | ||
* | ||
* @return Response | ||
*/ | ||
public function __invoke(Request $request, $year, $month, $day) | ||
{ | ||
$date = $this->getPostManager()->getPublicationDateQueryParts(sprintf('%d-%d-%d', $year, $month, $day), 'day'); | ||
|
||
if ($seoPage = $this->getSeoPage()) { | ||
$seoDescription = $this->getBlog()->getDescription(); | ||
$seoFormat = $this->dateTimeHelper->format($date, 'MMMM'); | ||
$seoDate = $this->dataTimeHelper->formatDate($date); | ||
|
||
$seoPage | ||
->setTitle($this->trans('archive_day.meta_title', [ | ||
'%title%' => $this->getBlog()->getTitle(), | ||
'%year%' => $year, | ||
'%month%' => $seoFormat, | ||
'%day%' => $day, | ||
'%date%' => $seoDate, | ||
])) | ||
->addMeta('property', 'og:title', $this->trans('archive_day.meta_title', [ | ||
'%title%' => $this->getBlog()->getTitle(), | ||
'%year%' => $year, | ||
'%month%' => $seoFormat, | ||
'%day%' => $day, | ||
'%date%' => $seoDate, | ||
])) | ||
->addMeta('name', 'description', $this->trans('archive_day.meta_description', [ | ||
'%title%' => $this->getBlog()->getTitle(), | ||
'%year%' => $year, | ||
'%month%' => $seoFormat, | ||
'%day%' => $day, | ||
'%description%' => $seoDescription, | ||
'%date%' => $seoDate, | ||
])) | ||
->addMeta('property', 'og:description', $this->trans('archive_day.meta_description', [ | ||
'%title%' => $this->getBlog()->getTitle(), | ||
'%year%' => $year, | ||
'%month%' => $seoFormat, | ||
'%day%' => $day, | ||
'%date%' => $seoDate, | ||
'%description%' => $seoDescription, | ||
])); | ||
} | ||
|
||
return $this->renderArchive($request, [ | ||
'date' => $date, | ||
], []); | ||
} | ||
} |
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
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
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,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Tests\Action; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Sonata\DatagridBundle\Pager\PagerInterface; | ||
use Sonata\IntlBundle\Templating\Helper\DateTimeHelper; | ||
use Sonata\NewsBundle\Action\DailyPostArchiveAction; | ||
use Sonata\NewsBundle\Entity\PostManager; | ||
use Sonata\NewsBundle\Model\BlogInterface; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Translation\TranslatorInterface; | ||
use Twig\Environment; | ||
|
||
class DailyPostArchiveActionTest extends TestCase | ||
{ | ||
public function testInvoke() | ||
{ | ||
$blog = $this->prophesize(BlogInterface::class); | ||
$translator = $this->prophesize(TranslatorInterface::class); | ||
$dateTimeHelper = $this->prophesize(DateTimeHelper::class); | ||
|
||
$dataParams = [ | ||
'query' => 'foo.publicationDateStart >= :startDate AND bar.publicationDateStart < :endDate', | ||
'params' => [ | ||
'startDate' => new \DateTime(), | ||
'endDate' => new \DateTime('tomorrow'), | ||
], | ||
]; | ||
|
||
$postManager = $this->prophesize(PostManager::class); | ||
$postManager->getPublicationDateQueryParts('2018-7-8', 'day') | ||
->willReturn($dataParams); | ||
$postManager->getPager([ | ||
'date' => $dataParams, | ||
], 1) | ||
->willReturn($this->prophesize(PagerInterface::class)); | ||
|
||
$twig = $this->prophesize(Environment::class); | ||
$twig->render('@SonataNews/Post/archive.html.twig', Argument::any()) | ||
->willReturn('HTML CONTENT'); | ||
|
||
$container = new Container(); | ||
$container->set('twig', $twig->reveal()); | ||
|
||
$action = new DailyPostArchiveAction( | ||
$blog->reveal(), | ||
$postManager->reveal(), | ||
$translator->reveal(), | ||
$dateTimeHelper->reveal() | ||
); | ||
$action->setContainer($container); | ||
|
||
$request = new Request(); | ||
$request->query->set('page', 1); | ||
|
||
$response = $action($request, 2018, 7, 8); | ||
|
||
$this->assertInstanceOf(Response::class, $response); | ||
} | ||
} |