Skip to content

Commit afe4018

Browse files
author
Andrei Onita
committed
added factories tests
1 parent 60bf396 commit afe4018

3 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Factory;
6+
7+
use Dot\Controller\AbstractController;
8+
use Dot\Controller\Event\ControllerEventListenerInterface;
9+
use Dot\Controller\Factory\ControllerEventListenersInitializer as Subject;
10+
use Laminas\EventManager\EventManager;
11+
use PHPUnit\Framework\TestCase;
12+
use Psr\Container\ContainerInterface;
13+
14+
class ControllerEventListenersInitializerTest extends TestCase
15+
{
16+
private ContainerInterface $container;
17+
private AbstractController $controller;
18+
private EventManager $eventManager;
19+
private Subject $subject;
20+
21+
public function setUp(): void
22+
{
23+
$this->container = $this->createMock(ContainerInterface::class);
24+
$this->controller = $this->createMock(AbstractController::class);
25+
$this->eventManager = $this->createMock(EventManager::class);
26+
$this->subject = new Subject();
27+
}
28+
29+
public function testAttachControllerListeners(): void
30+
{
31+
$config = [
32+
'dot_controller' => [
33+
'event_listeners' => [
34+
AbstractController::class => [
35+
[
36+
'type' => 'ListenerClass1',
37+
'priority' => 10,
38+
],
39+
'ListenerClass2',
40+
],
41+
],
42+
],
43+
];
44+
$this->container->method('get')->with('config')->willReturn($config);
45+
46+
$this->container->method('has')->willReturnMap([
47+
['ListenerClass1', true],
48+
['ListenerClass2', true],
49+
]);
50+
51+
$this->container->method('get')->willReturnMap([
52+
['ListenerClass1', $this->createMock(ControllerEventListenerInterface::class)],
53+
['ListenerClass2', $this->createMock(ControllerEventListenerInterface::class)],
54+
]);
55+
56+
$this->controller->method('getEventManager')->willReturn($this->eventManager);
57+
58+
$this->eventManager->expects($this->any())->method('attach')
59+
->with($this->isInstanceOf(ControllerEventListenerInterface::class), 10);
60+
$this->eventManager->expects($this->any())->method('attach')
61+
->with($this->isInstanceOf(ControllerEventListenerInterface::class), 1);
62+
63+
$this->subject->attachControllerListeners($this->container, $this->controller);
64+
65+
$this->assertTrue(true);
66+
}
67+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Factory;
6+
7+
use Dot\Controller\AbstractController;
8+
use Dot\Controller\Factory\PluginManagerAwareInitializer as Subject;
9+
use Dot\Controller\Plugin\PluginManager;
10+
use Dot\Controller\Plugin\PluginManagerAwareInterface;
11+
use PHPUnit\Framework\TestCase;
12+
use Psr\Container\ContainerInterface;
13+
14+
class PluginManagerAwareInitializerTest extends TestCase
15+
{
16+
private Subject $subject;
17+
private ContainerInterface $container;
18+
private PluginManager $pluginManager;
19+
public function setUp(): void
20+
{
21+
$this->subject = new Subject();
22+
$this->container = $this->createMock(ContainerInterface::class);
23+
$this->pluginManager = $this->createMock(PluginManager::class);
24+
}
25+
26+
public function testInvokeSetsPluginManagerWhenInstanceIsPluginManagerAwareInterface(): void
27+
{
28+
$this->container->expects($this->once())
29+
->method('get')
30+
->with(PluginManager::class)
31+
->willReturn($this->pluginManager);
32+
33+
$instance = $this->createMock(PluginManagerAwareInterface::class);
34+
35+
$instance->expects($this->once())
36+
->method('setPluginManager')
37+
->with($this->equalTo($this->pluginManager));
38+
39+
$this->subject->__invoke($this->container, $instance);
40+
}
41+
42+
public function testInvokeSetsDebugFlagWhenInstanceIsAbstractController(): void
43+
{
44+
$this->container->method('get')
45+
->willReturnMap([
46+
[PluginManager::class, $this->pluginManager],
47+
['config', ['debug' => true]],
48+
]);
49+
50+
$instance = $this->getMockBuilder(AbstractController::class)
51+
->getMockForAbstractClass();
52+
53+
$this->subject->__invoke($this->container, $instance);
54+
55+
$this->assertTrue($instance->isDebug());
56+
}
57+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Factory;
6+
7+
use Dot\Controller\Factory\PluginManagerFactory as Subject;
8+
use Dot\Controller\Plugin\PluginManager;
9+
use Dot\Controller\Plugin\TemplatePlugin;
10+
use Dot\Controller\Plugin\UrlHelperPlugin;
11+
use Mezzio\Helper\UrlHelper;
12+
use Mezzio\Template\TemplateRendererInterface;
13+
use PHPUnit\Framework\TestCase;
14+
use Psr\Container\ContainerInterface;
15+
16+
class PluginManagerFactoryTest extends TestCase
17+
{
18+
private Subject $subject;
19+
private ContainerInterface $container;
20+
private TemplateRendererInterface $templateRenderer;
21+
private UrlHelper $urlHelper;
22+
public function setUp(): void
23+
{
24+
$this->subject = new Subject();
25+
$this->container = $this->createMock(ContainerInterface::class);
26+
$this->pluginManager = $this->createMock(PluginManager::class);
27+
$this->templateRenderer = $this->createMock(TemplateRendererInterface::class);
28+
$this->urlHelper = $this->createMock(UrlHelper::class);
29+
}
30+
31+
public function testInvokeWithUrlHelper(): void
32+
{
33+
$config = [
34+
'dot_controller' => [
35+
'plugin_manager' => [],
36+
],
37+
];
38+
39+
$this->container->method('has')->willReturnMap([
40+
[UrlHelper::class, true],
41+
[TemplateRendererInterface::class, false],
42+
]);
43+
44+
$this->container->method('get')->willReturnMap([
45+
[TemplateRendererInterface::class, $this->templateRenderer],
46+
[UrlHelper::class, $this->urlHelper],
47+
['config', $config],
48+
]);
49+
50+
$pluginManager = $this->subject->__invoke($this->container);
51+
52+
$this->assertInstanceOf(PluginManager::class, $pluginManager);
53+
$this->assertTrue($pluginManager->has('url'));
54+
$this->assertFalse($pluginManager->has('template'));
55+
$this->assertInstanceOf(UrlHelperPlugin::class, $pluginManager->get('url'));
56+
}
57+
58+
public function testInvokeWithTemplateRenderer(): void
59+
{
60+
$config = [
61+
'dot_controller' => [
62+
'plugin_manager' => [],
63+
],
64+
];
65+
66+
$this->container->method('has')->willReturnMap([
67+
[UrlHelper::class, false],
68+
[TemplateRendererInterface::class, true],
69+
]);
70+
71+
$this->container->method('get')->willReturnMap([
72+
[TemplateRendererInterface::class, $this->templateRenderer],
73+
[UrlHelper::class, $this->urlHelper],
74+
['config', $config],
75+
]);
76+
77+
$pluginManager = $this->subject->__invoke($this->container);
78+
79+
$this->assertInstanceOf(PluginManager::class, $pluginManager);
80+
$this->assertTrue($pluginManager->has('template'));
81+
$this->assertFalse($pluginManager->has('url'));
82+
$this->assertInstanceOf(TemplatePlugin::class, $pluginManager->get('template'));
83+
}
84+
}

0 commit comments

Comments
 (0)