Skip to content

Commit 0047a23

Browse files
author
Maxofil
committed
Added test + namespace fix
1 parent e2a4770 commit 0047a23

File tree

8 files changed

+470
-2
lines changed

8 files changed

+470
-2
lines changed

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@
1616
"nette/caching": "^3.0",
1717
"nette/utils": "^3.0"
1818
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^9.1",
21+
"infection/infection": "^0.16",
22+
"mockery/mockery": "^1.2"
23+
},
24+
"scripts": {
25+
"phpunit": "phpunit tests",
26+
"infection": "infection"
27+
},
1928
"autoload": {
2029
"psr-4": {
2130
"Infinityloop\\": "src/"
2231
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Infinityloop\\Tests\\": "tests/"
36+
}
2337
}
2438
}

src/ObserverComponent/EventMapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
final class EventMapper
88
{
9-
use Nette\SmartObject;
9+
use \Nette\SmartObject;
1010

1111
private \Nette\Application\Application $application;
1212
private \Nette\Caching\IStorage $storage;
@@ -55,7 +55,6 @@ public function dispatchEvent(IEvent $event) : void
5555
{
5656
$presenter = $this->application->getPresenter();
5757
\assert($presenter instanceof \Nette\Application\UI\Control);
58-
5958
foreach ($this->getObserverList(\get_class($event)) as $observerPath) {
6059
\assert(\is_string($observerPath));
6160

tests/ObserverComponent/Bla.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Tests\ObserverComponent;
6+
7+
class Bla implements \Infinityloop\ObserverComponent\IObserverComponent
8+
{
9+
use \Infinityloop\ObserverComponent\TObserverComponent;
10+
11+
public static function getObservedEvents() : array
12+
{
13+
return [
14+
\Infinityloop\Tests\ObserverComponent\BlaEventEdit::class
15+
];
16+
}
17+
18+
public function observableUpdated(\Infinityloop\ObserverComponent\IEvent $event) : void
19+
{
20+
throw new \Exception('Kebab');
21+
}
22+
23+
public function lookupPath(?string $type = null, bool $throw = true) : ?string
24+
{
25+
return BlaPresenter::class;
26+
}
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Tests\ObserverComponent;
6+
7+
final class BlaEventEdit implements \Infinityloop\ObserverComponent\IEvent
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Tests\ObserverComponent;
6+
7+
class BlaPresenter extends \Nette\Application\UI\Presenter
8+
{
9+
}
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Infinityloop\Tests\ObserverComponent;
6+
7+
use Infinityloop\ObserverComponent\EventMapper;
8+
9+
final class EventMapperTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
10+
{
11+
public function testComponentRegisteredReturn() : void
12+
{
13+
$httpResponse2 = \Mockery::mock(\Nette\Application\IResponse::class);
14+
$httpResponse2->expects('send');
15+
16+
$request = new \Nette\Application\Request('BlaPresenter:edit');
17+
18+
$presenter = \Mockery::mock(\Nette\Application\IPresenter::class);
19+
$presenter->expects('getAction')
20+
->once()
21+
->with(true)
22+
->andReturn('edit');
23+
$presenter->expects('run')
24+
->andReturn($httpResponse2);
25+
26+
$presenterFactory = \Mockery::mock(\Nette\Application\IPresenterFactory::class);
27+
$presenterFactory->expects('createPresenter')
28+
->with('BlaPresenter:edit')
29+
->andReturn($presenter);
30+
31+
$router = \Mockery::mock(\Nette\Routing\Router::class);
32+
33+
$httpRequest = \Mockery::mock(\Nette\Http\IRequest::class);
34+
$httpResponse = \Mockery::mock(\Nette\Http\IResponse::class);
35+
36+
$application = new \Nette\Application\Application($presenterFactory, $router, $httpRequest, $httpResponse);
37+
$application->processRequest($request);
38+
39+
$storage = \Mockery::mock(\Nette\Caching\IStorage::class);
40+
$storage->expects('read')
41+
->once()
42+
->withAnyArgs()
43+
->andReturn([0 => BlaPresenter::class]);
44+
45+
$component = \Mockery::mock(Bla::class);
46+
$component->expects('lookupPath')
47+
->once()
48+
->with(\Nette\Application\IPresenter::class)
49+
->andReturn(BlaPresenter::class);
50+
51+
$instance = new \Infinityloop\ObserverComponent\EventMapper($application, $storage, false);
52+
$instance->registerObserver($component);
53+
}
54+
55+
public function testComponentRegisteredContinue() : void
56+
{
57+
$httpResponse2 = \Mockery::mock(\Nette\Application\IResponse::class);
58+
$httpResponse2->expects('send');
59+
60+
$request = new \Nette\Application\Request('BlaPresenter:edit');
61+
62+
$presenter = \Mockery::mock(\Nette\Application\IPresenter::class);
63+
$presenter->expects('getAction')
64+
->once()->with(true)
65+
->andReturn('edit');
66+
$presenter->expects('run')
67+
->andReturn($httpResponse2);
68+
69+
$presenterFactory = \Mockery::mock(\Nette\Application\IPresenterFactory::class);
70+
$presenterFactory->expects('createPresenter')
71+
->with('BlaPresenter:edit')
72+
->andReturn($presenter);
73+
74+
$router = \Mockery::mock(\Nette\Routing\Router::class);
75+
76+
$httpRequest = \Mockery::mock(\Nette\Http\IRequest::class);
77+
$httpResponse = \Mockery::mock(\Nette\Http\IResponse::class);
78+
79+
$application = new \Nette\Application\Application($presenterFactory, $router, $httpRequest, $httpResponse);
80+
$application->processRequest($request);
81+
82+
$storage = \Mockery::mock(\Nette\Caching\IStorage::class);
83+
$storage->expects('read')
84+
->twice()
85+
->withAnyArgs()
86+
->andReturn([0 => BlaPresenter::class, 1 => 'test']);
87+
88+
$component = \Mockery::mock(Bla::class);
89+
$component->expects('lookupPath')
90+
->with(\Nette\Application\IPresenter::class)
91+
->andReturn(BlaPresenter::class);
92+
$component->expects('getObservedEvents')
93+
->once()
94+
->withNoArgs()
95+
->andReturn([BlaEventEdit::class, 'shrek']);
96+
97+
$instance = new \Infinityloop\ObserverComponent\EventMapper($application, $storage);
98+
$instance->registerObserver($component);
99+
}
100+
101+
public function testRegisterObserver() : void
102+
{
103+
$httpResponse2 = \Mockery::mock(\Nette\Application\IResponse::class);
104+
$httpResponse2->expects('send');
105+
106+
$request = new \Nette\Application\Request('BlaPresenter:edit');
107+
108+
$presenter = \Mockery::mock(\Nette\Application\IPresenter::class);
109+
$presenter->expects('getAction')
110+
->once()->with(true)
111+
->andReturn('edit');
112+
$presenter->expects('run')
113+
->andReturn($httpResponse2);
114+
115+
$presenterFactory = \Mockery::mock(\Nette\Application\IPresenterFactory::class);
116+
$presenterFactory->expects('createPresenter')
117+
->with('BlaPresenter:edit')
118+
->andReturn($presenter);
119+
120+
$router = \Mockery::mock(\Nette\Routing\Router::class);
121+
122+
$httpRequest = \Mockery::mock(\Nette\Http\IRequest::class);
123+
$httpResponse = \Mockery::mock(\Nette\Http\IResponse::class);
124+
125+
$application = new \Nette\Application\Application($presenterFactory, $router, $httpRequest, $httpResponse);
126+
$application->processRequest($request);
127+
128+
$storage = \Mockery::mock(\Nette\Caching\IStorage::class);
129+
$storage->expects('read')
130+
->twice()
131+
->withAnyArgs()
132+
->andReturnNull();
133+
$storage->expects('write')
134+
->twice()
135+
->withAnyArgs();
136+
137+
$component = \Mockery::mock(Bla::class);
138+
$component->expects('lookupPath')
139+
->with(\Nette\Application\IPresenter::class)
140+
->andReturn(Bla::class);
141+
$component->expects('getObservedEvents')
142+
->withNoArgs()
143+
->andReturn([BlaEventEdit::class]);
144+
145+
$instance = new \Infinityloop\ObserverComponent\EventMapper($application, $storage);
146+
$instance->registerObserver($component);
147+
}
148+
149+
public function testDispatchEvent() : void
150+
{
151+
self::expectExceptionMessage('Kebab');
152+
153+
$httpResponse2 = \Mockery::mock(\Nette\Application\IResponse::class);
154+
$httpResponse2->expects('send');
155+
156+
$request = new \Nette\Application\Request('BlaPresenter:edit');
157+
158+
$component = new Bla();
159+
160+
$presenter = \Mockery::mock(\Nette\Application\IPresenter::class);
161+
$presenter->expects('getAction')
162+
->once()
163+
->with(true)
164+
->andReturn('edit');
165+
$presenter->expects('run')
166+
->andReturn($httpResponse2);
167+
$presenter->expects('getComponent')
168+
->once()
169+
->with(Bla::class)
170+
->andReturn($component);
171+
172+
$presenterFactory = \Mockery::mock(\Nette\Application\IPresenterFactory::class);
173+
$presenterFactory->expects('createPresenter')
174+
->with('BlaPresenter:edit')
175+
->andReturn($presenter);
176+
177+
$router = \Mockery::mock(\Nette\Routing\Router::class);
178+
179+
$httpRequest = \Mockery::mock(\Nette\Http\IRequest::class);
180+
$httpResponse = \Mockery::mock(\Nette\Http\IResponse::class);
181+
182+
$application = new \Nette\Application\Application($presenterFactory, $router, $httpRequest, $httpResponse);
183+
$application->processRequest($request);
184+
185+
$storage = \Mockery::mock(\Nette\Caching\IStorage::class);
186+
$storage->expects('read')
187+
->once()
188+
->withAnyArgs()
189+
->andReturn([0 => Bla::class]);
190+
191+
$event = new BlaEventEdit();
192+
193+
$instance = new \Infinityloop\ObserverComponent\EventMapper($application, $storage);
194+
$instance->dispatchEvent($event);
195+
}
196+
197+
public function testIsComponentRegistered() : void
198+
{
199+
$httpResponse2 = \Mockery::mock(\Nette\Application\IResponse::class);
200+
$httpResponse2->expects('send');
201+
202+
$request = new \Nette\Application\Request('BlaPresenter:edit');
203+
204+
$presenter = \Mockery::mock(\Nette\Application\IPresenter::class);
205+
$presenter->expects('getAction')
206+
->once()
207+
->with(true)
208+
->andReturn('edit');
209+
$presenter->expects('run')
210+
->andReturn($httpResponse2);
211+
212+
$presenterFactory = \Mockery::mock(\Nette\Application\IPresenterFactory::class);
213+
$presenterFactory->expects('createPresenter')
214+
->with('BlaPresenter:edit')
215+
->andReturn($presenter);
216+
217+
$router = \Mockery::mock(\Nette\Routing\Router::class);
218+
219+
$httpRequest = \Mockery::mock(\Nette\Http\IRequest::class);
220+
$httpResponse = \Mockery::mock(\Nette\Http\IResponse::class);
221+
222+
$application = new \Nette\Application\Application($presenterFactory, $router, $httpRequest, $httpResponse);
223+
$application->processRequest($request);
224+
225+
$storage = \Mockery::mock(\Nette\Caching\IStorage::class);
226+
$storage->expects('read')
227+
->times(3)->withAnyArgs()
228+
->andReturnNull();
229+
$storage->expects('write')
230+
->twice()
231+
->withAnyArgs();
232+
233+
$component = \Mockery::mock(Bla::class);
234+
$component->expects('lookupPath')
235+
->with(\Nette\Application\IPresenter::class)
236+
->andReturn(BlaPresenter::class);
237+
$component->expects('getObservedEvents')
238+
->withNoArgs()
239+
->andReturn([Bla::class]);
240+
241+
$instance = new \Infinityloop\ObserverComponent\EventMapper($application, $storage, false);
242+
$instance->registerObserver($component);
243+
}
244+
245+
public function testGetObserverListVisibility() : void
246+
{
247+
$reflection = new \ReflectionClass(EventMapper::class);
248+
249+
self::assertTrue($reflection->getMethod('getObserverList')->isPrivate());
250+
}
251+
252+
public function testIsComponentRegisteredVisibility() : void
253+
{
254+
$reflection = new \ReflectionClass(EventMapper::class);
255+
256+
self::assertTrue($reflection->getMethod('isComponentRegistered')->isPrivate());
257+
}
258+
259+
public function testGetEventMapVisibility() : void
260+
{
261+
$reflection = new \ReflectionClass(EventMapper::class);
262+
263+
self::assertTrue($reflection->getMethod('getEventMap')->isPrivate());
264+
}
265+
}

0 commit comments

Comments
 (0)