Skip to content

Commit 2474903

Browse files
committed
Initial project tests
1 parent 6babdcf commit 2474903

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
"ext-curl": "*",
2626
"kriswallsmith/buzz": ">=0.7"
2727
},
28+
"require-dev": {
29+
"phpunit/phpunit": "~4.5"
30+
},
2831
"autoload": {
2932
"psr-0": { "Gitlab\\": "lib/" }
3033
}

phpunit.xml.dist

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="test/bootstrap.php"
13+
>
14+
<testsuites>
15+
<testsuite name="php-gitlab-api Test Suite">
16+
<directory>./test/Gitlab/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<filter>
21+
<whitelist>
22+
<directory suffix=".php">./lib/Gitlab/</directory>
23+
</whitelist>
24+
</filter>
25+
</phpunit>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
use Gitlab\Api\AbstractApi;
4+
5+
class ProjectsTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldGetAllProjects()
11+
{
12+
$expectedArray = $this->getMultipleProjectsData();
13+
14+
$api = $this->getMultipleProjectsRequestMock('projects/all', $expectedArray);
15+
16+
$this->assertEquals($expectedArray, $api->all(1, 10));
17+
}
18+
19+
/**
20+
* @test
21+
*/
22+
public function shouldNotNeedPaginationWhenGettingProjects()
23+
{
24+
$expectedArray = $this->getMultipleProjectsData();
25+
26+
$api = $this->getApiMock();
27+
$api->expects($this->once())
28+
->method('get')
29+
->with('projects/all', array('page' => 1, 'per_page' => AbstractApi::PER_PAGE))
30+
->will($this->returnValue($expectedArray))
31+
;
32+
33+
$this->assertEquals($expectedArray, $api->all());
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function shouldGetAccessibleProjects()
40+
{
41+
$expectedArray = $this->getMultipleProjectsData();
42+
43+
$api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, 2, 7);
44+
45+
$this->assertEquals($expectedArray, $api->accessible(2, 7));
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function shouldGetOwnedProjects()
52+
{
53+
$expectedArray = $this->getMultipleProjectsData();
54+
55+
$api = $this->getMultipleProjectsRequestMock('projects/owned', $expectedArray, 3, 50);
56+
57+
$this->assertEquals($expectedArray, $api->owned(3, 50));
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function shouldSearchProjects()
64+
{
65+
$expectedArray = $this->getMultipleProjectsData();
66+
67+
$api = $this->getMultipleProjectsRequestMock('projects/search/a+project', $expectedArray);
68+
69+
$this->assertEquals($expectedArray, $api->search('a project', 1, 10));
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function shouldShowProject()
76+
{
77+
$expectedArray = array('id' => 1, 'name' => 'Project Name');
78+
79+
$api = $this->getApiMock();
80+
$api->expects($this->once())
81+
->method('get')
82+
->with('projects/1')
83+
->will($this->returnValue($expectedArray))
84+
;
85+
86+
$this->assertEquals($expectedArray, $api->show(1));
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function shouldCreateProject()
93+
{
94+
$expectedArray = array('id' => 1, 'name' => 'Project Name');
95+
96+
$api = $this->getApiMock();
97+
$api->expects($this->once())
98+
->method('post')
99+
->with('projects', array('name' => 'Project Name', 'issues_enabled' => true))
100+
->will($this->returnValue($expectedArray))
101+
;
102+
103+
$this->assertEquals($expectedArray, $api->create('Project Name', array(
104+
'issues_enabled' => true
105+
)));
106+
}
107+
108+
protected function getMultipleProjectsRequestMock($path, $expectedArray = array(), $page = 1, $per_page = 10)
109+
{
110+
$api = $this->getApiMock();
111+
$api->expects($this->once())
112+
->method('get')
113+
->with($path, array('page' => $page, 'per_page' => $per_page))
114+
->will($this->returnValue($expectedArray))
115+
;
116+
117+
return $api;
118+
}
119+
120+
protected function getMultipleProjectsData()
121+
{
122+
return array(
123+
array('id' => 1, 'name' => 'A project'),
124+
array('id' => 2, 'name' => 'Another project')
125+
);
126+
}
127+
128+
protected function getApiClass()
129+
{
130+
return 'Gitlab\Api\Projects';
131+
}
132+
}

test/Gitlab/Tests/Api/TestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
use Gitlab\Client;
4+
5+
abstract class TestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
abstract protected function getApiClass();
8+
9+
protected function getApiMock()
10+
{
11+
$httpClient = $this->getMock('Buzz\Client\Curl', array('send'));
12+
$httpClient
13+
->expects($this->any())
14+
->method('send');
15+
16+
$mock = $this->getMock('Gitlab\HttpClient\HttpClient', array(), array(null, array(), $httpClient));
17+
18+
$client = new Client($mock);
19+
$client->setHttpClient($mock);
20+
21+
return $this->getMockBuilder($this->getApiClass())
22+
->setMethods(array('get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'))
23+
->setConstructorArgs(array($client))
24+
->getMock()
25+
;
26+
}
27+
}

test/bootstrap.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
function includeIfExists($file)
4+
{
5+
if (file_exists($file)) {
6+
return include $file;
7+
}
8+
}
9+
10+
if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../.composer/autoload.php'))) {
11+
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
12+
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
13+
'php composer.phar install'.PHP_EOL);
14+
}
15+
16+
$loader->add('Gitlab\Tests', __DIR__);
17+
18+
return $loader;

0 commit comments

Comments
 (0)