|
6 | 6 |
|
7 | 7 | use App\Handler\HomePageHandler;
|
8 | 8 | use App\Handler\HomePageHandlerFactory;
|
| 9 | +use AppTest\InMemoryContainer; |
9 | 10 | use Mezzio\Router\RouterInterface;
|
10 | 11 | use Mezzio\Template\TemplateRendererInterface;
|
11 |
| -use PHPUnit\Framework\MockObject\MockObject; |
12 | 12 | use PHPUnit\Framework\TestCase;
|
13 |
| -use Psr\Container\ContainerInterface; |
14 |
| - |
15 |
| -use function in_array; |
16 | 13 |
|
17 | 14 | class HomePageHandlerFactoryTest extends TestCase
|
18 | 15 | {
|
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 |
| - |
31 | 16 | public function testFactoryWithoutTemplate(): void
|
32 | 17 | {
|
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)); |
43 | 20 |
|
44 | 21 | $factory = new HomePageHandlerFactory();
|
45 |
| - $homePage = $factory($this->container); |
| 22 | + $homePage = $factory($container); |
46 | 23 |
|
47 | 24 | self::assertInstanceOf(HomePageHandler::class, $homePage);
|
48 | 25 | }
|
49 | 26 |
|
50 | 27 | public function testFactoryWithTemplate(): void
|
51 | 28 | {
|
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)); |
73 | 32 |
|
74 | 33 | $factory = new HomePageHandlerFactory();
|
75 |
| - $homePage = $factory($this->container); |
| 34 | + $homePage = $factory($container); |
76 | 35 |
|
77 | 36 | self::assertInstanceOf(HomePageHandler::class, $homePage);
|
78 | 37 | }
|
|
0 commit comments