diff --git a/src/lib/PreCommit/Filter/ShortCommitMsg/Parser/Jira.php b/src/lib/PreCommit/Filter/ShortCommitMsg/Parser/Jira.php index 2761cf5..750a4dd 100644 --- a/src/lib/PreCommit/Filter/ShortCommitMsg/Parser/Jira.php +++ b/src/lib/PreCommit/Filter/ShortCommitMsg/Parser/Jira.php @@ -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; } diff --git a/src/tests/testsuite/PreCommit/Test/Filter/ShortCommitMsg/ParserTest.php b/src/tests/testsuite/PreCommit/Test/Filter/ShortCommitMsg/ParserTest.php index ff11260..1920caf 100644 --- a/src/tests/testsuite/PreCommit/Test/Filter/ShortCommitMsg/ParserTest.php +++ b/src/tests/testsuite/PreCommit/Test/Filter/ShortCommitMsg/ParserTest.php @@ -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; @@ -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." ], @@ -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." ], @@ -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." ], /** @@ -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." ], /** @@ -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." ] ]; @@ -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 */ @@ -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"); @@ -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); } @@ -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(); @@ -229,7 +158,6 @@ protected function getIssueMock($summary, $key, $type, $originalType) ->willReturn($type); $issue->method('getOriginalType') ->willReturn($originalType); - return $issue; } }