66
77use Dot \Controller \AbstractController as Subject ;
88use Dot \Controller \Exception \RuntimeException ;
9+ use Dot \Controller \Plugin \PluginInterface ;
910use Dot \Controller \Plugin \PluginManager ;
1011use Laminas \Diactoros \Response ;
1112use PHPUnit \Framework \MockObject \Exception ;
13+ use PHPUnit \Framework \MockObject \MockObject ;
1214use PHPUnit \Framework \TestCase ;
1315use Psr \Http \Message \ResponseInterface ;
16+ use Psr \Http \Message \ServerRequestInterface ;
17+ use Psr \Http \Server \RequestHandlerInterface ;
1418
1519class 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}
0 commit comments