Skip to content

Commit e692ef6

Browse files
committed
Introduce behat testing ! =)
1 parent 58d6ccd commit e692ef6

File tree

12 files changed

+210
-21
lines changed

12 files changed

+210
-21
lines changed

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ php:
55
- 5.4
66

77
before_script:
8-
- curl -s http://getcomposer.org/installer | php
9-
- php composer.phar update
8+
- curl -s http://getcomposer.org/installer | php -- --quiet
9+
- php composer.phar install --dev
1010

11-
script: phpunit
11+
script:
12+
- phpunit
13+
- bin/behat

behat.yml.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
default:
2+
paths:
3+
features: features
4+
bootstrap: features/bootstrap
5+
formatter:
6+
name: progress
7+
8+
context:
9+
parameters:
10+
base_url: "http://api.github.com"

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=5.3.2",
21-
"ext-curl": "*",
20+
"php": ">=5.3.2",
21+
"ext-curl": "*",
2222
"kriswallsmith/buzz": "0.7"
2323
},
24+
"require-dev": {
25+
"behat/behat": "2.4.*"
26+
},
2427
"autoload": {
2528
"psr-0": { "Github": "lib/" }
29+
},
30+
"config": {
31+
"bin-dir": "bin/"
2632
}
2733
}

features/bootstrap/FeatureContext.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
use Behat\Behat\Event\SuiteEvent,
4+
Behat\Behat\Event\FeatureEvent;
5+
use Behat\Behat\Context\Step\Given,
6+
Behat\Behat\Context\Step\When,
7+
Behat\Behat\Context\Step\Then;
8+
use Behat\Behat\Context\ClosuredContextInterface,
9+
Behat\Behat\Context\TranslatedContextInterface,
10+
Behat\Behat\Context\BehatContext,
11+
Behat\Behat\Exception\PendingException;
12+
use Behat\Gherkin\Node\PyStringNode,
13+
Behat\Gherkin\Node\TableNode;
14+
15+
use Github\Client;
16+
17+
require_once 'PHPUnit/Autoload.php';
18+
require_once 'PHPUnit/Framework/Assert/Functions.php';
19+
20+
class FeatureContext extends BehatContext
21+
{
22+
private $client;
23+
private $lastResponse;
24+
25+
public function __construct()
26+
{
27+
$this->client = new Client();
28+
}
29+
30+
/**
31+
* @AfterScenario
32+
*/
33+
public function clearLastResponse()
34+
{
35+
$this->lastResponse = null;
36+
}
37+
38+
/**
39+
* @When /^(?:|I )send a request for rate limit information$/
40+
*/
41+
public function iSendRequestForRateLimitInformation()
42+
{
43+
$this->lastResponse = $this->client->getRateLimit();
44+
}
45+
46+
/**
47+
* @Then /^(?:|I )should not reach my rate limit$/
48+
*/
49+
public function iShouldNotReachMyRateLimit()
50+
{
51+
$remainingCalls = $this->client->getHttpClient()->remainingCalls;
52+
53+
assertNotNull($remainingCalls);
54+
assertTrue(5000 > $remainingCalls);
55+
assertTrue(0 < $remainingCalls);
56+
}
57+
58+
/**
59+
* @When /^(?:|I )send a markdown data:$/
60+
*/
61+
public function iSendMarkdownData(PyStringNode $jsonString)
62+
{
63+
$data = json_decode($jsonString->getRaw(), true);
64+
65+
$this->lastResponse = $this->client->getMarkdownApi()->render(
66+
$data['text'],
67+
isset($data['mode']) ? $data['mode'] : null,
68+
isset($data['context']) ? $data['context'] : null
69+
);
70+
}
71+
72+
/**
73+
* @Then /^(?:the )?response should equal to html:$/
74+
*/
75+
public function theResponseShouldEqualToHtml(PyStringNode $htmlString)
76+
{
77+
$expected = $htmlString->getRaw();
78+
$actual = $this->lastResponse;
79+
80+
assertEquals($expected, $actual, "Failed asserting that equals to \n".print_r($actual, true));
81+
}
82+
83+
/**
84+
* @Then /^(?:the )?response should equal to json:$/
85+
*/
86+
public function theResponseShouldEqualToJson(PyStringNode $jsonString)
87+
{
88+
$expected = json_decode($jsonString->getRaw(), true);
89+
$actual = $this->lastResponse;
90+
91+
if (null === $expected) {
92+
throw new \RuntimeException(
93+
"Can not convert etalon to json:\n".$jsonString->getRaw()
94+
);
95+
}
96+
97+
assertEquals($expected, $actual, "Failed asserting that equals to \n".print_r($actual, true));
98+
}
99+
}

features/markdown.feature

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Feature: Markdown rendering
2+
In order to get rendered markdown file
3+
As an anonymous user
4+
I need to be able to post data to /markdown resource
5+
6+
Scenario: Get rendered markdown file
7+
When I send a markdown data:
8+
"""
9+
{
10+
"text": "Hello world github/linguist#1 **cool**, and #1!"
11+
}
12+
"""
13+
Then the response should equal to html:
14+
"""
15+
<p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>
16+
"""
17+
18+
Scenario: Get rendered markdown file with context
19+
When I send a markdown data:
20+
"""
21+
{
22+
"text": "Hello world KnpLabs/KnpBundles#1 **cool**, and #1!",
23+
"mode": "gfm",
24+
"context": "KnpLabs/KnpMenu"
25+
}
26+
"""
27+
Then the response should equal to html:
28+
"""
29+
<p>Hello world <a href="https://github.com/KnpLabs/KnpBundles/issues/1" class="issue-link" title="Display docs">KnpLabs/KnpBundles#1</a> <strong>cool</strong>, and <a href="https://github.com/KnpLabs/KnpMenu/issues/1" class="issue-link" title="Limitation when using the TwigRenderer from elsewhere than a Twig template">#1</a>!</p>
30+
"""

features/rate_limit.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Feature: Rate Limit
2+
In order to get rate limit information
3+
As an anonymous user
4+
I need to be able to make calls to /rate_limit resource
5+
6+
Scenario: Get rate limit
7+
When I send a request for rate limit information
8+
Then I should not reach my rate limit
9+
# Commented because GH limit is reset once per hour
10+
# Then the response should equal to json:
11+
# """
12+
# {
13+
# "rate": {
14+
# "remaining": 4999,
15+
# "limit": 5000
16+
# }
17+
# }
18+
# """

lib/Github/Api/Commit.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ class Commit extends Api
2323
*/
2424
public function getBranchCommits($username, $repo, $branch)
2525
{
26-
$branchSha = $this->getBranchSha($username, $repo, $branch);
26+
$url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/commits';
27+
28+
$sha = $this->getBranchSha($username, $repo, $branch);
29+
if (null !== $sha) {
30+
$url .= '?sha='.urlencode($sha);
31+
}
2732

28-
return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/commits?sha='.urlencode($branchSha));
33+
return $this->get($url);
2934
}
3035

3136
/**

lib/Github/Api/Markdown.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ class Markdown extends Api
1313
/**
1414
* @param string $text
1515
* @param string $mode
16+
* @param string $context
1617
*
1718
* @return string
1819
*/
19-
public function render($text, $mode = 'markdown')
20+
public function render($text, $mode = 'markdown', $context = null)
2021
{
21-
if (!in_array($mode, array())) {
22+
if (!in_array($mode, array('gfm', 'markdown'))) {
2223
$mode = 'markdown';
2324
}
2425

25-
return $this->post('markdown', array(
26+
$params = array(
2627
'text' => $text,
2728
'mode' => $mode
28-
));
29+
);
30+
if (null !== $context && 'gfm' === $mode) {
31+
$params['context'] = $context;
32+
}
33+
34+
return $this->post('markdown', $params);
2935
}
3036

3137
/**

lib/Github/Client.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ class Client
4242
protected $httpClient = null;
4343

4444
/**
45-
* HTTP Headers
45+
* The list of loaded API instances
4646
*
4747
* @var array
4848
*/
49-
private $headers = array();
49+
protected $apis = array();
5050

5151
/**
52-
* The list of loaded API instances
52+
* HTTP Headers
5353
*
5454
* @var array
5555
*/
56-
protected $apis = array();
56+
private $headers = array();
5757

5858
/**
5959
* Instantiate a new GitHub client
@@ -113,7 +113,7 @@ public function get($path, array $parameters = array(), $requestOptions = array(
113113
* Call any path, POST method
114114
* Ex: $api->post('repos/show/my-username', array('email' => 'my-new-email@provider.org'))
115115
*
116-
* @param string $path the GitHub path
116+
* @param string $path the GitHub path
117117
* @param array $parameters POST parameters
118118
* @param array $requestOptions reconfigure the request
119119
* @return array data returned
@@ -295,6 +295,14 @@ public function getMarkdownApi()
295295
return $this->apis['markdown'];
296296
}
297297

298+
/**
299+
* @return mixed
300+
*/
301+
public function getRateLimit()
302+
{
303+
return $this->get('rate_limit');
304+
}
305+
298306
/**
299307
* Inject an API instance
300308
*

lib/Github/HttpClient/HttpClient.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
class HttpClient implements HttpClientInterface
1919
{
20+
/**
21+
* @var integer
22+
*/
23+
public $remainingCalls;
24+
2025
/**
2126
* The http client options
2227
* @var array
@@ -205,9 +210,9 @@ protected function decodeResponse($response)
205210
*/
206211
protected function checkApiLimit(MessageInterface $response)
207212
{
208-
$limit = $response->getHeader('X-RateLimit-Remaining');
213+
$this->remainingCalls = $response->getHeader('X-RateLimit-Remaining');
209214

210-
if (null !== $limit && 1 > $limit) {
215+
if (null !== $this->remainingCalls && 1 > $this->remainingCalls) {
211216
throw new ApiLimitExceedException($this->options['api_limit']);
212217
}
213218
}

test/Github/Tests/Api/CommitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class CommitTest extends ApiTestCase
88
{
9-
public function testGetBranchCommits()
9+
public function testGetInvalidBranchCommits()
1010
{
1111
$api = $this->getApiMock();
1212

@@ -18,7 +18,7 @@ public function testGetBranchCommits()
1818

1919
$api->expects($this->at(1))
2020
->method('get')
21-
->with($this->equalTo('repos/ornicar/php-github-api/commits?sha=123'))
21+
->with($this->equalTo('repos/ornicar/php-github-api/commits'))
2222
->will($this->returnValue($expectedValue));
2323

2424
$this->assertEquals($expectedValue, $api->getBranchCommits('ornicar', 'php-github-api', 'v3'));

test/Github/Tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testAuthenticate()
2828
$method = 'method';
2929

3030
$httpClient = $this->getHttpClientMock();
31-
$httpClient->expects($this->exactly(3))
31+
$httpClient->expects($this->exactly(2))
3232
->method('setOption')
3333
->will($this->returnValue($httpClient));
3434

0 commit comments

Comments
 (0)