Skip to content

Commit 4143eb2

Browse files
committed
Adding tests for the new functionality and fixing some bugs those found
1 parent d8af6a6 commit 4143eb2

File tree

5 files changed

+152
-9
lines changed

5 files changed

+152
-9
lines changed

src/AppBundle/Issues/GitHub/CachedLabelsApi.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public function removeIssueLabel($issueNumber, $label, Repository $repository)
8787
}
8888
}
8989

90+
public function addIssueLabels($issueNumber, array $labels, Repository $repository)
91+
{
92+
foreach ($labels as $label) {
93+
$this->addIssueLabel($issueNumber, $label, $repository);
94+
}
95+
}
96+
9097
private function getCacheKey($issueNumber, Repository $repository)
9198
{
9299
return sprintf('%s_%s_%s', $issueNumber, $repository->getVendor(), $repository->getName());

src/AppBundle/Subscriber/AutoLabelPRFromContentSubscriber.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,20 @@ public function onPullRequest(GitHubEvent $event)
4444
}
4545

4646
// the PR body usually indicates if this is a Bug, Feature, BC Break or Deprecation
47-
if (preg_match('/^\|\s*Bug fix?\s*\|\s*yes\s*$/', $prBody, $matches)) {
47+
if (preg_match('/\|\s*Bug fix\?\s*\|\s*yes\s*/', $prBody, $matches)) {
4848
$prLabels[] = 'Bug';
4949
}
50-
if (preg_match('/^\|\s*New feature?\s*\|\s*yes\s*$/', $prBody, $matches)) {
50+
if (preg_match('/\|\s*New feature\?\s*\|\s*yes\s*/', $prBody, $matches)) {
5151
$prLabels[] = 'Feature';
5252
}
53-
if (preg_match('/^\|\s*BC breaks?\s*\|\s*yes\s*$/', $prBody, $matches)) {
53+
if (preg_match('/\|\s*BC breaks\?\s*\|\s*yes\s*/', $prBody, $matches)) {
5454
$prLabels[] = 'BC Break';
5555
}
56-
if (preg_match('/^\|\s*Deprecations?\s*\|\s*yes\s*$/', $prBody, $matches)) {
56+
if (preg_match('/\|\s*Deprecations\?\s*\|\s*yes\s*/', $prBody, $matches)) {
5757
$prLabels[] = 'Deprecation';
5858
}
5959

60-
foreach ($prLabels as $prLabel) {
61-
$this->labelsApi->addIssueLabel($prNumber, $prLabel, $event->getRepository());
62-
}
60+
$this->labelsApi->addIssueLabels($prNumber, $prLabels, $event->getRepository());
6361

6462
$event->setResponseData(array(
6563
'pull_request' => $prNumber,

src/AppBundle/Tests/Controller/WebhookControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getTests()
3434
'On pull request opened' => array(
3535
'pull_request',
3636
'pull_request.opened.json',
37-
array('pull_request' => 3, 'status_change' => 'needs_review'),
37+
array('pull_request' => 3, 'status_change' => 'needs_review', 'pr_labels' => array('Bug')),
3838
),
3939
'On issue labeled "bug"' => array(
4040
'issues',
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace AppBundle\Tests\Subscriber;
4+
5+
use AppBundle\Event\GitHubEvent;
6+
use AppBundle\GitHubEvents;
7+
use AppBundle\Issues\Status;
8+
use AppBundle\Repository\Repository;
9+
use AppBundle\Subscriber\AutoLabelPRFromContentSubscriber;
10+
use AppBundle\Subscriber\BugLabelNewIssueSubscriber;
11+
use AppBundle\Subscriber\NeedsReviewNewPRSubscriber;
12+
use AppBundle\Subscriber\StatusChangeByCommentSubscriber;
13+
use Symfony\Component\EventDispatcher\EventDispatcher;
14+
15+
class AutLabelPRFromContentSubscriberTest extends \PHPUnit_Framework_TestCase
16+
{
17+
private $autoLabelSubscriber;
18+
19+
private $labelsApi;
20+
21+
private $repository;
22+
23+
/**
24+
* @var EventDispatcher
25+
*/
26+
private static $dispatcher;
27+
28+
public static function setUpBeforeClass()
29+
{
30+
self::$dispatcher = new EventDispatcher();
31+
}
32+
33+
protected function setUp()
34+
{
35+
$this->labelsApi = $this->getMockBuilder('AppBundle\Issues\GitHub\CachedLabelsApi')
36+
->disableOriginalConstructor()
37+
->getMock();
38+
$this->autoLabelSubscriber = new AutoLabelPRFromContentSubscriber($this->labelsApi);
39+
$this->repository = new Repository('weaverryan', 'symfony', [], null);
40+
41+
self::$dispatcher->addSubscriber($this->autoLabelSubscriber);
42+
}
43+
44+
/**
45+
* @dataProvider getPRTests
46+
*/
47+
public function testAutoLabel($prTitle, $prBody, array $expectedNewLabels)
48+
{
49+
$this->labelsApi->expects($this->once())
50+
->method('addIssueLabels')
51+
->with(1234, $expectedNewLabels, $this->repository)
52+
->willReturn(null);
53+
54+
$event = new GitHubEvent(array(
55+
'action' => 'opened',
56+
'pull_request' => array(
57+
'number' => 1234,
58+
'title' => $prTitle,
59+
'body' => $prBody
60+
),
61+
), $this->repository);
62+
63+
self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event);
64+
65+
$responseData = $event->getResponseData();
66+
67+
$this->assertCount(2, $responseData);
68+
$this->assertSame(1234, $responseData['pull_request']);
69+
$this->assertSame($expectedNewLabels, $responseData['pr_labels']);
70+
}
71+
72+
public function getPRTests()
73+
{
74+
$tests = [];
75+
76+
// nothing added automatically
77+
$tests[] = [
78+
'Cool new Feature!',
79+
<<<EOF
80+
FOO
81+
EOF
82+
,
83+
[]
84+
];
85+
86+
$tests[] = [
87+
'I am fixing a bug!',
88+
<<<EOF
89+
Well hi cool peeps!
90+
91+
| Q | A
92+
| ------------- | ---
93+
| Branch? | master
94+
| Bug fix? | yes
95+
| New feature? | no
96+
| BC breaks? | no
97+
| Deprecations? | no
98+
| Tests pass? | yes
99+
| Fixed tickets | n/a
100+
| License | MIT
101+
| Doc PR | n/a
102+
EOF
103+
,
104+
['Bug']
105+
];
106+
107+
$tests[] = [
108+
'Using all the auto-labels!',
109+
<<<EOF
110+
Well hi cool peeps!
111+
112+
| Q | A
113+
| ------------- | ---
114+
| Branch? | master
115+
| Bug fix? | yes
116+
| New feature? | yes
117+
| BC breaks? | yes
118+
| Deprecations? | yes
119+
| Tests pass? | yes
120+
| Fixed tickets | n/a
121+
| License | MIT
122+
| Doc PR | n/a
123+
EOF
124+
,
125+
['Bug', 'Feature', 'BC Break', 'Deprecation']
126+
];
127+
128+
$tests[] = [
129+
'[Asset][BC Break][Fake] Extracting from title [Bug]',
130+
<<<EOF
131+
EOF
132+
,
133+
['Asset', 'BC Break', 'Bug']
134+
];
135+
136+
return $tests;
137+
}
138+
}

src/AppBundle/Tests/webhook_examples/pull_request.opened.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"type": "User",
3232
"site_admin": false
3333
},
34-
"body": "Testing PR",
34+
"body": "Testing PR - | Bug fix? | yes",
3535
"created_at": "2015-06-28T15:20:57Z",
3636
"updated_at": "2015-06-28T15:20:57Z",
3737
"closed_at": null,

0 commit comments

Comments
 (0)