Skip to content

Commit

Permalink
Add TextRuApi get method and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krylov123 committed Jul 31, 2017
1 parent c3f5a3b commit fc5dcc1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
55 changes: 41 additions & 14 deletions src/TextRuApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,49 @@ public static function add($userkey, $text, $options = [])
$post_options = ["userkey" => $userkey, "text" => $text];
if (!empty($options)) $post_options = array_merge($post_options, $options);

return $this::sendCurl($post_options);
$answer_decoded = self::sendCurl($post_options);

$result = [
"error" => ["code" => null, "desc" => null],
"text_uid" => null
];

if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;
if (isset($answer_decoded->text_uid)) $result["text_uid"] = $answer_decoded->text_uid;

return $result;

}

public static function get($userkey, $uid, $jsonvisible = null)
{
if ((empty($userkey)) || (empty($uid))) throw new WrongParameterException("Required params is empty", 400131);

$post_options = ["userkey" => $userkey, "uid" => $uid];
if (!is_null($jsonvisible)) $post_options["jsonvisible"] = "detail";

$answer_decoded = self::sendCurl($post_options);

$result = [
"error" => ["code" => null, "desc" => null],
"text_unique" => null,
"result_json" => null,
"spell_check" => null,
"seo_check" => null
];

if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;

if (isset($answer_decoded->text_unique)) $result["text_unique"] = $answer_decoded->text_unique;
if (isset($answer_decoded->result_json)) $result["result_json"] = $answer_decoded->result_json;
if (isset($answer_decoded->spell_check)) $result["spell_check"] = $answer_decoded->spell_check;
if (isset($answer_decoded->seo_check)) $result["seo_check"] = $answer_decoded->seo_check;

return $result;
}

public static function sendCurl($postfields, $url = 'http://api.text.ru/post')
{
if (is_array($postfields)) $postfields = http_build_query($postfields, '', '&');
Expand All @@ -55,20 +94,8 @@ public static function sendCurl($postfields, $url = 'http://api.text.ru/post')
$answer = curl_exec($ch);
$errno = curl_errno($ch);

$result = [
"error" => ["code" => null, "desc" => null],
"text_uid" => null
];

if ($errno) throw new CurlRequestException(curl_error($ch), $errno);

$answer_decoded = json_decode($answer);

if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;
if (isset($answer_decoded->text_uid)) $result["text_uid"] = $answer_decoded->text_uid;

return $result;

return json_decode($answer);
}
}
12 changes: 12 additions & 0 deletions tests/AddMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ public function test_empty_userkey_rise_exception()
$this->expectExceptionCode(400123);
TextRuApi::add("","Test text");
}

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

public function test_wrong_userkey()
{
$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");
$this->assertEquals($result["error"]["code"],140);
}
}

0 comments on commit fc5dcc1

Please sign in to comment.