|
14 | 14 | use OCP\AppFramework\Controller; |
15 | 15 | use OCP\AppFramework\Http\IOutput; |
16 | 16 | use OCP\AppFramework\Http\Response; |
| 17 | +use OCP\AppFramework\Http\StreamResponse; |
| 18 | +use OCP\IConfig; |
| 19 | +use OCP\IDBConnection; |
| 20 | +use OCP\ISession; |
| 21 | +use Psr\Log\LoggerInterface; |
17 | 22 |
|
18 | 23 | function rrmdir($directory) { |
19 | 24 | $files = array_diff(scandir($directory), ['.','..']); |
@@ -86,6 +91,92 @@ public function testControllerNameAndMethodAreBeingPassed(): void { |
86 | 91 | $this->container); |
87 | 92 | } |
88 | 93 |
|
| 94 | + public function testRequestIsNotFinishedEarlyByDefault(): void { |
| 95 | + $this->dispatcher->expects($this->once()) |
| 96 | + ->method('dispatch') |
| 97 | + ->willReturn(['HTTP/2.0 200 OK', [], [], $this->output, new Response()]); |
| 98 | + |
| 99 | + $this->io->expects($this->never()) |
| 100 | + ->method('finishRequest'); |
| 101 | + |
| 102 | + App::main($this->controllerName, $this->controllerMethod, $this->container, []); |
| 103 | + } |
| 104 | + |
| 105 | + public function testRequestIsFinishedEarlyWhenTheResponseAsksForIt(): void { |
| 106 | + $response = new Response(); |
| 107 | + $response->setFlushEarly(true); |
| 108 | + |
| 109 | + $this->dispatcher->expects($this->once()) |
| 110 | + ->method('dispatch') |
| 111 | + ->willReturn(['HTTP/2.0 200 OK', [], [], $this->output, $response]); |
| 112 | + |
| 113 | + $session = $this->createMock(ISession::class); |
| 114 | + $session->expects($this->once()) |
| 115 | + ->method('close'); |
| 116 | + $this->container[ISession::class] = $session; |
| 117 | + |
| 118 | + $connection = $this->createMock(IDBConnection::class); |
| 119 | + $connection->method('inTransaction') |
| 120 | + ->willReturn(false); |
| 121 | + $this->container[IDBConnection::class] = $connection; |
| 122 | + |
| 123 | + $logger = $this->createMock(LoggerInterface::class); |
| 124 | + $logger->expects($this->never()) |
| 125 | + ->method('warning'); |
| 126 | + $this->container[LoggerInterface::class] = $logger; |
| 127 | + |
| 128 | + $this->io->expects($this->once()) |
| 129 | + ->method('finishRequest'); |
| 130 | + |
| 131 | + App::main($this->controllerName, $this->controllerMethod, $this->container, []); |
| 132 | + } |
| 133 | + |
| 134 | + public function testRequestIsNotFinishedEarlyWhenDisabledByTheAdmin(): void { |
| 135 | + $response = new Response(); |
| 136 | + $response->setFlushEarly(true); |
| 137 | + |
| 138 | + $this->dispatcher->expects($this->once()) |
| 139 | + ->method('dispatch') |
| 140 | + ->willReturn(['HTTP/2.0 200 OK', [], [], $this->output, $response]); |
| 141 | + |
| 142 | + $config = $this->createMock(IConfig::class); |
| 143 | + $config->method('getSystemValueBool') |
| 144 | + ->with('flush_response_early', true) |
| 145 | + ->willReturn(false); |
| 146 | + $this->container[IConfig::class] = $config; |
| 147 | + |
| 148 | + $this->io->expects($this->never()) |
| 149 | + ->method('finishRequest'); |
| 150 | + |
| 151 | + App::main($this->controllerName, $this->controllerMethod, $this->container, []); |
| 152 | + } |
| 153 | + |
| 154 | + public function testOpenTransactionIsLoggedWhenFinishingEarly(): void { |
| 155 | + $response = new Response(); |
| 156 | + $response->setFlushEarly(true); |
| 157 | + |
| 158 | + $this->dispatcher->expects($this->once()) |
| 159 | + ->method('dispatch') |
| 160 | + ->willReturn(['HTTP/2.0 200 OK', [], [], $this->output, $response]); |
| 161 | + |
| 162 | + $this->container[ISession::class] = $this->createMock(ISession::class); |
| 163 | + |
| 164 | + $connection = $this->createMock(IDBConnection::class); |
| 165 | + $connection->method('inTransaction') |
| 166 | + ->willReturn(true); |
| 167 | + $this->container[IDBConnection::class] = $connection; |
| 168 | + |
| 169 | + $logger = $this->createMock(LoggerInterface::class); |
| 170 | + $logger->expects($this->once()) |
| 171 | + ->method('warning'); |
| 172 | + $this->container[LoggerInterface::class] = $logger; |
| 173 | + |
| 174 | + $this->io->expects($this->once()) |
| 175 | + ->method('finishRequest'); |
| 176 | + |
| 177 | + App::main($this->controllerName, $this->controllerMethod, $this->container, []); |
| 178 | + } |
| 179 | + |
89 | 180 | public function testBuildAppNamespace(): void { |
90 | 181 | $ns = App::buildAppNamespace('someapp'); |
91 | 182 | $this->assertEquals('OCA\Someapp', $ns); |
@@ -144,7 +235,9 @@ public function testNoOutput(string $statusCode): void { |
144 | 235 | } |
145 | 236 |
|
146 | 237 | public function testCallbackIsCalled(): void { |
147 | | - $mock = $this->getMockBuilder('OCP\AppFramework\Http\ICallbackResponse') |
| 238 | + // The dispatcher always hands back a Response, not a bare ICallbackResponse |
| 239 | + $mock = $this->getMockBuilder(StreamResponse::class) |
| 240 | + ->disableOriginalConstructor() |
148 | 241 | ->getMock(); |
149 | 242 |
|
150 | 243 | $return = ['HTTP/2.0 200 OK', [], [], $this->output, $mock]; |
|
0 commit comments