Skip to content

Commit c8952d7

Browse files
author
Andrei Onita
committed
new tests added
1 parent 2fa92d1 commit c8952d7

10 files changed

+259
-25
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,3 @@ Although these are optional, if a package is missing, the controller will not ha
9090

9191
- `template` wraps TemplateInterface provided by Mezzio, to make template engine accessible to any controller
9292
- `url` wraps the UrlHelper class provided by Laminas helpers package. Used to generate URIs from routes
93-
94-
95-
## Controller Events
96-
97-
// @TODO
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller;
6+
7+
use Dot\Controller\AbstractActionController;
8+
use Laminas\Diactoros\Response;
9+
use PHPUnit\Framework\MockObject\Exception;
10+
use PHPUnit\Framework\TestCase;
11+
use Psr\Http\Message\ResponseInterface;
12+
use Psr\Http\Message\ServerRequestInterface;
13+
use Psr\Http\Server\RequestHandlerInterface;
14+
15+
class AbstractActionControllerTest extends TestCase
16+
{
17+
/**
18+
* @throws Exception
19+
*/
20+
public function testNonExistingActionWillReturnResponse(): void
21+
{
22+
$request = $this->createMock(ServerRequestInterface::class);
23+
$handler = $this->createMock(RequestHandlerInterface::class);
24+
25+
$request->expects($this->any())->method('getAttribute')->withAnyParameters()->willReturn('test');
26+
27+
$subject = new class extends AbstractActionController {
28+
};
29+
30+
$response = $subject->process($request, $handler);
31+
$this->assertInstanceOf(ResponseInterface::class, $response);
32+
}
33+
34+
/**
35+
* @throws Exception
36+
*/
37+
public function testExistingActionWillReturnResponse(): void
38+
{
39+
$request = $this->createMock(ServerRequestInterface::class);
40+
$handler = $this->createMock(RequestHandlerInterface::class);
41+
42+
$request->expects($this->any())->method('getAttribute')->withAnyParameters()->willReturn('index');
43+
44+
$subject = new class extends AbstractActionController {
45+
public function indexAction(): ResponseInterface
46+
{
47+
return new Response();
48+
}
49+
};
50+
51+
$response = $subject->process($request, $handler);
52+
$this->assertInstanceOf(ResponseInterface::class, $response);
53+
}
54+
}

test/AbstractControllerTest.php

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66

77
use Dot\Controller\AbstractController as Subject;
88
use Dot\Controller\Exception\RuntimeException;
9+
use Dot\Controller\Plugin\PluginInterface;
910
use Dot\Controller\Plugin\PluginManager;
1011
use Laminas\Diactoros\Response;
1112
use PHPUnit\Framework\MockObject\Exception;
13+
use PHPUnit\Framework\MockObject\MockObject;
1214
use PHPUnit\Framework\TestCase;
1315
use Psr\Http\Message\ResponseInterface;
16+
use Psr\Http\Message\ServerRequestInterface;
17+
use Psr\Http\Server\RequestHandlerInterface;
1418

1519
class AbstractControllerTest extends TestCase
1620
{
17-
private PluginManager $pluginManager;
21+
private PluginManager|MockObject $pluginManager;
22+
private PluginInterface|MockObject $plugin;
23+
private ServerRequestInterface|MockObject $request;
24+
private RequestHandlerInterface|MockObject $handler;
1825
private Subject $subject;
1926

2027
/**
@@ -23,6 +30,9 @@ class AbstractControllerTest extends TestCase
2330
public function setUp(): void
2431
{
2532
$this->pluginManager = $this->createMock(PluginManager::class);
33+
$this->plugin = $this->createMock(PluginInterface::class);
34+
$this->request = $this->createMock(ServerRequestInterface::class);
35+
$this->handler = $this->createMock(RequestHandlerInterface::class);
2636
$this->subject = new class extends Subject {
2737
public function dispatch(): ResponseInterface
2838
{
@@ -31,22 +41,94 @@ public function dispatch(): ResponseInterface
3141
};
3242
}
3343

34-
public function testGetMethodFromAction()
44+
public function testGetMethodFromAction(): void
3545
{
3646
$testAction = 'test.this-action_name';
3747
$method = Subject::getMethodFromAction($testAction);
3848
$this->assertSame('testThisActionNameAction', $method);
3949
}
4050

41-
public function testGetPluginManager()
51+
public function testGetPluginManager(): void
4252
{
4353
$this->subject->setPluginManager($this->pluginManager);
4454
$this->assertInstanceOf(PluginManager::class, $this->subject->getPluginManager());
4555
}
4656

47-
public function testNoPluginManager()
57+
public function testNoPluginManager(): void
4858
{
4959
$this->expectException(RuntimeException::class);
5060
$this->subject->getPluginManager();
5161
}
62+
63+
public function testGetRequest(): void
64+
{
65+
$this->subject->process($this->request, $this->handler);
66+
$this->assertSame($this->request, $this->subject->getRequest());
67+
}
68+
69+
public function testGetHandler(): void
70+
{
71+
$this->subject->process($this->request, $this->handler);
72+
$this->assertSame($this->handler, $this->subject->getHandler());
73+
}
74+
75+
public function testDebug(): void
76+
{
77+
$this->assertFalse($this->subject->isDebug());
78+
79+
$this->subject->setDebug(true);
80+
$this->assertTrue($this->subject->isDebug());
81+
}
82+
83+
public function testCallPlugin(): void
84+
{
85+
$this->pluginManager->expects($this->once())
86+
->method('get')
87+
->with('somePlugin')
88+
->willReturn($this->plugin);
89+
90+
$controller = new class ($this->pluginManager) extends Subject {
91+
public function __construct(PluginManager $pluginManager)
92+
{
93+
$this->pluginManager = $pluginManager;
94+
}
95+
96+
public function dispatch(): ResponseInterface
97+
{
98+
return new Response();
99+
}
100+
};
101+
102+
$plugin = $controller->somePlugin();
103+
104+
$this->assertInstanceOf(PluginInterface::class, $plugin);
105+
}
106+
107+
public function testCallCallablePlugin(): void
108+
{
109+
$mockCallablePlugin = function ($param) {
110+
return "Mock plugin called with parameter: $param";
111+
};
112+
113+
$this->pluginManager->expects($this->once())
114+
->method('get')
115+
->with('callablePlugin')
116+
->willReturn($mockCallablePlugin);
117+
118+
$this->subject = new class ($this->pluginManager) extends Subject {
119+
public function __construct(PluginManager $pluginManager)
120+
{
121+
$this->pluginManager = $pluginManager;
122+
}
123+
124+
public function dispatch(): ResponseInterface
125+
{
126+
return new Response();
127+
}
128+
};
129+
130+
$result = $this->subject->callablePlugin('paramValue');
131+
132+
$this->assertSame('Mock plugin called with parameter: paramValue', $result);
133+
}
52134
}

test/Event/ControllerEventListenerTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getEventManager(): EventManagerInterface
3636
};
3737
}
3838

39-
public function testAttach()
39+
public function testAttach(): void
4040
{
4141
$eventManager = $this->traitObject->getEventManager();
4242

test/Factory/ControllerEventListenersInitializerTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@
88
use Dot\Controller\Event\ControllerEventListenerInterface;
99
use Dot\Controller\Factory\ControllerEventListenersInitializer as Subject;
1010
use Laminas\EventManager\EventManager;
11+
use PHPUnit\Framework\MockObject\Exception;
12+
use PHPUnit\Framework\MockObject\MockObject;
1113
use PHPUnit\Framework\TestCase;
14+
use Psr\Container\ContainerExceptionInterface;
1215
use Psr\Container\ContainerInterface;
16+
use Psr\Container\NotFoundExceptionInterface;
1317

1418
class ControllerEventListenersInitializerTest extends TestCase
1519
{
16-
private ContainerInterface $container;
17-
private AbstractController $controller;
18-
private EventManager $eventManager;
20+
private ContainerInterface|MockObject $container;
21+
private AbstractController|MockObject $controller;
22+
private EventManager|MockObject $eventManager;
1923
private Subject $subject;
2024

25+
/**
26+
* @throws Exception
27+
*/
2128
public function setUp(): void
2229
{
2330
$this->container = $this->createMock(ContainerInterface::class);
@@ -26,6 +33,11 @@ public function setUp(): void
2633
$this->subject = new Subject();
2734
}
2835

36+
/**
37+
* @throws ContainerExceptionInterface
38+
* @throws NotFoundExceptionInterface
39+
* @throws Exception
40+
*/
2941
public function testAttachControllerListeners(): void
3042
{
3143
$config = [

test/Factory/PluginManagerAwareInitializerTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,35 @@
99
use Dot\Controller\Plugin\PluginManager;
1010
use Dot\Controller\Plugin\PluginManagerAwareInterface;
1111
use Laminas\Diactoros\Response;
12+
use PHPUnit\Framework\MockObject\Exception;
13+
use PHPUnit\Framework\MockObject\MockObject;
1214
use PHPUnit\Framework\TestCase;
15+
use Psr\Container\ContainerExceptionInterface;
1316
use Psr\Container\ContainerInterface;
17+
use Psr\Container\NotFoundExceptionInterface;
1418
use Psr\Http\Message\ResponseInterface;
1519

1620
class PluginManagerAwareInitializerTest extends TestCase
1721
{
1822
private Subject $subject;
19-
private ContainerInterface $container;
20-
private PluginManager $pluginManager;
23+
private ContainerInterface|MockObject $container;
24+
private PluginManager|MockObject $pluginManager;
25+
26+
/**
27+
* @throws Exception
28+
*/
2129
public function setUp(): void
2230
{
2331
$this->subject = new Subject();
2432
$this->container = $this->createMock(ContainerInterface::class);
2533
$this->pluginManager = $this->createMock(PluginManager::class);
2634
}
2735

36+
/**
37+
* @throws ContainerExceptionInterface
38+
* @throws NotFoundExceptionInterface
39+
* @throws Exception
40+
*/
2841
public function testInvokeSetsPluginManagerWhenInstanceIsPluginManagerAwareInterface(): void
2942
{
3043
$this->container->expects($this->once())
@@ -41,6 +54,10 @@ public function testInvokeSetsPluginManagerWhenInstanceIsPluginManagerAwareInter
4154
$this->subject->__invoke($this->container, $instance);
4255
}
4356

57+
/**
58+
* @throws ContainerExceptionInterface
59+
* @throws NotFoundExceptionInterface
60+
*/
4461
public function testInvokeSetsDebugFlagWhenInstanceIsAbstractController(): void
4562
{
4663
$this->container->method('get')

test/Factory/PluginManagerFactoryTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,35 @@
1010
use Dot\Controller\Plugin\UrlHelperPlugin;
1111
use Mezzio\Helper\UrlHelper;
1212
use Mezzio\Template\TemplateRendererInterface;
13+
use PHPUnit\Framework\MockObject\Exception;
14+
use PHPUnit\Framework\MockObject\MockObject;
1315
use PHPUnit\Framework\TestCase;
16+
use Psr\Container\ContainerExceptionInterface;
1417
use Psr\Container\ContainerInterface;
18+
use Psr\Container\NotFoundExceptionInterface;
1519

1620
class PluginManagerFactoryTest extends TestCase
1721
{
1822
private Subject $subject;
19-
private ContainerInterface $container;
20-
private TemplateRendererInterface $templateRenderer;
21-
private UrlHelper $urlHelper;
23+
private ContainerInterface|MockObject $container;
24+
private TemplateRendererInterface|MockObject $templateRenderer;
25+
private UrlHelper|MockObject $urlHelper;
26+
27+
/**
28+
* @throws Exception
29+
*/
2230
public function setUp(): void
2331
{
2432
$this->subject = new Subject();
2533
$this->container = $this->createMock(ContainerInterface::class);
26-
$this->pluginManager = $this->createMock(PluginManager::class);
2734
$this->templateRenderer = $this->createMock(TemplateRendererInterface::class);
2835
$this->urlHelper = $this->createMock(UrlHelper::class);
2936
}
3037

38+
/**
39+
* @throws ContainerExceptionInterface
40+
* @throws NotFoundExceptionInterface
41+
*/
3142
public function testInvokeWithUrlHelper(): void
3243
{
3344
$config = [
@@ -55,6 +66,10 @@ public function testInvokeWithUrlHelper(): void
5566
$this->assertInstanceOf(UrlHelperPlugin::class, $pluginManager->get('url'));
5667
}
5768

69+
/**
70+
* @throws ContainerExceptionInterface
71+
* @throws NotFoundExceptionInterface
72+
*/
5873
public function testInvokeWithTemplateRenderer(): void
5974
{
6075
$config = [

test/Plugin/PluginManagerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DotTest\Controller\Plugin;
6+
7+
use Dot\Controller\Plugin\PluginInterface;
8+
use Dot\Controller\Plugin\PluginManager as Subject;
9+
use Laminas\ServiceManager\Exception\InvalidServiceException;
10+
use PHPUnit\Framework\MockObject\Exception;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Container\ContainerExceptionInterface;
14+
use Psr\Container\ContainerInterface;
15+
16+
class PluginManagerTest extends TestCase
17+
{
18+
protected ContainerInterface|MockObject $container;
19+
protected Subject $subject;
20+
21+
/**
22+
* @throws Exception
23+
*/
24+
protected function setup(): void
25+
{
26+
$this->container = $this->createMock(ContainerInterface::class);
27+
$this->subject = new Subject($this->container);
28+
}
29+
30+
/**
31+
* @throws ContainerExceptionInterface
32+
* @throws Exception
33+
*/
34+
public function testInstanceOf(): void
35+
{
36+
$plugin = $this->createMock(PluginInterface::class);
37+
$this->expectNotToPerformAssertions();
38+
$this->subject->validate($plugin);
39+
}
40+
41+
/**
42+
* @throws ContainerExceptionInterface
43+
*/
44+
public function testInvalidInstanceOf(): void
45+
{
46+
$this->expectException(InvalidServiceException::class);
47+
$this->subject->validate('invalidPlugin');
48+
}
49+
}

0 commit comments

Comments
 (0)