This repository has been archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #414 from snapshotpl/feed
Add feed mapper
- Loading branch information
Showing
9 changed files
with
212 additions
and
27 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
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,80 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
namespace ZfModule\Mapper; | ||
|
||
use Zend\Feed\Writer\Entry; | ||
use Zend\Feed\Writer\Feed; | ||
use Zend\Mvc\Controller\Plugin\Url as UrlPlugin; | ||
use ZfModule\Entity\Module as ModuleEntity; | ||
|
||
/** | ||
* ModuleToFeed | ||
* | ||
* @author Witold Wasiczko <witold@wasiczko.pl> | ||
*/ | ||
class ModuleToFeed | ||
{ | ||
/** | ||
* @var Feed | ||
*/ | ||
protected $feed; | ||
|
||
/** | ||
* @var UrlPlugin | ||
*/ | ||
protected $urlPlugin; | ||
|
||
/** | ||
* @param Feed $feed | ||
*/ | ||
public function __construct(Feed $feed, UrlPlugin $urlPlugin) | ||
{ | ||
$this->feed = $feed; | ||
$this->urlPlugin = $urlPlugin; | ||
} | ||
|
||
/** | ||
* @param array $modules | ||
*/ | ||
public function addModules($modules) | ||
{ | ||
foreach ($modules as $module) { | ||
$this->addModule($module); | ||
} | ||
} | ||
|
||
/** | ||
* @param ModuleEntity $module | ||
* @return Entry | ||
*/ | ||
public function addModule(ModuleEntity $module) | ||
{ | ||
$moduleDescription = $module->getDescription(); | ||
|
||
if (empty($moduleDescription)) { | ||
$moduleDescription = 'No description available'; | ||
} | ||
|
||
$moduleName = $module->getName(); | ||
$urlParams = ['vendor' => $module->getOwner(), 'module' => $moduleName]; | ||
|
||
$entry = $this->feed->createEntry(); | ||
|
||
$entry->setId($module->getIdentifier()); | ||
$entry->setTitle($moduleName); | ||
$entry->setDescription($moduleDescription); | ||
$entry->setLink($this->urlPlugin->fromRoute('view-module', $urlParams, ['force_canonical' => true])); | ||
$entry->addAuthor(['name' => $module->getOwner()]); | ||
$entry->setDateCreated($module->getCreatedAtDateTime()); | ||
|
||
$this->feed->addEntry($entry); | ||
|
||
return $entry; | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
module/ZfModule/test/ZfModuleTest/Mapper/ModuleToFeedTest.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,69 @@ | ||
<?php | ||
|
||
namespace ZfModuleTest\Mapper; | ||
|
||
class ModuleToFeedTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAddModule() | ||
{ | ||
$dateTime = new \DateTime(); | ||
|
||
$module = $this->getMockBuilder(\ZfModule\Entity\Module::class)->getMock(); | ||
$module->expects($this->once())->method('getName')->willReturn('name'); | ||
$module->expects($this->once())->method('getDescription')->willReturn('description'); | ||
$module->expects($this->once())->method('getCreatedAtDateTime')->willReturn($dateTime); | ||
|
||
$entry = $this->getMockBuilder(\Zend\Feed\Writer\Entry::class)->disableOriginalConstructor()->getMock(); | ||
$entry->expects($this->once())->method('setTitle')->with('name'); | ||
$entry->expects($this->once())->method('setDescription')->with('description'); | ||
$entry->expects($this->once())->method('setLink')->with('url'); | ||
$entry->expects($this->once())->method('setDateCreated')->with($dateTime); | ||
|
||
$url = $this->getMockBuilder(\Zend\Mvc\Controller\Plugin\Url::class)->getMock(); | ||
$url->expects($this->once())->method('fromRoute')->willReturn('url'); | ||
|
||
$feed = $this->getMockBuilder(\Zend\Feed\Writer\Feed::class)->disableOriginalConstructor()->getMock(); | ||
$feed->expects($this->once())->method('createEntry')->willReturn($entry); | ||
$feed->expects($this->once())->method('addEntry')->with($entry); | ||
|
||
$moduleToFeed = new \ZfModule\Mapper\ModuleToFeed($feed, $url); | ||
$moduleToFeed->addModule($module); | ||
} | ||
|
||
public function testAddModuleWithoutDesctription() | ||
{ | ||
$dateTime = new \DateTime(); | ||
|
||
$module = $this->getMockBuilder(\ZfModule\Entity\Module::class)->getMock(); | ||
$module->expects($this->once())->method('getName')->willReturn('name'); | ||
$module->expects($this->once())->method('getDescription')->willReturn(null); | ||
$module->expects($this->once())->method('getCreatedAtDateTime')->willReturn($dateTime); | ||
|
||
$entry = $this->getMockBuilder(\Zend\Feed\Writer\Entry::class)->disableOriginalConstructor()->getMock(); | ||
$entry->expects($this->once())->method('setTitle')->with('name'); | ||
$entry->expects($this->once())->method('setDescription')->with('No description available'); | ||
$entry->expects($this->once())->method('setLink')->with('url'); | ||
$entry->expects($this->once())->method('setDateCreated')->with($dateTime); | ||
|
||
$url = $this->getMockBuilder(\Zend\Mvc\Controller\Plugin\Url::class)->getMock(); | ||
$url->expects($this->once())->method('fromRoute')->willReturn('url'); | ||
|
||
$feed = $this->getMockBuilder(\Zend\Feed\Writer\Feed::class)->disableOriginalConstructor()->getMock(); | ||
$feed->expects($this->once())->method('createEntry')->willReturn($entry); | ||
$feed->expects($this->once())->method('addEntry')->with($entry); | ||
|
||
$moduleToFeed = new \ZfModule\Mapper\ModuleToFeed($feed, $url); | ||
$moduleToFeed->addModule($module); | ||
} | ||
|
||
public function testAddMultipleModules() | ||
{ | ||
$modulesCount = 11; | ||
$module = $this->getMockBuilder(\ZfModule\Entity\Module::class)->getMock(); | ||
$modules = array_fill(0, $modulesCount, $module); | ||
|
||
$moduleToFeed = $this->getMockBuilder(\ZfModule\Mapper\ModuleToFeed::class)->setMethods(['addModule'])->disableOriginalConstructor()->getMock(); | ||
$moduleToFeed->expects($this->exactly($modulesCount))->method('addModule')->with($module); | ||
$moduleToFeed->addModules($modules); | ||
} | ||
} |
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