3 final class NuanceGitHubRawEventTestCase
4 extends PhabricatorTestCase
{
6 public function testIssueEvents() {
7 $path = dirname(__FILE__
).'/issueevents/';
9 $cases = $this->readTestCases($path);
11 foreach ($cases as $name => $info) {
12 $input = $info['input'];
13 $expect = $info['expect'];
15 $event = NuanceGitHubRawEvent
::newEvent(
16 NuanceGitHubRawEvent
::TYPE_ISSUE
,
19 $this->assertGitHubRawEventParse($expect, $event, $name);
23 public function testRepositoryEvents() {
24 $path = dirname(__FILE__
).'/repositoryevents/';
26 $cases = $this->readTestCases($path);
28 foreach ($cases as $name => $info) {
29 $input = $info['input'];
30 $expect = $info['expect'];
32 $event = NuanceGitHubRawEvent
::newEvent(
33 NuanceGitHubRawEvent
::TYPE_REPOSITORY
,
36 $this->assertGitHubRawEventParse($expect, $event, $name);
40 private function assertGitHubRawEventParse(
42 NuanceGitHubRawEvent
$event,
46 'repository.name.full' => $event->getRepositoryFullName(),
47 'is.issue' => $event->isIssueEvent(),
48 'is.pull' => $event->isPullRequestEvent(),
49 'issue.number' => $event->getIssueNumber(),
50 'pull.number' => $event->getPullRequestNumber(),
51 'id' => $event->getID(),
52 'uri' => $event->getURI(),
53 'title.full' => $event->getEventFullTitle(),
54 'comment' => $event->getComment(),
55 'actor.id' => $event->getActorGitHubUserID(),
58 // Only verify the keys which are actually present in the test. This
59 // allows tests to specify only relevant keys.
60 $actual = array_select_keys($actual, array_keys($expect));
65 $this->assertEqual($expect, $actual, $name);
68 private function readTestCases($path) {
69 $files = Filesystem
::listDirectory($path, $include_hidden = false);
72 foreach ($files as $file) {
73 $data = Filesystem
::readFile($path.$file);
75 $parts = preg_split('/^~{5,}$/m', $data);
76 if (count($parts) < 2) {
79 'Expected test file "%s" to contain an input section in JSON, '.
80 'then an expected result section in JSON, with the two sections '.
81 'separated by a line of "~~~~~", but the divider is not present '.
84 } else if (count($parts) > 2) {
87 'Expected test file "%s" to contain exactly two sections, '.
88 'but it has more than two sections.',
92 list($input, $expect) = $parts;
95 $input = phutil_json_decode($input);
96 $expect = phutil_json_decode($expect);
97 } catch (Exception
$ex) {
98 throw new PhutilProxyException(
100 'Exception while decoding test data for test "%s".',
105 $tests[$file] = array(