protected function handleFile(File $file, Response $response) { if (isset($this->filesContents[$file->getPath()])) { return $this->filesContents[$file->getPath()]; } $file->getContents()->then(function ($contents) use($file) { $this->filesContents[$file->getPath()] = $contents; return $file->close()->then(function () use($contents) { return $contents; }); })->then(function ($fileContents) use($response) { $response->writeHead(200); $response->end($fileContents); }); }
public function testClose() { $path = 'foo.bar'; $fd = '0123456789abcdef'; $filesystem = $this->getMock('React\\Filesystem\\Eio\\Adapter', ['close', 'open'], [$this->getMock('React\\EventLoop\\StreamSelectLoop')]); $stream = $this->getMock('React\\Filesystem\\Stream\\GenericStreamInterface', ['getFiledescriptor'], ['foo:bar']); $stream->expects($this->once())->method('getFiledescriptor')->with()->will($this->returnValue($fd)); $openPromise = $this->getMock('React\\Promise\\PromiseInterface', ['then']); $openPromise->expects($this->once())->method('then')->with($this->isType('callable'))->will($this->returnCallback(function ($resolveCb) use($stream) { return new FulfilledPromise($resolveCb($stream)); })); $filesystem->expects($this->once())->method('open')->with($path, 'r')->will($this->returnValue($openPromise)); $closePromise = $this->getMock('React\\Promise\\PromiseInterface', ['then']); $closePromise->expects($this->once())->method('then')->with($this->isType('callable'))->will($this->returnCallback(function ($resolveCb) use($stream) { return \React\Promise\resolve($resolveCb($stream)); })); $filesystem->expects($this->once())->method('close')->with($fd)->will($this->returnValue($closePromise)); $file = new File($path, Filesystem::createFromAdapter($filesystem)); $file->open('r'); $file->close(); }