Skip to content

Commit 6906b25

Browse files
committed
Add phpunit.xml and few tests
1 parent 7684371 commit 6906b25

5 files changed

+80
-14
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ $result = TextRuApi::get($userkey, $uid, $jsonvisible);
6161
## PHPUnit тесты
6262
Запуск из корня компонента
6363
```bash
64-
phpunit ./tests/AddMethodTest.php --no-coverage
64+
./vendor/phpunit/phpunit/phpunit --no-coverage
6565
```

phpunit.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
processIsolation="false"
6+
stopOnFailure="false">
7+
<testsuites>
8+
<testsuite name="Application Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
</phpunit>

src/TextRuApi.php

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class TextRuApi
1919

2020
public function __construct($userkey, $default_options = [])
2121
{
22+
if (empty($userkey)) throw new WrongParameterException("userkey is empty", 400128);
23+
2224
$this->userkey = $userkey;
2325

2426
foreach ($default_options as $key => $value) {

tests/NonStaticMethodsTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace TextRuApi\Tests;
4+
5+
require_once __DIR__ . "/../src/TextRuApi.php";
6+
require_once __DIR__ . "/../src/Exception/CurlRequestException.php";
7+
require_once __DIR__ . "/../src/Exception/WrongParameterException.php";
8+
9+
use TextRuApi\Exception\WrongParameterException;
10+
use TextRuApi\Exception\UnknownMethodException;
11+
use TextRuApi\TextRuApi;
12+
13+
14+
class NonStaticMethodsTest extends \PHPUnit_Framework_TestCase
15+
{
16+
public function test_empty_userkey_rise_exception()
17+
{
18+
$this->expectException(WrongParameterException::class);
19+
$this->expectExceptionCode(400128);
20+
$app = new TextRuApi("");
21+
}
22+
23+
public function test_unknown_method_exception()
24+
{
25+
$this->expectException(UnknownMethodException::class);
26+
$this->expectExceptionCode(400126);
27+
$app = new TextRuApi("testkey");
28+
$app->unknownMethod();
29+
}
30+
31+
public function test_too_short_text()
32+
{
33+
$app = new TextRuApi("testkey");
34+
$result = $app->add("Short test text");
35+
$this->assertEquals(112, $result["error"]["code"]);
36+
}
37+
38+
public function test_wrong_userkey()
39+
{
40+
$app = new TextRuApi("php_unit_test");
41+
$result = $app->add("Test test test test test test test test test test test test test test test test test test test test test");
42+
$this->assertEquals(140, $result["error"]["code"]);
43+
}
44+
45+
public function test_default_option_not_in_allowed_list()
46+
{
47+
$this->expectException(WrongParameterException::class);
48+
$this->expectExceptionCode(400122);
49+
$app = new TextRuApi("test", ["unknown_option" => "test"]);
50+
}
51+
}

tests/AddMethodTest.php tests/StaticMethodsTest.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace TextRuApi\Tests;
44

5-
require __DIR__ . "/../src/TextRuApi.php";
6-
require __DIR__ . "/../src/Exception/CurlRequestException.php";
7-
require __DIR__ . "/../src/Exception/WrongParameterException.php";
5+
require_once __DIR__ . "/../src/TextRuApi.php";
6+
require_once __DIR__ . "/../src/Exception/CurlRequestException.php";
7+
require_once __DIR__ . "/../src/Exception/WrongParameterException.php";
88

99
use TextRuApi\Exception\WrongParameterException;
10+
use TextRuApi\Exception\UnknownMethodException;
1011
use TextRuApi\TextRuApi;
1112

1213

13-
class AddMethodTest extends \PHPUnit_Framework_TestCase
14+
class StaticMethodsTest extends \PHPUnit_Framework_TestCase
1415
{
1516
public function test_empty_userkey_rise_exception()
1617
{
@@ -19,22 +20,22 @@ public function test_empty_userkey_rise_exception()
1920
TextRuApi::add("", "Test text");
2021
}
2122

23+
public function test_unknown_static_method_exception()
24+
{
25+
$this->expectException(UnknownMethodException::class);
26+
$this->expectExceptionCode(400127);
27+
TextRuApi::unknownMethod();
28+
}
29+
2230
public function test_too_short_text()
2331
{
2432
$result = TextRuApi::add("afldkfjlas", "Short test text");
25-
$this->assertEquals($result["error"]["code"], 112);
33+
$this->assertEquals(112, $result["error"]["code"]);
2634
}
2735

2836
public function test_wrong_userkey()
2937
{
3038
$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"]);
39+
$this->assertEquals(140, $result["error"]["code"]);
3940
}
4041
}

0 commit comments

Comments
 (0)