Skip to content

Commit d87ece2

Browse files
author
arutyunyan
committed
Fix #376
1 parent b5d9eb8 commit d87ece2

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

src/syntax/entity/SuQLJsonService.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,42 @@
44

55
abstract class SuQLJsonService extends SuQLService
66
{
7-
public static function call($method, $params = [])
7+
/**
8+
* @var string вызываемый rpc метод
9+
*/
10+
private $rpcMethod;
11+
/**
12+
* Создает экземляр rpc метода
13+
* @param string $method
14+
* @return static
15+
*/
16+
public static function call($method)
17+
{
18+
$instance = new static();
19+
$instance->rpcMethod = $method;
20+
return $instance;
21+
}
22+
/**
23+
* Задает параметры rpc метода
24+
* @param array $params
25+
* @return \suql\syntax\entity\SuQLService
26+
*/
27+
public function withParams($params)
828
{
929
$body = [
1030
'json' => [
1131
'jsonrpc' => '2.0',
12-
'method' => $method,
32+
'method' => $this->rpcMethod,
1333
'params' => $params,
1434
'id' => uniqid(),
1535
],
1636
];
1737

1838
return parent::find($body);
1939
}
20-
40+
/**
41+
* @inheritdoc
42+
*/
2143
protected function processContent($content)
2244
{
2345
return json_decode($content, true);

src/syntax/entity/SuQLService.php

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static function find($body = [])
5151
}
5252
/**
5353
* Обработка полученного из сервиса контента
54+
* @param string $content
55+
* @return mixed
5456
*/
5557
protected function processContent($content)
5658
{

tests/suql/JsonServiceTest.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,41 @@
33
declare(strict_types=1);
44

55
use PHPUnit\Framework\TestCase;
6+
use suql\db\Container;
7+
use suql\syntax\Query;
8+
use test\suql\models\Query26;
69
use test\suql\models\Query29;
710

811
final class JsonServiceTest extends TestCase
912
{
13+
public function setUp(): void
14+
{
15+
// Create a database
16+
Container::create(require('config/db-null.php'));
17+
Query::create('create database db_test')->setConnection('connection')->exec();
18+
Container::add(require('config/db.php'));
19+
Query::create('create table table_26(f1 int, f2 int, primary key (f1))')->setConnection('db_test')->exec();
20+
Query::create('insert into table_26 (f1, f2) values (1, 1), (2, 2), (3, 3)')->setConnection('db_test')->exec();
21+
}
22+
23+
public function tearDown(): void
24+
{
25+
// Drop the database
26+
Query::create('drop database db_test')->setConnection('db_test')->exec();
27+
}
28+
1029
public function testJsonService(): void
1130
{
12-
$actual = Query29::call('generateIntegers', [
31+
$actual = Query29::call('generateIntegers')->withParams([
1332
'apiKey' => '175082e4-b7bb-43cc-a018-df19794bcbb2',
1433
'n' => 3,
15-
'min' => 0,
16-
'max' => 9
17-
]);
34+
'min' => 1,
35+
'max' => 3
36+
])->join(Query26::class)->select(['f2'])->fetchOne();
1837
foreach ($actual as $number) {
19-
$this->assertLessThanOrEqual(9, $number);
20-
$this->assertGreaterThanOrEqual(0, $number);
38+
$this->assertLessThan(4, $number);
39+
$this->assertGreaterThan(0, $number);
2140
}
22-
$this->assertCount(3, $actual);
41+
$this->assertLessThan(4, count($actual));
2342
}
2443
}

tests/suql/models/Query29.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@
33
namespace test\suql\models;
44

55
use suql\syntax\entity\SuQLJsonService;
6+
use test\suql\connections\TestMySQLConnection;
67

78
# [Request(uri="https://api.random.org/json-rpc/2/invoke", method="POST")]
89
class Query29 extends SuQLJsonService
910
{
11+
use TestMySQLConnection;
12+
1013
protected function processContent($content)
1114
{
1215
$content = json_decode($content, true);
13-
return $content['result']['random']['data'];
16+
$data = $content['result']['random']['data'];
17+
static::$data = [
18+
['f1' => $data[0]],
19+
['f1' => $data[1]],
20+
['f1' => $data[2]],
21+
];
22+
return parent::all();
23+
}
24+
25+
public function relations()
26+
{
27+
return [
28+
Query26::class => ['f1' => 'f1'],
29+
];
1430
}
1531
}

0 commit comments

Comments
 (0)