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

Enhancement: Simplify ModuleView view helper by simplifying Module service #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion module/User/view/zfc-user/user/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<br />
<div class="tab-content">
<div class="tab-pane active" id="modules">
<?php /* @var ZfModule\Entity\Module[] $modules */ ?>
<?php /* @var stdClass[] $modules */ ?>
<?php if (!count($modules)): ?>
<div class="alert alert-block">No modules have been submitted yet</div>
<?php else: ?>
Expand Down
21 changes: 8 additions & 13 deletions module/ZfModule/src/ZfModule/Service/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function allModules($limit = null)
}

/**
* @return Entity\Module[]
* @return stdClass[]
*/
public function currentUserModules()
{
Expand All @@ -122,25 +122,20 @@ public function currentUserModules()
'per_page' => 100,
]);

$modules = [];

foreach ($repositoryCollection as $repository) {
return array_filter(iterator_to_array($repositoryCollection), function ($repository) {
if (true === $repository->fork) {
continue;
return false;
}

if (false === $repository->permissions->push) {
continue;
return false;
}

$module = $this->moduleMapper->findByName($repository->name);
if (!($module instanceof Entity\Module)) {
continue;
if (null === $this->moduleMapper->findByName($repository->name)) {
return false;
}

array_push($modules, $module);
}

return $modules;
return true;
});
}
}
53 changes: 9 additions & 44 deletions module/ZfModule/src/ZfModule/View/Helper/ModuleView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,29 @@

namespace ZfModule\View\Helper;

use InvalidArgumentException;
use stdClass;
use Zend\View\Helper\AbstractHelper;
use ZfModule\Entity;

class ModuleView extends AbstractHelper
{
const BUTTON_SUBMIT = 'submit';
const BUTTON_REMOVE = 'remove';

/**
* @param Entity\Module|stdClass $module
* @param stdClass $module
* @param string $button
* @return string
*/
public function __invoke($module, $button = 'submit')
{
$values = $this->fetchValues($module);

$values += [
return $this->getView()->render('zf-module/helper/module-view.phtml', [
'owner' => $module->owner->login,
'name' => $module->name,
'createdAt' => $module->created_at,
'url' => $module->html_url,
'photoUrl' => $module->owner->avatar_url,
'description' => $module->description,
'button' => $button,
];

return $this->getView()->render('zf-module/helper/module-view.phtml', $values);
}

/**
* @param stdClass|Entity\Module$module
* @return array
*/
private function fetchValues($module)
{
if ((!$module instanceof stdClass) && !($module instanceof Entity\Module)) {
throw new InvalidArgumentException(sprintf(
'Parameter "%s" needs to be specified as %s',
'$module',
'an instance of stdClass or ZfModule\Entity\Module'
));
}

if ($module instanceof stdClass) {
return [
'owner' => $module->owner->login,
'name' => $module->name,
'createdAt' => $module->created_at,
'url' => $module->html_url,
'photoUrl' => $module->owner->avatar_url,
'description' => $module->description,
];
}

return [
'owner' => $module->getOwner(),
'name' => $module->getName(),
'createdAt' => $module->getCreatedAt(),
'url' => $module->getUrl(),
'photoUrl' => $module->getPhotoUrl(),
'description' => $module->getDescription(),
];
]);
}
}
10 changes: 5 additions & 5 deletions module/ZfModule/test/ZfModuleTest/Service/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public function testListUserModulesListsCurrentUsersModulesFromApiFoundInDatabas
$repository->permissions->push = true;
$repository->name = $name;

$module = $this->getMockBuilder(Entity\Module::class)->getMock();

$modules = [
$module,
$repositories = [
$repository,
];

$module = $this->getMockBuilder(Entity\Module::class)->getMock();

$moduleMapper = $this->getMockBuilder(Mapper\Module::class)->getMock();

$moduleMapper
Expand Down Expand Up @@ -130,7 +130,7 @@ public function testListUserModulesListsCurrentUsersModulesFromApiFoundInDatabas
$githubClient
);

$this->assertSame($modules, $service->currentUserModules());
$this->assertSame($repositories, $service->currentUserModules());
}

public function testListUserModulesDoesNotLookupModulesFromApiWhereUserHasNoPushPrivilege()
Expand Down
66 changes: 2 additions & 64 deletions module/ZfModule/test/ZfModuleTest/View/Helper/ModuleViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPUnit_Framework_TestCase;
use stdClass;
use Zend\View;
use ZfModule\Entity;
use ZfModule\View\Helper;

class ModuleViewTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -39,53 +38,9 @@ public function testInvokeHandlesRepository()
$helper($repository);
}

public function testInvokeHandlesModule()
{
$module = $this->module();

$view = $this->getMockForAbstractClass(View\Renderer\RendererInterface::class);

$view
->expects($this->once())
->method('render')
->with(
$this->equalTo('zf-module/helper/module-view.phtml'),
$this->equalTo([
'owner' => $module->getOwner(),
'name' => $module->getName(),
'createdAt' => $module->getCreatedAt(),
'url' => $module->getUrl(),
'photoUrl' => $module->getPhotoUrl(),
'description' => $module->getDescription(),
'button' => 'submit',
])
)
;

$helper = new Helper\ModuleView();
$helper->setView($view);

$helper($module);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvokeDoesNotHandleAnythingElse()
{
$module = 'foo';

$view = $this->getMockForAbstractClass(View\Renderer\RendererInterface::class);

$helper = new Helper\ModuleView();
$helper->setView($view);

$helper($module);
}

public function testInvokeAllowsSpecifyingButtonType()
{
$module = $this->module();
$repository = $this->repository();
$button = 'baz';

$view = $this->getMockForAbstractClass(View\Renderer\RendererInterface::class);
Expand All @@ -107,7 +62,7 @@ public function testInvokeAllowsSpecifyingButtonType()
$helper = new Helper\ModuleView();
$helper->setView($view);

$helper($module, $button);
$helper($repository, $button);
}

/**
Expand All @@ -128,21 +83,4 @@ private function repository()

return $repository;
}

/**
* @return Entity\Module
*/
private function module()
{
$module = new Entity\Module();

$module->setName('foo');
$module->setDescription('blah blah');
$module->setCreatedAt('1970-01-01 00:00:00');
$module->setUrl('http://www.example.org');
$module->setOwner('suzie');
$module->setPhotoUrl('http://www.example.org/img/suzie.gif');

return $module;
}
}