diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php index 0f361c23f65..5ed185b362b 100644 --- a/lib/Github/Api/Repository/Contents.php +++ b/lib/Github/Api/Repository/Contents.php @@ -5,6 +5,7 @@ use Github\Api\AbstractApi; use Github\Exception\InvalidArgumentException; use Github\Exception\ErrorException; +use Github\Exception\MissingArgumentException; /** * @link http://developer.github.com/v3/repos/contents/ @@ -62,10 +63,13 @@ public function show($username, $repository, $path = null, $reference = null) * @param string $content contents of the new file * @param string $message the commit message * @param null|string $branch name of a branch + * @param null|array $committer information about the committer + * + * @throws MissingArgumentException * * @return array information about the new file */ - public function create($username, $repository, $path, $content, $message, $branch = null) + public function create($username, $repository, $path, $content, $message, $branch = null, array $committer = null) { $url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path); @@ -78,6 +82,13 @@ public function create($username, $repository, $path, $content, $message, $branc $parameters['branch'] = $branch; } + if (null !== $committer) { + if (!isset($committer['name'], $committer['email'])) { + throw new MissingArgumentException(array('name', 'email')); + } + $parameters['committer'] = $committer; + } + return $this->put($url, $parameters); } @@ -92,10 +103,13 @@ public function create($username, $repository, $path, $content, $message, $branc * @param string $message the commit message * @param string $sha blob SHA of the file being replaced * @param null|string $branch name of a branch + * @param null|array $committer information about the committer + * + * @throws MissingArgumentException * * @return array information about the updated file */ - public function update($username, $repository, $path, $content, $message, $sha, $branch = null) + public function update($username, $repository, $path, $content, $message, $sha, $branch = null, array $committer = null) { $url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path); @@ -109,6 +123,13 @@ public function update($username, $repository, $path, $content, $message, $sha, $parameters['branch'] = $branch; } + if (null !== $committer) { + if (!isset($committer['name'], $committer['email'])) { + throw new MissingArgumentException(array('name', 'email')); + } + $parameters['committer'] = $committer; + } + return $this->put($url, $parameters); } @@ -123,10 +144,13 @@ public function update($username, $repository, $path, $content, $message, $sha, * @param string $message the commit message * @param string $sha blob SHA of the file being deleted * @param null|string $branch name of a branch + * @param null|array $committer information about the committer + * + * @throws MissingArgumentException * * @return array information about the updated file */ - public function rm($username, $repository, $path, $message, $sha, $branch = null) + public function rm($username, $repository, $path, $message, $sha, $branch = null, array $committer = null) { $url = 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents/'.rawurlencode($path); @@ -139,6 +163,13 @@ public function rm($username, $repository, $path, $message, $sha, $branch = null $parameters['branch'] = $branch; } + if (null !== $committer) { + if (!isset($committer['name'], $committer['email'])) { + throw new MissingArgumentException(array('name', 'email')); + } + $parameters['committer'] = $committer; + } + return $this->delete($url, $parameters); } diff --git a/test/Github/Tests/Api/Repository/ContentsTest.php b/test/Github/Tests/Api/Repository/ContentsTest.php index 0eb91a4cd15..0f74f237fb8 100644 --- a/test/Github/Tests/Api/Repository/ContentsTest.php +++ b/test/Github/Tests/Api/Repository/ContentsTest.php @@ -38,6 +38,122 @@ public function shouldShowReadme() $this->assertEquals($expectedValue, $api->readme('KnpLabs', 'php-github-api')); } + /** + * @test + */ + public function shouldCreateNewFile() + { + $expectedArray = array('content' => 'some data'); + $content = ' 'committer name', 'email' => 'email@example.com'); + $parameters = array( + 'content' => base64_encode($content), + 'message' => $message, + 'committer' => $committer, + 'branch' => $branch, + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->create('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $content, $message, $branch, $committer)); + } + + /** + * @test + * @expectedException Github\Exception\MissingArgumentException + * @expectedExceptionMessage One or more of required ("name", "email") parameters is missing! + */ + public function shouldThrowExceptionWhenCreateNewFileWithInvalidCommitter() + { + $committer = array('invalid_key' => 'some data'); + $api = $this->getApiMock(); + $api->create('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'some content', 'a commit message', null, $committer); + } + + /** + * @test + */ + public function shouldUpdateFile() + { + $expectedArray = array('content' => 'some data'); + $content = ' 'committer name', 'email' => 'email@example.com'); + $parameters = array( + 'content' => base64_encode($content), + 'message' => $message, + 'committer' => $committer, + 'branch' => $branch, + 'sha' => $sha, + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->update('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $content, $message, $sha, $branch, $committer)); + } + + /** + * @test + * @expectedException Github\Exception\MissingArgumentException + * @expectedExceptionMessage One or more of required ("name", "email") parameters is missing! + */ + public function shouldThrowExceptionWhenUpdateFileWithInvalidCommitter() + { + $committer = array('invalid_key' => 'some data'); + $api = $this->getApiMock(); + $api->update('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'some content', 'a commit message', null, null, $committer); + } + + /** + * @test + */ + public function shouldDeleteFile() + { + $expectedArray = array('content' => 'some data'); + $message = 'a commit message'; + $sha = 'a sha'; + $branch = 'master'; + $committer = array('name' => 'committer name', 'email' => 'email@example.com'); + $parameters = array( + 'message' => $message, + 'committer' => $committer, + 'branch' => $branch, + 'sha' => $sha, + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', $parameters) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->rm('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', $message, $sha, $branch, $committer)); + } + + /** + * @test + * @expectedException Github\Exception\MissingArgumentException + * @expectedExceptionMessage One or more of required ("name", "email") parameters is missing! + */ + public function shouldThrowExceptionWhenDeleteFileWithInvalidCommitter() + { + $committer = array('invalid_key' => 'some data'); + $api = $this->getApiMock(); + $api->rm('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php', 'a commit message', null, null, $committer); + } + /** * @test */