Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Refactor for new service and event manager #8

Merged
merged 10 commits into from
Dec 14, 2015

Conversation

kynx
Copy link
Contributor

@kynx kynx commented Oct 25, 2015

This PR contains changes to get zend-session working with the new zend-servicemanager and zend-eventmanager.

I've tried to keep changes to a minimum. The main points are:

  • factories updated to follow new ServiceManager FactoryInterface
  • ValidatorChain::construct() now accept an EventInterface as an optional second arg to use as a prototype
  • tests updated to mock Zend\Cache

@kynx kynx changed the title Refactor service event manager Refactor for mew service and event manager Oct 25, 2015
@kynx kynx changed the title Refactor for mew service and event manager Refactor for new service and event manager Oct 25, 2015
@@ -21,7 +21,7 @@
*
* @return bool
*/
public function isValid();
public function __invoke();
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be changed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what else can be done. These things end up in EventManager::triggerListeners(), which does a $listener($event), not a $listener->isValid() or a call_user_func($listener). Suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

Previous format was correct. Implementators was using [new $validator($data), 'isValid'] which is callable too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I'll be. Right you are. Must've been something else in there breaking all the tests for validators then.

@Maks3w
Copy link
Member

Maks3w commented Oct 25, 2015

As far as I see we could decouple zend_cache implementation and mock the interfaces.

Could you refactor the test and mock the dependency in a different PR?


$manager->start();

$this->assertEquals(1, $manager->getValidatorChain()->getListeners('session.validate')->count());
$chain = $manager->getValidatorChain();
$r = new \ReflectionMethod($chain, 'getListenersByEventName');
Copy link
Member

Choose a reason for hiding this comment

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

I would like to see an alternative to this using the public API.

Copy link
Member

Choose a reason for hiding this comment

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

@Maks3w - for tests, I've been using a similar pattern in other components.
The getListeners and getEvents methods only really existed for testing
purposes, which is why they were removed during the refactor. The downside
is, of course, that testing now often needs to rely on reflection to
validate expectations. My point is: there isn't a way currently to verify
using the public API at this point other than triggering the event, which
often has side effects. Reflection should be fine here.
On Oct 25, 2015 2:09 PM, "Maks3w" notifications@github.com wrote:

In test/Service/SessionManagerFactoryTest.php
#8 (comment)
:

     $manager->start();
  •    $this->assertEquals(1, $manager->getValidatorChain()->getListeners('session.validate')->count());
    
  •    $chain = $manager->getValidatorChain();
    
  •    $r = new \ReflectionMethod($chain, 'getListenersByEventName');
    

I would like to see an alternative to this using the public API.


Reply to this email directly or view it on GitHub
https://github.com/zendframework/zend-session/pull/8/files#r42948778.

Copy link
Member

Choose a reason for hiding this comment

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

And spy EventManagerInterface::attach was called?

Copy link
Member

Choose a reason for hiding this comment

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

@Maks3w Correct. That's what the reflection calls are doing, essentially; verifying that the factory attached listeners for specific event names.

* @param callable $callback
* @param int $priority
* @return \Zend\Stdlib\CallbackHandler
*/
public function attach($event, $callback = null, $priority = 1)
public function attach($eventName, callable $callback, $priority = 1)
Copy link
Member

Choose a reason for hiding this comment

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

Note the callable typehint make impossible to be array

Copy link
Member

Choose a reason for hiding this comment

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

Not true; arrays can be php callables. [$instance, $methodName] validates
as callable when:

  • $instance is an object where $methodName is a publicly callable
    method, OR
  • $instance is an object or class name and $methodName is a publicly
    callable static method.
    On Oct 25, 2015 2:30 PM, "Maks3w" notifications@github.com wrote:

In src/ValidatorChain.php
#8 (comment)
:

  * @param  callable $callback
  * @param  int $priority
  * @return \Zend\Stdlib\CallbackHandler
  */
  • public function attach($event, $callback = null, $priority = 1)
  • public function attach($eventName, callable $callback, $priority = 1)

Note the callable typehint make impossible to be array


Reply to this email directly or view it on GitHub
https://github.com/zendframework/zend-session/pull/8/files#r42949108.

@kynx
Copy link
Contributor Author

kynx commented Oct 26, 2015

I've updated the initial description of this PR to reflect the changes made following @Maks3w review. Anything more need doing, or can I go out and play with expressive now ;)

@kynx
Copy link
Contributor Author

kynx commented Nov 22, 2015

Heya, this has kind of slipped of my radar. But it's useful if you're using zend-session with dev versions of zend-eventmanager.

I'm pretty sure the coverage decrease isn't down to me. Is there anything else you can see?

@@ -9,8 +9,8 @@

namespace Zend\Session\Service;

use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
Copy link
Member

Choose a reason for hiding this comment

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

container-interop should be added as a dev dependency. Yes, the SM depends on it as well, but you're explicitly referencing it in code here, which makes it another dependency.

@weierophinney
Copy link
Member

@kynx Are you able/willing to address the feedback I've posted today? If not, let me know, and one of us will create a new PR that pulls in your changes and finishes them.

If you are, make sure you look through the PRs merged to the SM and EM (mostly the SM) since you started this pull request, as we've made a number of changes that simplify the migration tremendously (e.g., it's no longer immutable; has() no longer has the second optional argument; the various methods for injecting factories are re-instated; etc.).

Thanks in advance!

@kynx
Copy link
Contributor Author

kynx commented Dec 8, 2015

@weierophinney I'm pretty stacked out until the weekend, so please feel free to take this over if anyone has time before then. Cheers!

@ezimuel ezimuel merged commit 933cc1a into zendframework:develop Dec 14, 2015
@ezimuel
Copy link
Contributor

ezimuel commented Dec 14, 2015

@kynx I merged this PR including the feedbacks of @weierophinney and fixed some unit test.
Thanks for your work!

@kynx kynx deleted the refactor-service-event-manager branch January 11, 2016 16:23
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.

4 participants