Skip to content

Commit d64c116

Browse files
authored
Merge pull request #429 from morinluc0/355-wiki-support
Created the api and models for the Wiki endpoint
2 parents 5918788 + f151c37 commit d64c116

File tree

4 files changed

+263
-1
lines changed

4 files changed

+263
-1
lines changed

lib/Gitlab/Api/Wiki.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php namespace Gitlab\Api;
2+
3+
class Wiki extends AbstractApi
4+
{
5+
6+
/**
7+
* @param int $project_id
8+
* @param array $params
9+
* @return mixed
10+
*/
11+
public function create($project_id, array $params)
12+
{
13+
return $this->post($this->getProjectPath($project_id, 'wikis'), $params);
14+
}
15+
16+
/**
17+
* @param int $project_id
18+
* @param int $wiki_slug
19+
* @return mixed
20+
*/
21+
public function show($project_id, $wiki_slug)
22+
{
23+
return $this->get($this->getProjectPath($project_id, 'wikis/'.$this->encodePath($wiki_slug)));
24+
}
25+
26+
/**
27+
* @param int $project_id
28+
* @return mixed
29+
*/
30+
public function showAll($project_id)
31+
{
32+
return $this->get($this->getProjectPath($project_id, 'wikis'));
33+
}
34+
35+
/**
36+
* @param int $project_id
37+
* @param array $params
38+
* @return mixed
39+
*/
40+
public function update($project_id, $wiki_slug, array $params)
41+
{
42+
return $this->put($this->getProjectPath($project_id, 'wikis/'.$this->encodePath($wiki_slug)), $params);
43+
}
44+
45+
/**
46+
* @param int $project_id
47+
* @param int $wiki_slug
48+
* @return mixed
49+
*/
50+
public function remove($project_id, $wiki_slug)
51+
{
52+
return $this->delete($this->getProjectPath($project_id, 'wikis/'.$this->encodePath($wiki_slug)));
53+
}
54+
}

lib/Gitlab/Client.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ public function schedules()
305305
return new Api\Schedules($this);
306306
}
307307

308+
/**
309+
* @return Api\Wiki
310+
*/
311+
public function wiki()
312+
{
313+
return new Api\Wiki($this);
314+
}
315+
308316
/**
309317
* @return Api\IssuesStatistics
310318
*/
@@ -398,10 +406,12 @@ public function api($name)
398406
case 'schedules':
399407
return $this->schedules();
400408

409+
case 'wiki':
410+
return $this->wiki();
411+
401412
case 'issues_statistics':
402413
return $this->issuesStatistics();
403414

404-
405415
default:
406416
throw new InvalidArgumentException('Invalid endpoint: "'.$name.'"');
407417
}

lib/Gitlab/Model/Wiki.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
use Gitlab\Client;
6+
7+
/**
8+
* Class Wiki
9+
*
10+
* @property-read string $slug
11+
* @property-read string $title
12+
* @property-read string $format
13+
* @property-read string $content
14+
* @property-read Project $project
15+
*/
16+
class Wiki extends AbstractModel
17+
{
18+
/**
19+
* @var array
20+
*/
21+
protected static $properties = array(
22+
"project",
23+
"slug",
24+
"title",
25+
"format",
26+
"content",
27+
);
28+
29+
/**
30+
* @param Client $client
31+
* @param Project $project
32+
* @param array $data
33+
* @return Wiki
34+
*/
35+
public static function fromArray(Client $client, Project $project, array $data)
36+
{
37+
$wiki = new static($project, $data['slug'], $client);
38+
39+
return $wiki->hydrate($data);
40+
}
41+
42+
/**
43+
* @param Project $project
44+
* @param string $slug
45+
* @param Client $client
46+
*/
47+
public function __construct(Project $project, $slug = null, Client $client = null)
48+
{
49+
$this->setClient($client);
50+
$this->setData('project', $project);
51+
$this->setData('slug', $slug);
52+
}
53+
54+
/**
55+
* @return Wiki
56+
*/
57+
public function show()
58+
{
59+
$data = $this->client->wiki()->show($this->project->id, $this->slug);
60+
61+
return static::fromArray($this->getClient(), $this->project, $data);
62+
}
63+
64+
/**
65+
* @param array $params
66+
* @return Wiki
67+
*/
68+
public function update(array $params)
69+
{
70+
$data = $this->client->wiki()->update($this->project->id, $this->slug, $params);
71+
72+
return static::fromArray($this->getClient(), $this->project, $data);
73+
}
74+
}

test/Gitlab/Tests/Api/WikiTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Gitlab\Tests\Api;
4+
5+
class WikiTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldCreateWiki()
11+
{
12+
$expectedArray = [
13+
"format" => "markdown",
14+
"slug" => "Test-Wiki",
15+
"title" => "Test Wiki",
16+
"content" => "This is the test Wiki",
17+
];
18+
19+
20+
$api = $this->getApiMock();
21+
$api->expects($this->once())
22+
->method('post')
23+
->with('projects/1/wikis', [
24+
"format" => "markdown",
25+
"title" => "Test Wiki",
26+
"content" => "This is the test Wiki"
27+
])
28+
->will($this->returnValue($expectedArray));
29+
30+
$this->assertEquals($expectedArray, $api->create(1,
31+
[
32+
"format" => "markdown",
33+
"title" => "Test Wiki",
34+
"content" => "This is the test Wiki"
35+
]
36+
));
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function shouldShowWiki()
43+
{
44+
$expectedArray = [
45+
"slug" => "Test-Wiki",
46+
"title" => "Test Wiki",
47+
"format" => "markdown"
48+
];
49+
50+
$api = $this->getApiMock();
51+
$api->expects($this->once())
52+
->method('get')
53+
->with('projects/1/wikis/Test-Wiki')
54+
->will($this->returnValue($expectedArray));
55+
56+
$this->assertEquals($expectedArray, $api->show(1, "Test-Wiki"));
57+
}
58+
59+
/**
60+
* @test
61+
*/
62+
public function shouldShowAllWiki()
63+
{
64+
$expectedArray = [
65+
"slug" => "Test-Wiki",
66+
"title" => "Test Wiki",
67+
"format" => "markdown"
68+
];
69+
70+
$api = $this->getApiMock();
71+
$api->expects($this->once())
72+
->method('get')
73+
->with('projects/1/wikis')
74+
->will($this->returnValue($expectedArray))
75+
;
76+
77+
$this->assertEquals($expectedArray, $api->showAll(1));
78+
}
79+
80+
/**
81+
* @test
82+
*/
83+
public function shouldUpdateWiki()
84+
{
85+
$expectedArray = [
86+
'slug' => 'Test-Wiki',
87+
'title' => 'Test Wiki',
88+
"format" => "markdown",
89+
"content" => "This is the test Wiki that has been updated"
90+
];
91+
92+
$api = $this->getApiMock();
93+
$api->expects($this->once())
94+
->method('put')
95+
->with('projects/1/wikis/Test-Wiki', ["content" => "This is the test Wiki that has been updated"])
96+
->will($this->returnValue($expectedArray))
97+
;
98+
99+
$this->assertEquals($expectedArray, $api->update(1, "Test-Wiki", ["content" => "This is the test Wiki that has been updated"]));
100+
}
101+
102+
/**
103+
* @test
104+
*/
105+
public function shouldRemoveWiki()
106+
{
107+
$expectedBool = true;
108+
109+
$api = $this->getApiMock();
110+
$api->expects($this->once())
111+
->method('delete')
112+
->with('projects/1/wikis/Test-Wiki')
113+
->will($this->returnValue($expectedBool))
114+
;
115+
116+
$this->assertEquals($expectedBool, $api->remove(1, "Test-Wiki"));
117+
}
118+
119+
120+
protected function getApiClass()
121+
{
122+
return 'Gitlab\Api\Wiki';
123+
}
124+
}

0 commit comments

Comments
 (0)