Skip to content

Commit

Permalink
fix: notices on add duplicating torrent
Browse files Browse the repository at this point in the history
Add suppress notices and assume that empty response mean duplicate.

Closes #34
  • Loading branch information
popstas committed Mar 7, 2016
1 parent 4b71ec7 commit ec101e9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
8 changes: 0 additions & 8 deletions src/Command/StatsGet.php
Expand Up @@ -140,14 +140,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

private function removeTorrents(InputInterface $input, OutputInterface $output, array $rows)
{
$limit = (int)$input->getOption('limit') ? (int)$input->getOption('limit') : 0;

$rows = TableUtils::sortRowsByColumnNumber($rows, $input->getOption('sort'));

if ($limit && $limit < count($rows)) {
$rows = array_slice($rows, 0, $limit);
}

$torrentIds = TorrentListUtils::getArrayField($rows, 1);
$command = $this->getApplication()->find('torrent-remove');
$arguments = array(
Expand Down
10 changes: 7 additions & 3 deletions src/Command/TorrentAdd.php
Expand Up @@ -40,8 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$torrentFiles = $input->getArgument('torrent-files');
foreach ($torrentFiles as $torrentFile) {
$this->addFile($input, $output, $client, $torrentFile);
$output->writeln($torrentFile . ' added. Waiting for Transmission...');
$client->waitForTransmission(10);
}

$output->writeln('All torrents added.');
Expand All @@ -50,7 +48,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
private function addFile(InputInterface $input, OutputInterface $output, TransmissionClient $client, $torrentFile)
{
$this->dryRun($input, $output, function () use ($torrentFile, $client, $input, $output) {
$client->addTorrent($torrentFile);
$torrentAdded = $client->addTorrent($torrentFile);
if ($torrentAdded) {
$output->writeln($torrentFile . ' added. Waiting for Transmission...');
$client->waitForTransmission(10);
} else {
$output->writeln($torrentFile . ' was not added. Probably it was added before.');
}
}, 'dry-run, don\'t really add torrents');
}
}
9 changes: 8 additions & 1 deletion src/TransmissionClient.php
Expand Up @@ -59,6 +59,10 @@ public function getTorrentData(array $ids = [], $fields = [])

public function addTorrent($torrentFile, $downloadDir = null)
{
// remove error suppress after https://github.com/MartialGeek/transmission-api/issues/6 closed
$errorLevel = ini_get('error_reporting');
error_reporting(E_ALL & ~ E_NOTICE & ~ E_STRICT & ~ E_DEPRECATED);

$arguments = [];
if (is_file($torrentFile)) {
$arguments[Torrent\Add::METAINFO] = base64_encode(file_get_contents($torrentFile));
Expand All @@ -71,7 +75,10 @@ public function addTorrent($torrentFile, $downloadDir = null)
}

$this->createSession();
return $this->api->torrentAdd($this->sessionId, $arguments);

$response = $this->api->torrentAdd($this->sessionId, $arguments);
error_reporting($errorLevel);
return $response;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Command/TorrentAddTest.php
Expand Up @@ -14,6 +14,12 @@ public function setUp()

public function testAddOne()
{
$this->app->getClient()->method('addTorrent')->willReturn([
'hashString' => '29cf5b5a005af16250801fd1586efae39604c0f5',
'id' => 101,
'name' => 'movie.mkv',
]);

$torrentFile = tempnam(sys_get_temp_dir(), 'torrent');
$this->app->getClient()->expects($this->once())->method('addTorrent');
$this->executeCommand(['torrent-files' => [$torrentFile]]);
Expand All @@ -23,11 +29,26 @@ public function testAddOne()

public function testAddSeveral()
{
$this->app->getClient()->method('addTorrent')->willReturn([
'hashString' => '29cf5b5a005af16250801fd1586efae39604c0f5',
'id' => 101,
'name' => 'movie.mkv',
]);

$this->app->getClient()->expects($this->exactly(2))->method('addTorrent');
$this->executeCommand(['torrent-files' => ['url-1', 'url-2']]);
$this->assertRegExp('/added/', $this->getDisplay());
}

public function testAddDuplicate()
{
$this->app->getClient()->method('addTorrent')->willReturn(null);

$this->app->getClient()->expects($this->once())->method('addTorrent');
$this->executeCommand(['torrent-files' => ['url-1']]);
$this->assertRegExp('/added before/', $this->getDisplay());
}

public function testDryRun()
{
$this->app->getClient()->expects($this->never())->method('addTorrent');
Expand Down

0 comments on commit ec101e9

Please sign in to comment.