Skip to content

Commit 2fa92d1

Browse files
author
Andrei Onita
committed
added tests for events and plugins
1 parent afe4018 commit 2fa92d1

File tree

5 files changed

+290
-2
lines changed

5 files changed

+290
-2
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Event;
6+
7+
use Dot\Controller\Event\ControllerEvent;
8+
use Dot\Controller\Event\ControllerEventListenerTrait as Subject;
9+
use Laminas\EventManager\EventManagerInterface;
10+
use PHPUnit\Framework\TestCase;
11+
12+
use function is_callable;
13+
14+
class ControllerEventListenerTraitTest extends TestCase
15+
{
16+
private object $traitObject;
17+
18+
protected function setUp(): void
19+
{
20+
$eventManager = $this->createMock(EventManagerInterface::class);
21+
22+
$this->traitObject = new class ($eventManager) {
23+
use Subject;
24+
25+
private EventManagerInterface $eventManager;
26+
27+
public function __construct(EventManagerInterface $eventManager)
28+
{
29+
$this->eventManager = $eventManager;
30+
}
31+
32+
public function getEventManager(): EventManagerInterface
33+
{
34+
return $this->eventManager;
35+
}
36+
};
37+
}
38+
39+
public function testAttach()
40+
{
41+
$eventManager = $this->traitObject->getEventManager();
42+
43+
$eventManager->expects($this->exactly(2))
44+
->method('attach')
45+
->willReturnMap([
46+
[
47+
ControllerEvent::EVENT_CONTROLLER_BEFORE_DISPATCH,
48+
$this->callback(function ($callback) {
49+
return is_callable($callback)
50+
&& $callback[0] === $this->traitObject
51+
&& $callback[1] === 'onBeforeDispatch';
52+
}),
53+
1,
54+
],
55+
[
56+
ControllerEvent::EVENT_CONTROLLER_AFTER_DISPATCH,
57+
$this->callback(function ($callback) {
58+
return is_callable($callback)
59+
&& $callback[0] === $this->traitObject
60+
&& $callback[1] === 'onAfterDispatch';
61+
}),
62+
1,
63+
],
64+
]);
65+
66+
$this->traitObject->attach($eventManager);
67+
}
68+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Event;
6+
7+
use Dot\Controller\AbstractController;
8+
use Dot\Controller\Event\ControllerEvent;
9+
use Dot\Controller\Event\DispatchControllerEventsTrait as Subject;
10+
use Dot\Controller\Exception\RuntimeException;
11+
use Laminas\Diactoros\Response;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\ResponseInterface;
14+
use stdClass;
15+
16+
class DispatchControllerEventsTraitTest extends TestCase
17+
{
18+
private AbstractController $subject;
19+
20+
protected function setUp(): void
21+
{
22+
$this->subject = new class extends AbstractController {
23+
use Subject;
24+
25+
public function dispatch(): ResponseInterface
26+
{
27+
return new Response();
28+
}
29+
};
30+
}
31+
32+
public function testInvalidControllerDispatch(): void
33+
{
34+
$eventName = 'test.event.name';
35+
36+
$data = ['key' => 'value'];
37+
38+
$invalidController = new class {
39+
use Subject;
40+
};
41+
42+
$this->expectException(RuntimeException::class);
43+
$this->expectExceptionMessage('Only controllers can dispatch controller events');
44+
45+
$invalidController->dispatchEvent($eventName, ['data' => $data]);
46+
}
47+
48+
public function testDispatchWithDefaultTarget(): void
49+
{
50+
$eventName = 'test.event.name';
51+
52+
$data = ['key' => 'value'];
53+
54+
$result = $this->subject->dispatchEvent($eventName, ['data' => $data]);
55+
56+
$this->assertInstanceOf(ControllerEvent::class, $result);
57+
58+
$this->assertSame($eventName, $result->getName());
59+
$this->assertSame($this->subject, $result->getTarget());
60+
$this->assertSame($data, $result->getParam('data'));
61+
}
62+
63+
public function testDispatchEventWithExplicitTarget(): void
64+
{
65+
$eventName = 'test.event.name';
66+
67+
$data = ['key' => 'value'];
68+
69+
$targetObject = new stdClass();
70+
71+
$result = $this->subject->dispatchEvent($eventName, ['data' => $data], $targetObject);
72+
73+
$this->assertInstanceOf(ControllerEvent::class, $result);
74+
75+
$this->assertSame($eventName, $result->getName());
76+
$this->assertSame($targetObject, $result->getTarget());
77+
$this->assertSame($data, $result->getParam('data'));
78+
}
79+
}

test/Factory/PluginManagerAwareInitializerTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use Dot\Controller\Factory\PluginManagerAwareInitializer as Subject;
99
use Dot\Controller\Plugin\PluginManager;
1010
use Dot\Controller\Plugin\PluginManagerAwareInterface;
11+
use Laminas\Diactoros\Response;
1112
use PHPUnit\Framework\TestCase;
1213
use Psr\Container\ContainerInterface;
14+
use Psr\Http\Message\ResponseInterface;
1315

1416
class PluginManagerAwareInitializerTest extends TestCase
1517
{
@@ -47,8 +49,12 @@ public function testInvokeSetsDebugFlagWhenInstanceIsAbstractController(): void
4749
['config', ['debug' => true]],
4850
]);
4951

50-
$instance = $this->getMockBuilder(AbstractController::class)
51-
->getMockForAbstractClass();
52+
$instance = new class extends AbstractController {
53+
public function dispatch(): ResponseInterface
54+
{
55+
return new Response();
56+
}
57+
};
5258

5359
$this->subject->__invoke($this->container, $instance);
5460

test/Plugin/TemplatePluginTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Plugin;
6+
7+
use Dot\Controller\Plugin\TemplatePlugin as Subject;
8+
use Mezzio\Template\TemplateRendererInterface;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class TemplatePluginTest extends TestCase
12+
{
13+
protected TemplateRendererInterface $templateRenderer;
14+
protected Subject $subject;
15+
16+
protected function setup(): void
17+
{
18+
$this->templateRenderer = $this->createMock(TemplateRendererInterface::class);
19+
$this->subject = new Subject($this->templateRenderer);
20+
}
21+
22+
public function testInvokeWithoutArguments(): void
23+
{
24+
$result = $this->subject->__invoke();
25+
26+
$this->assertInstanceOf(Subject::class, $result);
27+
}
28+
29+
public function testInvokeWithArguments(): void
30+
{
31+
$templateName = 'test-template';
32+
$params = ['param1' => 'value1', 'param2' => 'value2'];
33+
34+
$this->templateRenderer->expects($this->once())
35+
->method('render')
36+
->with($templateName, $params)
37+
->willReturn('<html><body>Test content</body></html>');
38+
39+
$result = $this->subject->__invoke($templateName, $params);
40+
41+
$this->assertSame('<html><body>Test content</body></html>', $result);
42+
}
43+
44+
public function testRender(): void
45+
{
46+
$templateName = 'test-template';
47+
$params = ['param1' => 'value1', 'param2' => 'value2'];
48+
49+
$this->templateRenderer->expects($this->once())
50+
->method('render')
51+
->with($templateName, $params)
52+
->willReturn('<html><body>Test content</body></html>');
53+
54+
$result = $this->subject->render($templateName, $params);
55+
56+
$this->assertSame('<html><body>Test content</body></html>', $result);
57+
}
58+
59+
public function testAddDefaultParam(): void
60+
{
61+
$templateName = 'test-template';
62+
$param = 'defaultParam';
63+
$value = 'defaultValue';
64+
65+
$this->templateRenderer->expects($this->once())
66+
->method('addDefaultParam')
67+
->with($templateName, $param, $value);
68+
69+
$this->subject->addDefaultParam($templateName, $param, $value);
70+
}
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Plugin;
6+
7+
use Dot\Controller\Plugin\UrlHelperPlugin as Subject;
8+
use Mezzio\Helper\UrlHelper;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class UrlHelperPluginTest extends TestCase
12+
{
13+
private UrlHelper $urlHelper;
14+
private Subject $subject;
15+
16+
public function setUp(): void
17+
{
18+
$this->urlHelper = $this->createMock(UrlHelper::class);
19+
$this->subject = new Subject($this->urlHelper);
20+
}
21+
22+
public function testInvokeWithoutArguments(): void
23+
{
24+
$result = $this->subject->__invoke();
25+
26+
$this->assertInstanceOf(Subject::class, $result);
27+
}
28+
29+
public function testInvokeWithArguments(): void
30+
{
31+
$routeName = 'test.route';
32+
$routeParams = ['id' => 123];
33+
$queryParams = ['sort' => 'asc'];
34+
$fragmentIdentifier = 'section';
35+
$options = ['absolute' => true];
36+
37+
$this->urlHelper->expects($this->once())
38+
->method('generate')
39+
->with($routeName, $routeParams, $queryParams, $fragmentIdentifier, $options)
40+
->willReturn('http://example.com/test-route?id=123&sort=asc#section');
41+
42+
$result = $this->subject->__invoke($routeName, $routeParams, $queryParams, $fragmentIdentifier, $options);
43+
44+
$this->assertSame('http://example.com/test-route?id=123&sort=asc#section', $result);
45+
}
46+
47+
public function testGenerate(): void
48+
{
49+
$routeName = 'test.route';
50+
$routeParams = ['id' => 123];
51+
$queryParams = ['sort' => 'asc'];
52+
$fragmentIdentifier = 'section';
53+
$options = ['absolute' => true];
54+
55+
$this->urlHelper->expects($this->once())
56+
->method('generate')
57+
->with($routeName, $routeParams, $queryParams, $fragmentIdentifier, $options)
58+
->willReturn('http://example.com/test-route?id=123&sort=asc#section');
59+
60+
$result = $this->subject->generate($routeName, $routeParams, $queryParams, $fragmentIdentifier, $options);
61+
62+
$this->assertSame('http://example.com/test-route?id=123&sort=asc#section', $result);
63+
}
64+
}

0 commit comments

Comments
 (0)