forked from laminas-api-tools/api-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationTest.php
159 lines (135 loc) · 4.91 KB
/
ApplicationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace LaminasTest\ApiTools;
use Exception;
use Laminas\ApiTools\Application;
use Laminas\EventManager\EventInterface;
use Laminas\EventManager\EventManager;
use Laminas\Http\PhpEnvironment;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
class ApplicationTest extends TestCase
{
use ProphecyTrait;
protected function setUp(): void
{
$events = new EventManager();
$request = $this->prophesize(PhpEnvironment\Request::class);
$response = $this->prophesize(PhpEnvironment\Response::class);
$this->services = $this->setUpServices(
$this->prophesize(ServiceManager::class),
$events,
$request,
$response
);
$this->app = $this->setUpMvcEvent(
$this->createApplication(
$this->services->reveal(),
$events,
$request->reveal(),
$response->reveal()
),
$request,
$response
);
}
/**
* Create and return an Application instance.
*
* Checks to see which version of laminas-mvc is present, and uses that to
* determine how to construct the instance.
*
* @param ServiceManager $services
* @param EventManager $events
* @param PhpEnvironment\Request $request
* @param PhpEnvironment\Response $response
* @return Application
*/
public function createApplication($services, $events, $request, $response)
{
$r = new ReflectionMethod(Application::class, '__construct');
if ($r->getNumberOfRequiredParameters() === 2) {
// v2
return new Application([], $services, $events, $request, $response);
}
// v3
return new Application($services, $events, $request, $response);
}
/**
* @param ObjectProphecy&ServiceManager $services
* @param ObjectProphecy&PhpEnvironment\Request $request
* @param ObjectProphecy&PhpEnvironment\Response $response
* @return ObjectProphecy&ServiceManager
*/
public function setUpServices($services, EventManager $events, $request, $response)
{
$services->get('config')->willReturn([]);
$services->get('EventManager')->willReturn($events);
$services->get('Request')->willReturn($request->reveal());
$services->get('Response')->willReturn($response->reveal());
return $services;
}
/**
* @param ObjectProphecy&PhpEnvironment\Request $request
* @param ObjectProphecy&PhpEnvironment\Response $response
* @throws ReflectionException
*/
public function setUpMvcEvent(Application $app, $request, $response): Application
{
$event = new MvcEvent();
$event->setTarget($app);
$event->setApplication($app)
->setRequest($request->reveal())
->setResponse($response->reveal());
$r = new ReflectionProperty($app, 'event');
$r->setAccessible(true);
$r->setValue($app, $event);
return $app;
}
public function testRouteListenerRaisingExceptionTriggersDispatchErrorAndSkipsDispatch(): void
{
$events = $this->app->getEventManager();
$response = $this->prophesize(PhpEnvironment\Response::class)->reveal();
$events->attach('route', function ($e) {
throw new Exception();
});
$events->attach('dispatch.error', function ($e) use ($response) {
$this->assertNotEmpty($e->getError());
return $response;
});
$events->attach('dispatch', function ($e) {
$this->fail('dispatch event triggered when it should not be');
});
$events->attach('render', function ($e) {
$this->fail('render event triggered when it should not be');
});
$finishTriggered = false;
$events->attach('finish', function ($e) use (&$finishTriggered) {
$finishTriggered = true;
});
$this->app->run();
$this->assertTrue($finishTriggered);
$this->assertSame($response, $this->app->getResponse());
}
public function testStopPropagationFromPrevEventShouldBeCleared(): void
{
$events = $this->app->getEventManager();
$response = $this->prophesize(PhpEnvironment\Response::class)->reveal();
$events->attach('route', function (EventInterface $e) use ($response) {
$e->stopPropagation(true);
return $response;
});
$isStopPropagation = null;
$events->attach('finish', function (EventInterface $e) use (&$isStopPropagation) {
$isStopPropagation = $e->propagationIsStopped();
});
$this->app->run();
$this->assertFalse($isStopPropagation);
}
}