public function testTorrentAddShouldThrowAnExceptionWithAnInvalidSessionId() { $arguments = ['filename' => '/path/to/Fedora.torrent']; $requestBody = '{"method":"torrent-add","arguments":{"filename":"/path/to/Fedora.torrent"}}'; $invalidSessionId = uniqid(); $this->sendRequest($requestBody, $invalidSessionId)->andThrow($this->generateCSRFException()); try { $this->rpcClient->torrentAdd($invalidSessionId, $arguments); } catch (CSRFException $e) { $this->assertSame($this->sessionId, $e->getSessionId()); } }
public function testTorrentAddShouldThrowAnExceptionWithDuplicateTorrent() { $success = false; $arguments = ['filename' => '/path/to/Fedora.torrent']; $requestBody = '{"method":"torrent-add","arguments":{"filename":"/path/to/Fedora.torrent"}}'; $hashString = md5('Fedora.iso'); $torrentId = 42; $torrentName = 'Fedora.iso'; $this->sendRequest($requestBody)->andReturn($this->guzzleResponse); $this->setResponseBody(sprintf('{"arguments":{"torrent-duplicate":{"id":%d,"name":"%s","hashString":"%s"}},"result":"success"}', $torrentId, $torrentName, $hashString)); try { $this->rpcClient->torrentAdd($this->sessionId, $arguments); } catch (DuplicateTorrentException $e) { $this->assertSame($torrentId, $e->getTorrentId()); $this->assertSame($torrentName, $e->getTorrentName()); $this->assertSame($hashString, $e->getTorrentHashString()); $success = true; } if (!$success) { $this->fail('DuplicateTorrentException was not thrown.'); } }
function checkNotEmptyList(array $torrentList) { if (empty($torrentList)) { echo 'No torrents found.' . PHP_EOL; exit(0); } } $sessionId = ''; // Fetching a new session ID try { $api->sessionGet($sessionId); } catch (CSRFException $e) { $sessionId = $e->getSessionId(); } // Adding a new torrent to the download queue $torrentData = $api->torrentAdd($sessionId, [Add::FILENAME => $newTorrentFile]); printf('New torrent "%s" with ID %d added:', $torrentData[Get::NAME], $torrentData[Get::ID]); echo PHP_EOL; $torrentList = getTorrentData($api, $sessionId, []); checkNotEmptyList($torrentList); echo 'Torrent list:' . PHP_EOL; printTorrentData($torrentList); // Stopping the first torrent $api->torrentStop($sessionId, new TorrentIdList([$torrentList[0][Get::ID]])); sleep(1); // The transmission API is not real time $torrentList = getTorrentData($api, $sessionId, []); checkNotEmptyList($torrentList); echo 'Torrent list:' . PHP_EOL; printTorrentData($torrentList); // Starting the first torrent