Skip to content

Commit 32fc16c

Browse files
author
Alexander Obuhovich
committed
Add some basic tests for client classes
1 parent 5dc5e5b commit 32fc16c

File tree

11 files changed

+166
-8
lines changed

11 files changed

+166
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
2+
phpunit.xml
23
composer.lock
34
.idea

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ php:
1717
install:
1818
- composer install --prefer-dist
1919

20+
before_script:
21+
- ~/.phpenv/versions/5.4/bin/php -S localhost:8000 -t $(pwd) > /dev/null 2> /tmp/webserver_output.txt &
22+
- export REPO_URL=http://localhost:8000/
23+
2024
script:
2125
- ./vendor/bin/phpunit
26+
27+
after_failure:
28+
- cat /tmp/webserver_output.txt

CONTRIBUTING.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ JIRA REST API Client is an open source, community-driven project. If you'd like
1515
7. Create Pull Request.
1616

1717
## Running the Tests
18-
Make sure that you don't break anything with your changes by running:
18+
19+
1. make sure repository is located in web server document root (or it's sub-folder)
20+
2. copy `phpunit.xml.dist` file into `phpunit.xml` file
21+
3. in the `phpunit.xml` file:
22+
* uncomment part, where `REPO_URL` environment variable is defined
23+
* set `REPO_URL` environment variable value to URL, from where repository can be accessed (e.g. `http://localhost/path/to/repository/`)
24+
4. run following command to make sure that you don't break anything with your changes:
1925

2026
```bash
2127
$> phpunit

phpunit.xml.dist

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<phpunit backupGlobals="true"
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="true"
34
backupStaticAttributes="false"
45
colors="true"
56
convertErrorsToExceptions="true"
@@ -13,13 +14,17 @@
1314
syntaxCheck="true"
1415
strict="true"
1516
verbose="true">
16-
<php>
17-
<ini name="date.timezone" value="Asia/Tokyo" />
18-
</php>
17+
1918
<testsuites>
2019
<testsuite name="Jira_Api Test Suite">
2120
<directory>tests</directory>
2221
</testsuite>
2322
</testsuites>
2423

25-
</phpunit>
24+
<!--
25+
<php>
26+
<server name="REPO_URL" value="http://localhost/"/>
27+
</php>
28+
-->
29+
30+
</phpunit>

src/Jira/Api/Client/ClientInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ interface ClientInterface
4242
* @param boolean $debug Debug this request.
4343
*
4444
* @return array|string
45+
* @throws \InvalidArgumentException When data is not an array and http method is GET.
4546
*/
4647
public function sendRequest(
4748
$method,

src/Jira/Api/Client/CurlClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ public function sendRequest(
7575
$curl = curl_init();
7676

7777
if ( $method == 'GET' ) {
78-
$url .= '?' . http_build_query($data);
79-
8078
if ( !is_array($data) ) {
8179
throw new \InvalidArgumentException('Data must be an array.');
8280
}
81+
82+
$url .= '?' . http_build_query($data);
8383
}
8484

8585
curl_setopt($curl, CURLOPT_URL, $endpoint . $url);

src/Jira/Api/Client/PHPClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ protected function isHttpsSupported()
7676
*
7777
* @return array|string
7878
* @throws \Exception When non-supported implementation of AuthenticationInterface is given.
79+
* @throws \InvalidArgumentException When data is not an array and http method is GET.
7980
*/
8081
public function sendRequest(
8182
$method,
@@ -130,6 +131,10 @@ public function sendRequest(
130131
$context['http']['content'] = $__data;
131132
}
132133
else {
134+
if ( !is_array($data) ) {
135+
throw new \InvalidArgumentException('Data must be an array.');
136+
}
137+
133138
$url .= '?' . http_build_query($data);
134139
}
135140

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Tests\chobie\Jira\Api\Client;
4+
5+
6+
use chobie\Jira\Api\Authentication\Anonymous;
7+
use chobie\Jira\Api\Client\ClientInterface;
8+
9+
abstract class AbstractClientTestCase extends \PHPUnit_Framework_TestCase
10+
{
11+
12+
/**
13+
* Client.
14+
*
15+
* @var ClientInterface
16+
*/
17+
protected $client;
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
if ( empty($_SERVER['REPO_URL']) ) {
24+
$this->markTestSkipped('The "REPO_URL" environment variable not set.');
25+
}
26+
27+
$this->client = $this->createClient();
28+
}
29+
30+
public function testGetRequestDataAsArray()
31+
{
32+
$query_params = array('param1' => 'value1', 'param2' => 'value2');
33+
$trace_result = $this->traceRequest('GET', $query_params);
34+
35+
$this->assertEquals($query_params, $trace_result['_GET']);
36+
}
37+
38+
/**
39+
* @expectedException \InvalidArgumentException
40+
* @expectedExceptionMessage Data must be an array.
41+
*/
42+
public function testGetRequestDataAsString()
43+
{
44+
$this->traceRequest('GET', 'param1=value1&param2=value2');
45+
}
46+
47+
/**
48+
* Traces a request.
49+
*
50+
* @param string $method Request method.
51+
* @param array $data Request data.
52+
* @param boolean $is_file This is a file upload request.
53+
* @param boolean $debug Debug this request.
54+
*
55+
* @return array
56+
*/
57+
protected function traceRequest($method, $data = array(), $is_file = false, $debug = false)
58+
{
59+
$result = $this->client->sendRequest(
60+
$method,
61+
'/tests/debug_response.php',
62+
$data,
63+
rtrim($_SERVER['REPO_URL'], '/'),
64+
new Anonymous(),
65+
$is_file,
66+
$debug
67+
);
68+
69+
return unserialize($result);
70+
}
71+
72+
/**
73+
* Creates client.
74+
*
75+
* @return ClientInterface
76+
*/
77+
abstract protected function createClient();
78+
79+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests\chobie\Jira\Api\Client;
4+
5+
6+
use chobie\Jira\Api\Client\ClientInterface;
7+
use chobie\Jira\Api\Client\CurlClient;
8+
9+
class CurlClientTest extends AbstractClientTestCase
10+
{
11+
12+
/**
13+
* Creates client.
14+
*
15+
* @return ClientInterface
16+
*/
17+
protected function createClient()
18+
{
19+
return new CurlClient();
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests\chobie\Jira\Api\Client;
4+
5+
6+
use chobie\Jira\Api\Client\ClientInterface;
7+
use chobie\Jira\Api\Client\PHPClient;
8+
9+
class PHPClientTest extends AbstractClientTestCase
10+
{
11+
12+
/**
13+
* Creates client.
14+
*
15+
* @return ClientInterface
16+
*/
17+
protected function createClient()
18+
{
19+
return new PHPClient();
20+
}
21+
22+
}

0 commit comments

Comments
 (0)