Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default value to getParameter #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/Common/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ abstract class AbstractGateway implements GatewayInterface
/**
* Create a new gateway instance
*
* @param ClientInterface $httpClient A HTTP client to make API calls with
* @param HttpRequest $httpRequest A Symfony HTTP request object
* @param ClientInterface $httpClient A HTTP client to make API calls with
* @param HttpRequest $httpRequest A Symfony HTTP request object
*/
public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
{
Expand All @@ -84,7 +84,7 @@ public function getShortName()
/**
* Initialize this gateway with default parameters
*
* @param array $parameters
* @param array $parameters
* @return $this
*/
public function initialize(array $parameters = array())
Expand Down Expand Up @@ -114,17 +114,18 @@ public function getDefaultParameters()
}

/**
* @param string $key
* @param string $key
* @param null $default
* @return mixed
*/
public function getParameter($key)
public function getParameter($key, $default = null)
{
return $this->traitGetParameter($key);
return $this->traitGetParameter($key, $default);
}

/**
* @param string $key
* @param mixed $value
* @param string $key
* @param mixed $value
* @return $this
*/
public function setParameter($key, $value)
Expand All @@ -141,7 +142,7 @@ public function getTestMode()
}

/**
* @param boolean $value
* @param boolean $value
* @return $this
*/
public function setTestMode($value)
Expand All @@ -158,7 +159,7 @@ public function getCurrency()
}

/**
* @param string $value
* @param string $value
* @return $this
*/
public function setCurrency($value)
Expand Down
9 changes: 5 additions & 4 deletions src/Common/ParametersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ protected function setParameter($key, $value)
/**
* Get one parameter.
*
* @param string $key Parameter key
* @param string $key Parameter key
* @param null $default
* @return mixed A single parameter value.
*/
protected function getParameter($key)
protected function getParameter($key, $default = null)
{
return $this->parameters->get($key);
return $this->parameters->get($key, $default);
}

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ public function validate(...$args)
{
foreach ($args as $key) {
$value = $this->parameters->get($key);
if (! isset($value)) {
if (!isset($value)) {
throw new InvalidRequestException("The $key parameter is required");
}
}
Expand Down
21 changes: 19 additions & 2 deletions tests/Omnipay/Common/AbstractGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public function testSetSetParameter()
$this->assertEquals($token, $this->gateway->getParameter('token'));
}

public function testGetParameterWithDefaultValue()
{
$default = 'foo';

$this->assertEquals($default, $this->gateway->getParameter('foobar', $default));
}

public function testGetParameterWithoutDefaultValueIsEqualsToNull()
{
$this->assertEquals(null, $this->gateway->getParameter('foobar'));
}

public function testTestMode()
{
$this->assertSame($this->gateway, $this->gateway->setTestMode(true));
Expand Down Expand Up @@ -195,6 +207,11 @@ public function callCreateRequest($class, array $parameters)

class AbstractGatewayTest_MockAbstractRequest extends AbstractRequest
{
public function getData() {}
public function sendData($data) {}
public function getData()
{
}

public function sendData($data)
{
}
}