Skip to content

Commit 42e0b5e

Browse files
committed
Add default options to constructor, plus new tests
1 parent fc5dcc1 commit 42e0b5e

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/TextRuApi.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ class TextRuApi
1212

1313
private static $allowed_options_get = ["exceptdomain", "excepturl", "visible", "copying", "callback"];
1414

15-
private function __construct($userkey)
15+
public function __construct($userkey, $default_options = [])
1616
{
1717
$this->userkey = $userkey;
18+
19+
foreach ($default_options as $key => $value) {
20+
if (!in_array($key, self::$allowed_options_get)) throw new WrongParameterException("Unknown option " . $key . " provided", 400122);
21+
}
22+
$this->default_options = $default_options;
1823
}
1924

2025
public function userkey($userkey = null)
@@ -33,7 +38,7 @@ public static function add($userkey, $text, $options = [])
3338
if (!is_array($options)) throw new WrongParameterException("Options param must be array", 400124);
3439

3540
foreach ($options as $key => $value) {
36-
if (!in_array($key, $this::$allowed_options_get)) throw new WrongParameterException("Unknown option " . $key . " provided", 400125);
41+
if (!in_array($key, self::$allowed_options_get)) throw new WrongParameterException("Unknown option " . $key . " provided", 400125);
3742
}
3843

3944
$post_options = ["userkey" => $userkey, "text" => $text];
@@ -54,6 +59,11 @@ public static function add($userkey, $text, $options = [])
5459

5560
}
5661

62+
public function adds($text, $options = [])
63+
{
64+
return $this::add($this->userkey, $text, $options);
65+
}
66+
5767
public static function get($userkey, $uid, $jsonvisible = null)
5868
{
5969
if ((empty($userkey)) || (empty($uid))) throw new WrongParameterException("Required params is empty", 400131);
@@ -82,6 +92,11 @@ public static function get($userkey, $uid, $jsonvisible = null)
8292
return $result;
8393
}
8494

95+
public static function gets($uid, $jsonvisible = null)
96+
{
97+
return $this::add($this->userkey, $uid, $jsonvisible);
98+
}
99+
85100
public static function sendCurl($postfields, $url = 'http://api.text.ru/post')
86101
{
87102
if (is_array($postfields)) $postfields = http_build_query($postfields, '', '&');

tests/AddMethodTest.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@ public function test_empty_userkey_rise_exception()
1616
{
1717
$this->expectException(WrongParameterException::class);
1818
$this->expectExceptionCode(400123);
19-
TextRuApi::add("","Test text");
19+
TextRuApi::add("", "Test text");
2020
}
2121

2222
public function test_too_short_text()
2323
{
24-
$result = TextRuApi::add("afldkfjlas","Short test text");
25-
$this->assertEquals($result["error"]["code"],112);
24+
$result = TextRuApi::add("afldkfjlas", "Short test text");
25+
$this->assertEquals($result["error"]["code"], 112);
2626
}
2727

2828
public function test_wrong_userkey()
2929
{
30-
$result = TextRuApi::add("php_unit_test","Test test test test test test test test test test test test test test test test test test test test test");
31-
$this->assertEquals($result["error"]["code"],140);
30+
$result = TextRuApi::add("php_unit_test", "Test test test test test test test test test test test test test test test test test test test test test");
31+
$this->assertEquals($result["error"]["code"], 140);
32+
}
33+
34+
public function test_default_option_not_in_allowed_list()
35+
{
36+
$this->expectException(WrongParameterException::class);
37+
$this->expectExceptionCode(400122);
38+
$app = new TextRuApi("test", ["unknown_option" => "test"]);
3239
}
3340
}

0 commit comments

Comments
 (0)