Skip to content

Commit d73ad7b

Browse files
committed
Simplify Factory test by utilising a PSR container stub
Signed-off-by: George Steel <george@net-glue.co.uk>
1 parent 5c4b7b4 commit d73ad7b

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

test/AppTest/Handler/HomePageHandlerFactoryTest.php

+8-49
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,32 @@
66

77
use App\Handler\HomePageHandler;
88
use App\Handler\HomePageHandlerFactory;
9+
use AppTest\InMemoryContainer;
910
use Mezzio\Router\RouterInterface;
1011
use Mezzio\Template\TemplateRendererInterface;
11-
use PHPUnit\Framework\MockObject\MockObject;
1212
use PHPUnit\Framework\TestCase;
13-
use Psr\Container\ContainerInterface;
14-
15-
use function in_array;
1613

1714
class HomePageHandlerFactoryTest extends TestCase
1815
{
19-
/** @var ContainerInterface&MockObject */
20-
protected $container;
21-
22-
/** @var RouterInterface&MockObject */
23-
protected $router;
24-
25-
protected function setUp(): void
26-
{
27-
$this->container = $this->createMock(ContainerInterface::class);
28-
$this->router = $this->createMock(RouterInterface::class);
29-
}
30-
3116
public function testFactoryWithoutTemplate(): void
3217
{
33-
$this->container
34-
->expects($this->once())
35-
->method('has')
36-
->with(TemplateRendererInterface::class)
37-
->willReturn(false);
38-
$this->container
39-
->expects($this->once())
40-
->method('get')
41-
->with(RouterInterface::class)
42-
->willReturn($this->router);
18+
$container = new InMemoryContainer();
19+
$container->setService(RouterInterface::class, $this->createMock(RouterInterface::class));
4320

4421
$factory = new HomePageHandlerFactory();
45-
$homePage = $factory($this->container);
22+
$homePage = $factory($container);
4623

4724
self::assertInstanceOf(HomePageHandler::class, $homePage);
4825
}
4926

5027
public function testFactoryWithTemplate(): void
5128
{
52-
$renderer = $this->createMock(TemplateRendererInterface::class);
53-
$this->container
54-
->expects($this->once())
55-
->method('has')
56-
->with(TemplateRendererInterface::class)
57-
->willReturn(true);
58-
$this->container
59-
->expects($this->exactly(2))
60-
->method('get')
61-
->with(self::callback(static function (string $name): bool {
62-
self::assertTrue(in_array($name, [
63-
RouterInterface::class,
64-
TemplateRendererInterface::class,
65-
]));
66-
67-
return true;
68-
}))
69-
->willReturnOnConsecutiveCalls(
70-
$this->router,
71-
$renderer
72-
);
29+
$container = new InMemoryContainer();
30+
$container->setService(RouterInterface::class, $this->createMock(RouterInterface::class));
31+
$container->setService(TemplateRendererInterface::class, $this->createMock(TemplateRendererInterface::class));
7332

7433
$factory = new HomePageHandlerFactory();
75-
$homePage = $factory($this->container);
34+
$homePage = $factory($container);
7635

7736
self::assertInstanceOf(HomePageHandler::class, $homePage);
7837
}

test/AppTest/InMemoryContainer.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppTest;
6+
7+
use Psr\Container\ContainerInterface;
8+
use RuntimeException;
9+
10+
use function array_key_exists;
11+
use function sprintf;
12+
13+
/**
14+
* A PSR Container stub. Useful for testing factories without excessive mocking
15+
*/
16+
class InMemoryContainer implements ContainerInterface
17+
{
18+
/** @var array<string, mixed> */
19+
public array $services = [];
20+
21+
public function setService(string $name, mixed $service): void
22+
{
23+
$this->services[$name] = $service;
24+
}
25+
26+
/**
27+
* @param string $id
28+
* @return mixed
29+
*/
30+
public function get($id)
31+
{
32+
if (! $this->has($id)) {
33+
throw new RuntimeException(sprintf('Service not found "%s"', $id));
34+
}
35+
36+
return $this->services[$id];
37+
}
38+
39+
/**
40+
* @param string $id
41+
* @return bool
42+
*/
43+
public function has($id)
44+
{
45+
return array_key_exists($id, $this->services);
46+
}
47+
}

0 commit comments

Comments
 (0)