Skip to content

Commit

Permalink
refactor JWTOptions::__construct()
Browse files Browse the repository at this point in the history
  • Loading branch information
dshovchko committed Nov 27, 2017
1 parent 636f13d commit 7ce75ff
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/JWTOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class JWTOptions
/**
* @var array
*/
protected $jwt=array(
protected $jwt = array(
'using' => false,
'UAPAY_pubkey' => '',
'our_privkey' => '',
Expand All @@ -25,30 +25,44 @@ public function __construct($options)
{
if (isset($options['jwt']))
{
// using
if ( ! isset($options['jwt']['using']))
{
throw new Exception\Data('parameter jwt/using is not specified');
}
if ( ! is_bool($options['jwt']['using']))
{
throw new Exception\Data('parameter jwt/using is incorrect');
}
// using
if ( ! isset($options['jwt']['UAPAY_pubkey']))
{
throw new Exception\Data('parameter jwt/UAPAY_pubkey is not specified');
}
// using
if ( ! isset($options['jwt']['our_privkey']))
{
throw new Exception\Data('parameter jwt/our_privkey is not specified');
}
$this->is_valid_using($options);
$this->is_present_option($options, 'UAPAY_pubkey');
$this->is_present_option($options, 'our_privkey');

$this->jwt = $options['jwt'];
}
}

/**
* Check is valid 'using' option
*
* @param array $options
* @throws Exception\Data
*/
protected function is_valid_using($options)
{
$this->is_present_option($options, 'using');
if ( ! is_bool($options['jwt']['using']))
{
throw new Exception\Data('parameter jwt/using is incorrect');
}
}

/**
* Check is present option
*
* @param array $options
* @param string $name
* @throws Exception\Data
*/
protected function is_present_option($options, $name)
{
if ( ! isset($options['jwt'][$name]))
{
throw new Exception\Data('parameter jwt/'.$name.' is not specified');
}
}

/**
* Get jwt options
*
Expand Down
52 changes: 52 additions & 0 deletions test/JWTOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,58 @@ public function test_constructor_no_jwt_our_privkey()
$jo = new JWTOptions(array('api_uri'=>'localhost', 'jwt'=>array('using'=>true, 'UAPAY_pubkey'=>'')));
}

public function test_is_valid_using()
{
$options = array(
'api_uri'=>'localhost',
'jwt'=>array('using'=>true, 'UAPAY_pubkey'=>'public', 'our_privkey'=>'private')
);
$jo = new JWTOptions($options);
$payload = $this->invokeMethod($jo, 'is_valid_using', array($options));
}

/**
* @expectedException UAPAY\Exception\Data
* @expectedExceptionMessage parameter jwt/using is incorrect
*/
public function test_is_valid_using_bad_type()
{
$options = array(
'api_uri'=>'localhost',
'jwt'=>array('using'=>true, 'UAPAY_pubkey'=>'public', 'our_privkey'=>'private')
);
$jo = new JWTOptions($options);

$options['jwt']['using'] = 1234;
$payload = $this->invokeMethod($jo, 'is_valid_using', array($options));
}

public function test_is_present_option()
{
$options = array(
'api_uri'=>'localhost',
'jwt'=>array('using'=>true, 'UAPAY_pubkey'=>'public', 'our_privkey'=>'private')
);
$jo = new JWTOptions($options);
$payload = $this->invokeMethod($jo, 'is_present_option', array($options, 'our_privkey'));
}

/**
* @expectedException UAPAY\Exception\Data
* @expectedExceptionMessage parameter jwt/our_privkey is not specified
*/
public function test_is_present_option_not_present()
{
$options = array(
'api_uri'=>'localhost',
'jwt'=>array('using'=>true, 'UAPAY_pubkey'=>'public', 'our_privkey'=>'private')
);
$jo = new JWTOptions($options);

unset($options['jwt']['our_privkey']);
$payload = $this->invokeMethod($jo, 'is_present_option', array($options, 'our_privkey'));
}

public function test_get()
{
$jo = new JWTOptions(array(
Expand Down

0 comments on commit 7ce75ff

Please sign in to comment.