Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / nuance / github / __tests__ / NuanceGitHubRawEventTestCase.php
blob4af9a440e2e504a96811a18a246244d2de1cae0e
1 <?php
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,
17 $input);
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,
34 $input);
36 $this->assertGitHubRawEventParse($expect, $event, $name);
40 private function assertGitHubRawEventParse(
41 array $expect,
42 NuanceGitHubRawEvent $event,
43 $name) {
45 $actual = array(
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));
62 ksort($expect);
63 ksort($actual);
65 $this->assertEqual($expect, $actual, $name);
68 private function readTestCases($path) {
69 $files = Filesystem::listDirectory($path, $include_hidden = false);
71 $tests = array();
72 foreach ($files as $file) {
73 $data = Filesystem::readFile($path.$file);
75 $parts = preg_split('/^~{5,}$/m', $data);
76 if (count($parts) < 2) {
77 throw new Exception(
78 pht(
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 '.
82 'in the file.',
83 $file));
84 } else if (count($parts) > 2) {
85 throw new Exception(
86 pht(
87 'Expected test file "%s" to contain exactly two sections, '.
88 'but it has more than two sections.',
89 $file));
92 list($input, $expect) = $parts;
94 try {
95 $input = phutil_json_decode($input);
96 $expect = phutil_json_decode($expect);
97 } catch (Exception $ex) {
98 throw new PhutilProxyException(
99 pht(
100 'Exception while decoding test data for test "%s".',
101 $file),
102 $ex);
105 $tests[$file] = array(
106 'input' => $input,
107 'expect' => $expect,
111 return $tests;