Skip to content

Commit

Permalink
Merge pull request #35 from Slamdunk/depend_on_interface
Browse files Browse the repository at this point in the history
UrlHelperMiddleware: depend on the new interface
  • Loading branch information
gsteel committed Mar 14, 2023
2 parents f22dff3 + f32211c commit b2489bb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/UrlHelperMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Mezzio\Helper;

use Mezzio\Helper\UrlHelper;
use Mezzio\Router\RouteResult;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -16,7 +15,7 @@
*/
class UrlHelperMiddleware implements MiddlewareInterface
{
public function __construct(private UrlHelper $helper)
public function __construct(private UrlHelperInterface $helper)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/UrlHelperMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __invoke(ContainerInterface $container): UrlHelperMiddleware
}

$helper = $container->get($this->urlHelperServiceName);
assert($helper instanceof UrlHelper);
assert($helper instanceof UrlHelperInterface);

return new UrlHelperMiddleware($helper);
}
Expand Down
13 changes: 13 additions & 0 deletions test/UrlHelperMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Mezzio\Helper\Exception\MissingHelperException;
use Mezzio\Helper\UrlHelper;
use Mezzio\Helper\UrlHelperInterface;
use Mezzio\Helper\UrlHelperMiddleware;
use Mezzio\Helper\UrlHelperMiddlewareFactory;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -90,4 +91,16 @@ public function testFactoryAllowsSerialization(): void
self::assertInstanceOf(UrlHelperMiddlewareFactory::class, $factory);
self::assertAttributeSame('MyUrlHelper', 'urlHelperServiceName', $factory);
}

public function testFactoryAllowsCustomUrlHelperInterfaceImplementations(): void
{
$helper = $this->createMock(UrlHelperInterface::class);
$this->injectContainer('MyUrlHelper', $helper);
$factory = new UrlHelperMiddlewareFactory('MyUrlHelper');

$middleware = $factory($this->container);

self::assertInstanceOf(UrlHelperMiddleware::class, $middleware);
self::assertAttributeSame($helper, 'helper', $middleware);
}
}
8 changes: 4 additions & 4 deletions test/UrlHelperMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MezzioTest\Helper;

use Mezzio\Helper\UrlHelper;
use Mezzio\Helper\UrlHelperInterface;
use Mezzio\Helper\UrlHelperMiddleware;
use Mezzio\Router\RouteResult;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -16,16 +16,16 @@
/** @covers \Mezzio\Helper\UrlHelperMiddleware */
final class UrlHelperMiddlewareTest extends TestCase
{
/** @var UrlHelper&MockObject */
private UrlHelper $helper;
/** @var UrlHelperInterface&MockObject */
private UrlHelperInterface $helper;

private UrlHelperMiddleware $middleware;

protected function setUp(): void
{
parent::setUp();

$this->helper = $this->createMock(UrlHelper::class);
$this->helper = $this->createMock(UrlHelperInterface::class);

$this->middleware = new UrlHelperMiddleware($this->helper);
}
Expand Down

0 comments on commit b2489bb

Please sign in to comment.