Skip to content

Commit d11a367

Browse files
committed
Fix creating of Git Trees
1 parent 3867a94 commit d11a367

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

lib/Github/Api/AbstractApi.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,21 @@ protected function delete($path, array $parameters = array(), $requestOptions =
6666
{
6767
return $this->client->delete($path, $parameters, $requestOptions);
6868
}
69+
70+
/**
71+
* Translates array to object, for proper json data (forcing `[]` instead of `{}`)
72+
*
73+
* @param array $params
74+
*
75+
* @return object
76+
*/
77+
protected function translateArrayToObject(array $params)
78+
{
79+
$object = new \stdClass();
80+
foreach ($params as $key => $param) {
81+
$object->$key = $param;
82+
}
83+
84+
return $object;
85+
}
6986
}

lib/Github/Api/GitData/Trees.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ public function show($username, $repository, $sha, $recursive = false)
1818

1919
public function create($username, $repository, array $params)
2020
{
21-
if (!isset($params['tree'])) {
21+
if (!isset($params['tree']) || !is_array($params['tree'])) {
2222
throw new MissingArgumentException('tree');
2323
}
24-
if (!isset($params['tree']['path'], $params['tree']['mode'], $params['tree']['type'])) {
25-
throw new MissingArgumentException(array('tree.path', 'tree.mode', 'tree.type'));
24+
25+
if (!isset($params['tree'][0])) {
26+
$params['tree'] = array($params['tree']);
2627
}
2728

28-
// If `sha` is not set, `content` is required
29-
if (!isset($params['tree']['sha']) && !isset($params['tree']['content'])) {
30-
throw new MissingArgumentException('tree.content');
29+
foreach ($params['tree'] as $key => $tree) {
30+
if (!isset($tree['path'], $tree['mode'], $tree['type'])) {
31+
throw new MissingArgumentException(array("tree.$key.path", "tree.$key.mode", "tree.$key.type"));
32+
}
33+
34+
// If `sha` is not set, `content` is required
35+
if (!isset($tree['sha']) && !isset($tree['content'])) {
36+
throw new MissingArgumentException("tree.$key.content");
37+
}
38+
39+
$params['tree'][$key] = $this->translateArrayToObject($tree);
3140
}
3241

3342
return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/git/trees', $params);

test/Github/Tests/Api/AbstractApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Github\Api\AbstractApi;
66

77
/**
8-
* AbstractApi test case
8+
* AbstractApi test case
99
*
1010
* @author Leszek Prabucki <[email protected]>
1111
*/

test/Github/Tests/Api/GitData/TreesTest.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@ public function shouldCreateTreeUsingSha()
3535
$expectedValue = array('sha' => '123', 'comitter');
3636
$data = array(
3737
'tree' => array(
38-
'path' => 'path',
39-
'mode' => 'mode',
40-
'type' => 'type',
41-
'sha' => '1234'
38+
array(
39+
'path' => 'path',
40+
'mode' => 'mode',
41+
'type' => 'type',
42+
'sha' => '1234'
43+
),
44+
array(
45+
'path' => 'htap',
46+
'mode' => 'edom',
47+
'type' => 'epyt',
48+
'sha' => '4321'
49+
),
4250
)
4351
);
4452

@@ -59,10 +67,18 @@ public function shouldCreateTreeUsingContent()
5967
$expectedValue = array('sha' => '123', 'comitter');
6068
$data = array(
6169
'tree' => array(
62-
'path' => 'path',
63-
'mode' => 'mode',
64-
'type' => 'type',
65-
'content' => 'content'
70+
array(
71+
'path' => 'path',
72+
'mode' => 'mode',
73+
'type' => 'type',
74+
'content' => 'content'
75+
),
76+
array(
77+
'path' => 'htap',
78+
'mode' => 'edom',
79+
'type' => 'epyt',
80+
'content' => 'tnetnoc'
81+
),
6682
)
6783
);
6884

@@ -174,6 +190,23 @@ public function shouldNotCreateTreeWithoutTreeParam()
174190
$api->create('KnpLabs', 'php-github-api', $data);
175191
}
176192

193+
/**
194+
* @test
195+
* @expectedException Github\Exception\MissingArgumentException
196+
*/
197+
public function shouldNotCreateTreeWhenTreeParamIsNotArray()
198+
{
199+
$data = array(
200+
'tree' => ''
201+
);
202+
203+
$api = $this->getApiMock();
204+
$api->expects($this->never())
205+
->method('post');
206+
207+
$api->create('KnpLabs', 'php-github-api', $data);
208+
}
209+
177210
protected function getApiClass()
178211
{
179212
return 'Github\Api\GitData\Trees';

0 commit comments

Comments
 (0)