Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- [#155](https://github.com/zendframework/zend-view/pull/155) modifies the `Zend\View\Helper\Service\IdentifyFactory` such that it will
now also look for the service `Zend\Authentication\AuthenticationServiceInterface`
if the service `Zend\Authentication\AuthenticationService` is not found. This
allows using a service named after the interface instead of the
implementation if desired.

- [#158](https://github.com/zendframework/zend-view/pull/158) modifies how a `ViewModel` (and all extensions) is cloned; the `$variables`
property, if it is an object, is now cloned as well to ensure changes in the
new instance do not affect the current one.
Expand Down
6 changes: 5 additions & 1 deletion doc/book/helpers/identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ The `Identity` helper allows retrieving the identity from the
`AuthenticationService`.

For the `Identity` helper to work, a `Zend\Authentication\AuthenticationService`
name or alias must be defined and recognized by the `ServiceManager`.
or `Zend\Authentication\AuthenticationServiceInterface` name or alias must be
defined and recognized by the `ServiceManager`.

`Identity` returns the identity discovered in the `AuthenticationService`, or
`null` if no identity is available.
Expand Down Expand Up @@ -43,3 +44,6 @@ return [
],
];
```

If that service is not registered, the plugin will then look for a service named
`Zend\Authentication\AuthenticationServiceInterface`, and use that if found.
25 changes: 22 additions & 3 deletions src/Helper/Service/IdentityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\View\Helper\Service;

use Interop\Container\ContainerInterface;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Helper\Identity;
Expand All @@ -30,10 +32,13 @@ public function __invoke(ContainerInterface $container, $name, array $options =
if (! method_exists($container, 'configure')) {
$container = $container->getServiceLocator();
}

$helper = new Identity();
if ($container->has('Zend\Authentication\AuthenticationService')) {
$helper->setAuthenticationService($container->get('Zend\Authentication\AuthenticationService'));

if (null !== ($authenticationService = $this->discoverAuthenticationService($container))) {
$helper->setAuthenticationService($authenticationService);
}

return $helper;
}

Expand All @@ -47,4 +52,18 @@ public function createService(ServiceLocatorInterface $serviceLocator, $rName =
{
return $this($serviceLocator, $cName);
}

/**
* @return null|AuthenticationServiceInterface
*/
private function discoverAuthenticationService(ContainerInterface $container)
{
if ($container->has(AuthenticationService::class)) {
return $container->get(AuthenticationService::class);
}

return $container->has(AuthenticationServiceInterface::class)
? $container->get(AuthenticationServiceInterface::class)
: null;
}
}
67 changes: 67 additions & 0 deletions test/Helper/Service/IdentityFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* @see https://github.com/zendframework/zend-view for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-view/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\View\Helper\Service;

use PHPUnit\Framework\TestCase;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\View\Helper\Identity;
use Zend\View\Helper\Service\IdentityFactory;

class IdentityFactoryTest extends TestCase
{
public function testFactoryReturnsEmptyIdentityIfNoAuthenticationServicePresent()
{
$container = $this->prophesize(ServiceManager::class);
$container->has(AuthenticationService::class)->willReturn(false);
$container->get(AuthenticationService::class)->shouldNotBeCalled();
$container->has(AuthenticationServiceInterface::class)->willReturn(false);
$container->get(AuthenticationServiceInterface::class)->shouldNotBeCalled();

$factory = new IdentityFactory();
$plugin = $factory($container->reveal(), Identity::class);

$this->assertInstanceOf(Identity::class, $plugin);
$this->assertNull($plugin->getAuthenticationService());
}

public function testFactoryReturnsIdentityWithConfiguredAuthenticationServiceWhenPresent()
{
$container = $this->prophesize(ServiceManager::class);
$authentication = $this->prophesize(AuthenticationService::class);

$container->has(AuthenticationService::class)->willReturn(true);
$container->get(AuthenticationService::class)->will([$authentication, 'reveal']);
$container->has(AuthenticationServiceInterface::class)->willReturn(false);
$container->get(AuthenticationServiceInterface::class)->shouldNotBeCalled();

$factory = new IdentityFactory();
$plugin = $factory($container->reveal(), Identity::class);

$this->assertInstanceOf(Identity::class, $plugin);
$this->assertSame($authentication->reveal(), $plugin->getAuthenticationService());
}

public function testFactoryReturnsIdentityWithConfiguredAuthenticationServiceInterfaceWhenPresent()
{
$container = $this->prophesize(ServiceManager::class);
$authentication = $this->prophesize(AuthenticationServiceInterface::class);

$container->has(AuthenticationService::class)->willReturn(false);
$container->get(AuthenticationService::class)->shouldNotBeCalled();
$container->has(AuthenticationServiceInterface::class)->willReturn(true);
$container->get(AuthenticationServiceInterface::class)->will([$authentication, 'reveal']);

$factory = new IdentityFactory();
$plugin = $factory($container->reveal(), Identity::class);

$this->assertInstanceOf(Identity::class, $plugin);
$this->assertSame($authentication->reveal(), $plugin->getAuthenticationService());
}
}