Skip to content

Commit

Permalink
Fixed andkirby#92: Issue duplicated in description.
Browse files Browse the repository at this point in the history
 - Fixed tests. Created parser integration tests.
 - Refactored Jira class. Added verification for an issue initialization.
  • Loading branch information
igor518 committed Jul 5, 2017
1 parent 3416c84 commit 0214154
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 83 deletions.
5 changes: 3 additions & 2 deletions src/lib/PreCommit/Filter/ShortCommitMsg/Parser/Jira.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,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 @@ -5,6 +5,7 @@
namespace testsuite\PreCommit\Test\Filter\ShortCommitMsg;

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

Expand Down Expand Up @@ -32,8 +33,6 @@ public function dataProvider()
'task', //issue type
'Task', //original issue type
"R 123 My new test header \nTest 2.", //full commit message
'TEST', // project key
'jira', // tracker type
"Refactored TEST-123: Test summary!!!\n - My new test header\nTest 2."
],

Expand All @@ -46,8 +45,6 @@ public function dataProvider()
'task', //issue type
'Task', //original issue type
"I 1 My new test header \nTest 2.", //full commit message
'TEST', //project key
'jira', //tracker type
"Implemented TEST-1: Test summary!!!\n - My new test header\nTest 2."
],

Expand All @@ -60,8 +57,6 @@ public function dataProvider()
'task', // issue type
'Task', // original issue type
"1 \nTest 2.", // full commit message
'TEST', // project key
'jira', // tracker type
"Implemented TEST-1: Test summary!!!\n - Test 2."
],
/**
Expand All @@ -73,8 +68,6 @@ public function dataProvider()
'task', // issue type
'Task', // original issue type
"12 Test 1 \nTest 2.", // full commit message
'TEST', // project key
'jira', // tracker type
"Implemented TEST-12: Test summary!!!\n - Test 1\nTest 2."
],
/**
Expand All @@ -86,8 +79,6 @@ public function dataProvider()
'task', //issue type
'Task', //original issue type
"C 22 My new test header \nTest 2.", //full commit message
'TEST', // project key
'jira', // tracker type
"CR Changes TEST-22: Test summary!!!\n - My new test header\nTest 2."
]
];
Expand All @@ -101,8 +92,6 @@ public function dataProvider()
* @param string $type
* @param string $originalType
* @param string $commitMessage
* @param string $projectKey
* @param string $trackerType
* @param string $resultValue
* @dataProvider dataProvider
*/
Expand All @@ -112,70 +101,25 @@ public function testFilterShortMessageWithoutVerb(
$type,
$originalType,
$commitMessage,
$projectKey,
$trackerType,
$resultValue
) {
// Real message.
$message = new Message();

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

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

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


// Create a config object mock
$configMock = $this->getMockBuilder('PreCommit\Config')
->getMock();

$map = [
[
"hooks/commit-msg/message/verb/list/", true, [
"I" => "Implemented",
"R" => "Refactored",
"F" => "Fixed",
"C" => "CR Changes"
]
],
[
"formatters/ShortCommitMsg/formatting/$trackerType", true, [
"regular" => "~^__format__~",
"format" => "__verb__ __issue_key__: __summary__",
],
]
];

$mapNode = [
["tracker/type", true, $trackerType],
["tracker/jira/project", true, $projectKey],
["tracker/jira/active_task", true, $issueKey],
["filters/ShortCommitMsg/issue/default_type_verb/task", true, "I"],
["filters/ShortCommitMsg/issue/default_type_verb/bug", true, "F"],
];

// Configure the getNodeArray stub.
$configMock->method('getNodeArray')
->will($this->returnValueMap($map));

// Configure the getNode stub.
$configMock->method('getNode')
->will($this->returnValueMap($mapNode));

// Getting jira parser reflector.
$reflector = new \ReflectionClass("\\PreCommit\\Filter\\ShortCommitMsg\\Parser\\Jira");

// A new parser object. Set a custom config mock to a config property.

// Create a new instance without constructor.
$parser = $reflector->newInstanceWithoutConstructor();
$configProperty = $reflector->getProperty("config");
$configProperty->setAccessible(true);
$configProperty->setValue($parser, $configMock);
// 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");
Expand All @@ -185,23 +129,8 @@ public function testFilterShortMessageWithoutVerb(
// Interpret original commit message.
$result = $parser->interpret($message);

$reflector = new \ReflectionClass("\\PreCommit\\Filter\\ShortCommitMsg\\Formatter");

$formatter = $reflector->newInstanceWithoutConstructor();
$configProperty = $reflector->getProperty("config");

// Get a formatter type property, set a tracker type for the Formatter.
$typeProperty = $reflector->getProperty("type");
$typeProperty->setAccessible(true);
$typeProperty->setValue($formatter, $trackerType);

// Get a formatter config property, set a config model for the Formatter.
$configProperty->setAccessible(true);
$configProperty->setValue($formatter, $configMock);

// Filter result message through a formatter object.
$resultMessage = $formatter->filter($result);

// Verify commit messages
$this->assertEquals($resultMessage->__toString(), $resultValue);
}

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

return $issue;
}
}

0 comments on commit 0214154

Please sign in to comment.