public function testDestructorCallsCurlCloseOnUsedObject() { $uriMock = $this->getMockForAbstractClass('\\Psr\\Http\\Message\\UriInterface'); $uriMock->method('getScheme')->willReturn('ftp'); $this->request->method('getUri')->willReturn($uriMock); $this->getFunctionMock(self::SUT_NAMESPACE, 'curl_exec')->expects($this->any())->willReturn(true); $this->getFunctionMock(self::SUT_NAMESPACE, 'curl_setopt_array')->expects($this->any())->willReturn(true); $this->subjectUnderTest->execute(); $this->getFunctionMock(self::SUT_NAMESPACE, 'curl_close')->expects($this->atLeastOnce()); $this->subjectUnderTest->__destruct(); }
/** * @param $url * * @return bool|string Returns false on error or downloaded RSS file content */ private function fetchFeed($url) { $rssRequest = new Request('GET', $url); $fetchJob = new FetchJob($this->appConfiguration, $rssRequest); //TODO: Implement RSS size limit when FetchJob start supporting it try { if (!$fetchJob->execute()) { throw new \Exception('RSS download job failed for unknown reason'); } } catch (\Exception $e) { $type = get_class($e); $this->logger->error($type . ': ' . $e->getMessage()); return false; } $responseStream = $fetchJob->getResponseStream(); fseek($responseStream, 0); return stream_get_contents($responseStream); }
private function downloadLinks(array $links) { $this->logger->info('Begin downloading ' . count($links) . ' files...'); foreach ($links as $name => $link) { //TODO check if filename exists! $filePath = $this->appConfiguration->getFilesSavePath() . '/' . $name . '_' . md5(rand()) . '.torrent'; $this->logger->debug("Downloading {$name} => {$link} to {$filePath}"); $fp = @fopen($filePath, 'w'); if (!$fp) { $this->logger->error("Downloading of {$link} failed - unable to create file {$filePath}"); continue; } $request = new Request('GET', $link); $job = new FetchJob($this->appConfiguration, $request); $job->setResponseStream($fp); $this->logger->debug("Executing job for {$link}"); try { if (!$job->execute()) { $this->logger->error("Downloading of {$link} failed - unknown error occurred"); continue; } } catch (\Exception $e) { $type = get_class($e); $this->logger->error("Downloading of {$link} failed - {$type}: " . $e->getMessage()); continue; } fclose($fp); $this->logger->info("Downloaded {$link} to {$filePath}"); } }