Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Fix: View helper should actually be a controller plugin #362

Merged

Conversation

localheinz
Copy link
Member

This PR

  • turns a view helper which should be a controller plugin into a controller plugin
  • implements an extension of the ZfcUser\Controller\UserController in order to set view variables in the view model returned from the indexAction()

@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch from bc5a0ba to af3da63 Compare February 4, 2015 13:47
@Ocramius
Copy link
Member

Ocramius commented Feb 4, 2015

@localheinz needs a rebase

@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch from 83a3ba6 to b8410f0 Compare February 4, 2015 13:50
@localheinz
Copy link
Member Author

Rebased, @Ocramius! Thank you for the review.

@@ -23,7 +23,6 @@
<div class="tab-content">
<div class="tab-pane active" id="modules">
<?php
$modules = $this->listModule(['user' => true]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually agree with this. Why was it moved to a controller plugin? What's the rationale behind it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see from what the view helper does:

  • it accesses the database layer
  • it makes calls to an external API
  • it returns an array of data

As far as my experience goes, these are all things I would never expect from a view helper.

Instead, I would see the responsibility of a view helper in accepting some data and preparing it for presentation in the view, for example, by applying some formatting or filling the data into a view template.

Please note that, as far as I can see, I wouldn't even create a controller plugin just yet, as it's only ever used once. Instead, I'd probably leave it to a service.

This was just the smallest refactoring possible, I believe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are all things I would never expect from a view helper

I disagree with this, but I also see why you'd think that. As long as these are all read operations, IMO it's fine to have them in view helpers. Yes, they sould not cause changes/events to be triggered.

Instead, I'd probably leave it to a service

Possibly a good solution, yes.

@localheinz
Copy link
Member Author

@Ocramius

Also see #308.

*/
class UserController extends ZfcUserController
{
public function indexAction()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This controller has no associated tests

@Ocramius
Copy link
Member

Ocramius commented Feb 4, 2015

Once the missing tests are introduced (integration with @coversNothing is fine) I can merge. I'm fine with the current approach after discussion.

@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch 2 times, most recently from 1dab7d0 to 3744c8a Compare February 4, 2015 19:32
@localheinz
Copy link
Member Author

@Ocramius

Added a test for the case when the authentication service has no identity.

Tried to add a test for the success case and have almost been mocking myself to death and/or trying to detach listeners, still getting failing tests because of that.

@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch 4 times, most recently from 553153d to 076ac84 Compare February 4, 2015 19:47
@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch 2 times, most recently from 15ab3eb to 557e7da Compare February 4, 2015 19:51
)
;

$listModule = $this->getMockBuilder(ListModule::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mocking $listModule as function () {...} is enough

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(if it makes your life easier: otherwise, current approach is fine)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius

I think mocking $listModule as function () {...} is enough

Doesn't work, as PluginManager is validating and asking for an implementation of Zend\Mvc\Controller\Plugin\PluginInterface. However, I mocked out all of the view helpers giving me a headache, tests should pass now!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grrr @ PluginManager.

Yeah, mocking controllers is a mess

@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch from 84a9f53 to 099812e Compare February 4, 2015 22:33
@localheinz localheinz force-pushed the fix/controller-plugin-vs-view-helper branch from 099812e to c81f8f8 Compare February 4, 2015 22:52
Ocramius added a commit that referenced this pull request Feb 4, 2015
…-helper

Fix: View helper should actually be a controller plugin
@Ocramius Ocramius merged commit a8c4db1 into zendframework:master Feb 4, 2015
@Ocramius
Copy link
Member

Ocramius commented Feb 4, 2015

@localheinz let it be merged!

@localheinz
Copy link
Member Author

😊

@localheinz localheinz deleted the fix/controller-plugin-vs-view-helper branch February 4, 2015 23:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants