Skip to content

Commit e26046e

Browse files
committed
add connection agent designer test
1 parent baf4958 commit e26046e

File tree

4 files changed

+110
-10
lines changed

4 files changed

+110
-10
lines changed

src/Backend/Action/Connection/Agent/AgentAbstract.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ public function __construct(private Connector $connector, private FrameworkConfi
4141
{
4242
}
4343

44-
protected function getConnection(int $connectionId): AgentInterface
44+
protected function getConnection(RequestInterface $request): AgentInterface
4545
{
46+
$connectionId = $request->get('connection_id');
47+
if (empty($connectionId)) {
48+
throw new BadRequestException('Provided no connection');
49+
}
50+
4651
$connection = $this->connector->getConnection($connectionId);
4752
if (!$connection instanceof AgentInterface) {
4853
throw new BadRequestException('Provided an invalid connection');

src/Backend/Action/Connection/Agent/Send.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ public function handle(RequestInterface $request, ParametersInterface $configura
4949
{
5050
$this->assertConnectionEnabled();
5151

52-
$connectionId = (int) $request->get('connection_id');
53-
if (empty($connectionId)) {
54-
throw new BadRequestException('Provided no connection');
55-
}
56-
57-
$agent = $this->getConnection($connectionId);
52+
$agent = $this->getConnection($request);
5853
$payload = $request->getPayload();
5954

6055
assert($payload instanceof AgentContent);

src/Installation/NewInstallation.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use Fusio\Impl\Table;
3333
use Fusio\Marketplace;
3434
use Fusio\Model;
35-
use Psr\Container\ContainerInterface;
3635
use PSX\Api\Model\Passthru;
3736
use PSX\Api\OperationInterface;
3837
use PSX\Schema\ContentType;
@@ -599,8 +598,8 @@ private static function getOperations(): array
599598
httpMethod: 'POST',
600599
httpPath: '/connection/:connection_id/agent',
601600
httpCode: 200,
602-
outgoing: Model\Backend\AgentMessage::class,
603-
incoming: Model\Backend\AgentMessage::class,
601+
outgoing: Model\Backend\AgentContent::class,
602+
incoming: Model\Backend\AgentContent::class,
604603
throws: [999 => Model\Common\Message::class],
605604
description: 'Sends a message to an agent',
606605
),
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/*
3+
* Fusio - Self-Hosted API Management for Builders.
4+
* For the current version and information visit <https://www.fusio-project.org/>
5+
*
6+
* Copyright (c) Christoph Kappestein <christoph.kappestein@gmail.com>
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Fusio\Impl\Tests\Backend\Api\Connection\Agent;
22+
23+
use Fusio\Impl\Tests\DbTestCase;
24+
25+
/**
26+
* CollectionTest
27+
*
28+
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
29+
* @license http://www.apache.org/licenses/LICENSE-2.0
30+
* @link https://www.fusio-project.org
31+
*/
32+
class CollectionTest extends DbTestCase
33+
{
34+
public function testGet()
35+
{
36+
$response = $this->sendRequest('/backend/connection/Agent/agent', 'GET', array(
37+
'User-Agent' => 'Fusio TestCase',
38+
'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf'
39+
));
40+
41+
$body = (string) $response->getBody();
42+
43+
$this->assertEquals(404, $response->getStatusCode(), $body);
44+
}
45+
46+
public function testPost()
47+
{
48+
$response = $this->sendRequest('/backend/connection/Agent/agent', 'POST', array(
49+
'User-Agent' => 'Fusio TestCase',
50+
'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf'
51+
), json_encode([
52+
'type' => 'text',
53+
'content' => 'What is the meaning of life?',
54+
]));
55+
56+
$body = (string) $response->getBody();
57+
58+
$expect = <<<JSON
59+
{
60+
"type": "text",
61+
"content": "The answer ist: 42"
62+
}
63+
JSON;
64+
65+
$this->assertEquals(200, $response->getStatusCode(), $body);
66+
$this->assertJsonStringEqualsJsonString($expect, $body, $body);
67+
}
68+
69+
public function testPut()
70+
{
71+
$response = $this->sendRequest('/backend/connection/Agent/agent', 'PUT', array(
72+
'User-Agent' => 'Fusio TestCase',
73+
'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf'
74+
), json_encode([
75+
'foo' => 'bar',
76+
]));
77+
78+
$body = (string) $response->getBody();
79+
80+
$this->assertEquals(404, $response->getStatusCode(), $body);
81+
}
82+
83+
public function testDelete()
84+
{
85+
$response = $this->sendRequest('/backend/connection/Agent/agent', 'DELETE', array(
86+
'User-Agent' => 'Fusio TestCase',
87+
'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf'
88+
), json_encode([
89+
'foo' => 'bar',
90+
]));
91+
92+
$body = (string) $response->getBody();
93+
94+
$this->assertEquals(404, $response->getStatusCode(), $body);
95+
}
96+
97+
protected function isTransactional(): bool
98+
{
99+
return false;
100+
}
101+
}

0 commit comments

Comments
 (0)