Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #136

Merged
merged 3 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/lib/PreCommit/Filter/ShortCommitMsg/Parser/Jira.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ protected function interpretShortMessage($message)
//endregion

$userMessage = null;
if ($m) {
$userMessage = trim(array_pop($m));
if ($m && isset($m[6])) {
$userMessage = $m[6];
}

return [$commitVerb, $issueKey, $userMessage];
Expand Down Expand Up @@ -334,8 +334,9 @@ protected function mergeComment($comment, $commentInline)
*/
protected function initIssue($issueKey)
{
$this->issue = Issue::factory($issueKey);

if (!$this->issue) {
$this->issue = Issue::factory($issueKey);
}
return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/
namespace testsuite\PreCommit\Test\Filter\ShortCommitMsg;

use PreCommit\Filter\ShortCommitMsg;
use PreCommit\Filter\Explode;
use PreCommit\Filter\ShortCommitMsg\Formatter;
use PreCommit\Issue;
use PreCommit\Message;

Expand All @@ -31,37 +32,55 @@ public function dataProvider()
'Test summary!!!', //issue summary
'task', //issue type
'Task', //original issue type
"My test 1!\nTest 2.", //user message
"123 My test 1!\nTest 2.", //full commit message
'Implemented', //verb
'', //short verb
"R 123 My new test header \nTest 2.", //full commit message
"Refactored TEST-123: Test summary!!!\n - My new test header\nTest 2."
],

/**
* Test getting verb by short verb
* Test getting default verb by issue type
*/
[
'TEST-123', //issue key
'TEST-1', //issue key
'Test summary!!!', //issue summary
'task', //issue type
'Task', //original issue type
"My test 1!\nTest 2.", //user message
"F 123 My test 1!\nTest 2.", //full commit message
'Fixed', //verb
'F', //short verb
"I 1 My new test header \nTest 2.", //full commit message
"Implemented TEST-1: Test summary!!!\n - My new test header\nTest 2."
],

/**
* Test working with full issue key
* Test getting default verb by issue type
*/
[
'TEST-123', //issue key
'TEST-1', // issue key
'Test summary!!!', // issue summary
'task', // issue type
'Task', // original issue type
"1 \nTest 2.", // full commit message
"Implemented TEST-1: Test summary!!!\n - Test 2."
],
/**
* Test getting default verb by issue type
*/
[
'TEST-12', // issue key
'Test summary!!!', // issue summary
'task', // issue type
'Task', // original issue type
"12 Test 1 \nTest 2.", // full commit message
"Implemented TEST-12: Test summary!!!\n - Test 1\nTest 2."
],
/**
* Test getting default verb by issue type
*/
[
'TEST-22', //issue key
'Test summary!!!', //issue summary
'task', //issue type
'Task', //original issue type
"My test 1!\nTest 2.", //user message
"F TEST-123 My test 1!\nTest 2.", //full commit message
'Fixed', //verb
'F', //short verb
],
"C 22 My new test header \nTest 2.", //full commit message
"CR Changes TEST-22: Test summary!!!\n - My new test header\nTest 2."
]
];
}

Expand All @@ -72,45 +91,47 @@ public function dataProvider()
* @param string $summary
* @param string $type
* @param string $originalType
* @param string $userBody
* @param string $commitMessage
* @param string $verb
* @param string $shortVerb
* @param string $resultValue
* @dataProvider dataProvider
*/
public function testFilterShortMessageWithoutVerb(
$issueKey,
$summary,
$type,
$originalType,
$userBody,
$commitMessage,
$verb,
$shortVerb
$resultValue
) {
$this->markTestIncomplete();

$message = new Message();
$message = new Message();
$explodeFilter = new Explode();
$formatter = new Formatter();

// Prepare a commit message
$message->body = $commitMessage;

// Filter commit message through the explode filter.
$message = $explodeFilter->filter($message);
$issue = $this->getIssueMock($summary, $issueKey, $type, $originalType);

/** @var ShortCommitMsg\Parser|\PHPUnit_Framework_MockObject_MockObject $parser */
$parser = $this->getMock('PreCommit\Filter\ShortCommitMsg\Parser', ['_initIssue', '_getIssue']);
$parser->method('_initIssue')
->willReturnSelf();
$parser->method('_getIssue')
->willReturn($issue);
// Getting jira parser reflector.
$reflector = new \ReflectionClass("\\PreCommit\\Filter\\ShortCommitMsg\\Parser\\Jira");

// Create a new instance from reflector.
$parser = $reflector->newInstance();

// Specify an "issue" property. Add a custom mock object as an issue property.
$issueProperty = $reflector->getProperty("issue");
$issueProperty->setAccessible(true);
$issueProperty->setValue($parser, $issue);

// Interpret original commit message.
$result = $parser->interpret($message);

$this->assertEquals($message, $result);
$this->assertEquals($issue, $message->issue);
$this->assertEquals($summary, $message->summary);
$this->assertEquals($issueKey, $message->issueKey);
$this->assertEquals($userBody, $message->userBody);
$this->assertEquals($verb, $message->verb);
$this->assertEquals($shortVerb, $message->shortVerb);
// Filter result message through a formatter object.
$resultMessage = $formatter->filter($result);
$this->assertEquals($resultMessage->__toString(), $resultValue);
}

/**
Expand All @@ -124,8 +145,11 @@ public function testFilterShortMessageWithoutVerb(
*/
protected function getIssueMock($summary, $key, $type, $originalType)
{
/** @var Issue\AbstractAdapter|\PHPUnit_Framework_MockObject_MockObject $filter */
$issue = $this->getMock('PreCommit\Issue\AdapterAbstract', [], [], '', false);
/** @var Issue\AbstractAdapter|\PHPUnit_Framework_MockObject_MockObject $issue */
$issue = $this->getMockBuilder('PreCommit\Issue\AdapterAbstract')
->setMethods(['getSummary', 'getKey', 'getType', 'getOriginalType'])
->getMock();

$issue->method('getSummary')
->willReturn($summary);
$issue->method('getKey')
Expand All @@ -134,7 +158,6 @@ protected function getIssueMock($summary, $key, $type, $originalType)
->willReturn($type);
$issue->method('getOriginalType')
->willReturn($originalType);

return $issue;
}
}