Skip to content

Commit 7975c88

Browse files
mbabkerwilsonge
authored andcommitted
Add GraphQL support (#39)
1 parent e6d5fa4 commit 7975c88

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

Tests/Package/GraphqlTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
4+
* @license GNU General Public License version 2 or later; see LICENSE
5+
*/
6+
7+
namespace Joomla\Github\Tests;
8+
9+
use Joomla\Github\Package\Graphql;
10+
use Joomla\Github\Tests\Stub\GitHubTestCase;
11+
12+
/**
13+
* Test class for Graphql.
14+
*
15+
* @since 1.0
16+
*/
17+
class GraphqlTest extends GitHubTestCase
18+
{
19+
/**
20+
* @var Graphql
21+
*/
22+
protected $object;
23+
24+
/**
25+
* Sets up the fixture, for example, opens a network connection.
26+
* This method is called before a test is executed.
27+
*
28+
* @access protected
29+
*
30+
* @return void
31+
*/
32+
protected function setUp()
33+
{
34+
parent::setUp();
35+
36+
$this->object = new Graphql($this->options, $this->client);
37+
}
38+
39+
/**
40+
* Tests the create method
41+
*
42+
* @return void
43+
*/
44+
public function testCreate()
45+
{
46+
$this->response->code = 201;
47+
$this->response->body = $this->sampleString;
48+
49+
// Build the query.
50+
$query = 'foo';
51+
52+
// Build the request data.
53+
$data = array(
54+
'query' => $query,
55+
);
56+
57+
// Build the headers.
58+
$headers = array(
59+
'Accept' => 'application/vnd.github.v4+json',
60+
);
61+
62+
$this->client->expects($this->once())
63+
->method('post')
64+
->with('/graphql', $data, $headers)
65+
->will($this->returnValue($this->response));
66+
67+
$this->assertThat(
68+
$this->object->execute($query),
69+
$this->equalTo(json_decode($this->sampleString))
70+
);
71+
}
72+
73+
/**
74+
* Tests the create method
75+
*
76+
* @return void
77+
*/
78+
public function testCreateFailure()
79+
{
80+
$exception = false;
81+
82+
$this->response->code = 500;
83+
$this->response->body = $this->errorString;
84+
85+
// Build the query.
86+
$query = 'foo';
87+
88+
// Build the request data.
89+
$data = array(
90+
'query' => $query,
91+
);
92+
93+
// Build the headers.
94+
$headers = array(
95+
'Accept' => 'application/vnd.github.v4+json',
96+
);
97+
98+
$this->client->expects($this->once())
99+
->method('post')
100+
->with('/graphql', $data, $headers)
101+
->will($this->returnValue($this->response));
102+
103+
try
104+
{
105+
$this->object->execute($query);
106+
$this->fail('Exception not thrown');
107+
}
108+
catch (\DomainException $e)
109+
{
110+
$this->assertThat(
111+
$e->getMessage(),
112+
$this->equalTo(json_decode($this->errorString)->message)
113+
);
114+
}
115+
}
116+
}

src/Github.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @property-read Package\Emojis $emojis GitHub API object for the emojis package.
2020
* @property-read Package\Gists $gists GitHub API object for the gists package.
2121
* @property-read Package\Gitignore $gitignore GitHub API object for the gitignore package.
22+
* @property-read Package\Graphql $graphql GitHub API object for the GraphQL v4 API.
2223
* @property-read Package\Issues $issues GitHub API object for the issues package.
2324
* @property-read Package\Markdown $markdown GitHub API object for the markdown package.
2425
* @property-read Package\Meta $meta GitHub API object for the meta package.

src/Package/Graphql.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Part of the Joomla Framework Github Package
4+
*
5+
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
6+
* @license GNU General Public License version 2 or later; see LICENSE
7+
*/
8+
9+
namespace Joomla\Github\Package;
10+
11+
use Joomla\Github\AbstractPackage;
12+
13+
/**
14+
* GitHub API GraphQL class for the Joomla Framework.
15+
*
16+
* @since __DEPLOY_VERSION__
17+
*
18+
* @documentation https://developer.github.com/v4/
19+
*/
20+
class Graphql extends AbstractPackage
21+
{
22+
/**
23+
* Execute a query against the GraphQL API.
24+
*
25+
* @param string $query The query to perform.
26+
*
27+
* @return string
28+
*
29+
* @since __DEPLOY_VERSION__
30+
*/
31+
public function execute($query)
32+
{
33+
// Build the request path.
34+
$path = '/graphql';
35+
36+
$headers = array(
37+
'Accept' => 'application/vnd.github.v4+json',
38+
);
39+
40+
$data = array(
41+
'query' => $query,
42+
);
43+
44+
// Send the request.
45+
return $this->processResponse(
46+
$this->client->post($this->fetchUrl($path), $data, $headers),
47+
201
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)