diff --git a/src/JWTOptions.php b/src/JWTOptions.php index df36dad..c4659a5 100644 --- a/src/JWTOptions.php +++ b/src/JWTOptions.php @@ -9,7 +9,7 @@ class JWTOptions /** * @var array */ - protected $jwt=array( + protected $jwt = array( 'using' => false, 'UAPAY_pubkey' => '', 'our_privkey' => '', @@ -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 * diff --git a/test/JWTOptionsTest.php b/test/JWTOptionsTest.php index c96cd1f..d79fc7b 100644 --- a/test/JWTOptionsTest.php +++ b/test/JWTOptionsTest.php @@ -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(