Skip to content

Commit 451e8c4

Browse files
committed
updated validation for BranchRestrictions::update()
1 parent b5b1cfc commit 451e8c4

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/Bitbucket/API/Repositories/BranchRestrictions.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function all($account, $repo)
4242
* @access public
4343
* @param string $account The team or individual account owning the repository.
4444
* @param string $repo The repository identifier.
45-
* @param array $params Additional parameters
45+
* @param array|string $params Additional parameters as array or JSON string
4646
* @return MessageInterface
4747
*
4848
* @throws \InvalidArgumentException
@@ -98,15 +98,19 @@ public function get($account, $repo, $id)
9898
* @param string $account The team or individual account owning the repository.
9999
* @param string $repo The repository identifier.
100100
* @param int $id The restriction's identifier.
101-
* @param array $params Additional parameters
101+
* @param array|string $params Additional parameters as array or JSON string
102102
* @return MessageInterface
103103
*
104104
* @throws \InvalidArgumentException
105105
*/
106106
public function update($account, $repo, $id, $params = array())
107107
{
108108
// allow developer to directly specify params as json if (s)he wants.
109-
if (!empty($params) && is_string($params)) {
109+
if ('array' !== gettype($params)) {
110+
if (empty($params)) {
111+
throw new \InvalidArgumentException('Invalid JSON provided.');
112+
}
113+
110114
$params = $this->decodeJSON($params);
111115
}
112116

test/Bitbucket/Tests/API/Repositories/BranchRestrictionsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ public function testUpdateRestrictionFromJSON()
162162
$restriction->update('gentle', 'eof', 1, $params);
163163
}
164164

165+
/**
166+
* @expectedException \InvalidArgumentException
167+
*/
168+
public function testUpdateRestrictionWithInvalidParams()
169+
{
170+
/** @var \Bitbucket\API\Repositories\BranchRestrictions $restrictions */
171+
$restrictions = $this->getApiMock('Bitbucket\API\Repositories\BranchRestrictions');
172+
173+
$restrictions->update('gentle', 'eof', 1, '');
174+
$restrictions->update('gentle', 'eof', 1, 3);
175+
$restrictions->update('gentle', 'eof', 1, "{ 'foo': 'bar' }");
176+
}
177+
165178
/**
166179
* @expectedException \InvalidArgumentException
167180
*/

0 commit comments

Comments
 (0)