Skip to content

Commit

Permalink
Fix issue with exception in pull request link generation
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Apr 26, 2018
1 parent 47e33b0 commit 729f57c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/EntryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function preparePullRequestLink(int $pr, ?string $package) : string
return $link;
}

throw Exception\InvalidPullRequestLinkException::forLink($link);
throw Exception\InvalidPullRequestLinkException::forPackage($package, $pr);
}

$link = $this->generatePullRequestLink($pr, (new ComposerPackage())->getName(realpath(getcwd())));
Expand Down Expand Up @@ -210,7 +210,7 @@ private function generatePullRequestLink(int $pr, string $package) : ?string

private function probeLink(string $link) : bool
{
$headers = get_headers($link, 1, stream_context_create(['http' => ['method'=>'HEAD']]));
$headers = get_headers($link, 1, stream_context_create(['http' => ['method' => 'HEAD']]));
$statusLine = explode(' ', $headers[0]);
$statusCode = (int) $statusLine[1];

Expand Down
23 changes: 23 additions & 0 deletions src/Exception/InvalidPullRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @see https://github.com/phly/keep-a-changelog-tagger for the canonical source repository
* @copyright Copyright (c) 2018 Matthew Weier O'Phinney
* @license https://github.com/phly/keep-a-changelog-tagger/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Phly\KeepAChangelog\Exception;

use RuntimeException;

class InvalidPullRequestException extends RuntimeException
{
public static function for(int $pr) : self
{
return new self(sprintf(
'PR %d is not valid',
$pr
));
}
}
7 changes: 4 additions & 3 deletions src/Exception/InvalidPullRequestLinkException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

class InvalidPullRequestLinkException extends RuntimeException
{
public static function forLink(string $link) : self
public static function forPackage(string $package, int $pr) : self
{
return new self(sprintf(
'The pull request link %s does not exist',
$link
'The pull request package %s has no PR %d',
$package,
$pr
));
}

Expand Down
22 changes: 19 additions & 3 deletions test/EntryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testPrepareEntryReturnsEntryVerbatimIfNoPrOptionProvided()
public function testPrepareEntryWithPrOptionRaisesExceptionIfPackageOptionIsInvalid()
{
$entry = 'This is the entry';
$pr = 42;
$pr = 1;
$package = 'not-a-valid-package-name';

$this->input->getArgument('entry')->willReturn($entry);
Expand All @@ -99,10 +99,26 @@ public function testPrepareEntryWithPrOptionRaisesExceptionIfPackageOptionIsInva
$this->reflectMethod($command, 'prepareEntry')->invoke($command, $this->input->reveal());
}

public function testPrepareEntryWithPrOptionRaisesExceptionIfLinkIsInvalid()
{
$entry = 'This is the entry';
$pr = 9999999999;
$package = 'phly/keep-a-changelog';

$this->input->getArgument('entry')->willReturn($entry);
$this->input->getOption('pr')->willReturn($pr);
$this->input->getOption('package')->willReturn($package);

$command = new EntryCommand('entry:added');

$this->expectException(Exception\InvalidPullRequestLinkException::class);
$this->reflectMethod($command, 'prepareEntry')->invoke($command, $this->input->reveal());
}

public function testPrepareEntryReturnsEntryWithPrLinkPrefixedWhenPackageOptionPresentAndValid()
{
$entry = 'This is the entry';
$pr = 42;
$pr = 1;
$package = 'phly/keep-a-changelog';

$this->input->getArgument('entry')->willReturn($entry);
Expand All @@ -111,7 +127,7 @@ public function testPrepareEntryReturnsEntryWithPrLinkPrefixedWhenPackageOptionP

$command = new EntryCommand('entry:added');

$expected = '[#42](https://github.com/phly/keep-a-changelog/pull/42) ' . $entry;
$expected = '[#1](https://github.com/phly/keep-a-changelog/pull/1) ' . $entry;

$this->assertSame(
$expected,
Expand Down

0 comments on commit 729f57c

Please sign in to comment.