Skip to content

Commit

Permalink
add test coverage statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Jul 27, 2024
1 parent c8e3cb5 commit 1ecc443
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Backend/Action/Dashboard/GetAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class GetAll implements ActionInterface
private View\Statistic\IncomingTransactions $incomingTransactions;
private View\Statistic\MostUsedOperations $mostUsedOperations;
private View\Statistic\TimePerOperation $timePerOperation;
private View\Statistic\TestCoverage $testCoverage;
private View\Dashboard\LatestApps $latestApps;
private View\Dashboard\LatestRequests $latestRequests;
private View\Dashboard\LatestUsers $latestUsers;
private View\Dashboard\LatestTransactions $latestTransactions;

public function __construct(TableManagerInterface $tableManager)
{
Expand All @@ -54,10 +54,10 @@ public function __construct(TableManagerInterface $tableManager)
$this->incomingTransactions = $tableManager->getTable(View\Statistic\IncomingTransactions::class);
$this->mostUsedOperations = $tableManager->getTable(View\Statistic\MostUsedOperations::class);
$this->timePerOperation = $tableManager->getTable(View\Statistic\TimePerOperation::class);
$this->testCoverage = $tableManager->getTable(View\Statistic\TestCoverage::class);
$this->latestApps = $tableManager->getTable(View\Dashboard\LatestApps::class);
$this->latestRequests = $tableManager->getTable(View\Dashboard\LatestRequests::class);
$this->latestUsers = $tableManager->getTable(View\Dashboard\LatestUsers::class);
$this->latestTransactions = $tableManager->getTable(View\Dashboard\LatestTransactions::class);
}

public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context): mixed
Expand All @@ -71,10 +71,10 @@ public function handle(RequestInterface $request, ParametersInterface $configura
'incomingTransactions' => $this->incomingTransactions->getView($transactionFilter, $context),
'mostUsedOperations' => $this->mostUsedOperations->getView($logFilter, $context),
'timePerOperation' => $this->timePerOperation->getView($logFilter, $context),
'testCoverage' => $this->testCoverage->getView($context),
'latestApps' => $this->latestApps->getView($context),
'latestRequests' => $this->latestRequests->getView($context),
'latestUsers' => $this->latestUsers->getView($context),
'latestTransactions' => $this->latestTransactions->getView($context),
];
}
}
51 changes: 51 additions & 0 deletions src/Backend/Action/Statistic/GetTestCoverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* Fusio is an open source API management platform which helps to create innovative API solutions.
* For the current version and information visit <https://www.fusio-project.org/>
*
* Copyright 2015-2023 Christoph Kappestein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Fusio\Impl\Backend\Action\Statistic;

use Fusio\Engine\ActionInterface;
use Fusio\Engine\ContextInterface;
use Fusio\Engine\ParametersInterface;
use Fusio\Engine\RequestInterface;
use Fusio\Impl\Backend\View;

/**
* GetTestCoverage
*
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://www.fusio-project.org
*/
class GetTestCoverage implements ActionInterface
{
private View\Statistic\TestCoverage $view;

public function __construct(View\Statistic\TestCoverage $view)
{
$this->view = $view;
}

public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context): mixed
{
return $this->view->getView(
$context
);
}
}
64 changes: 64 additions & 0 deletions src/Backend/View/Statistic/TestCoverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/*
* Fusio is an open source API management platform which helps to create innovative API solutions.
* For the current version and information visit <https://www.fusio-project.org/>
*
* Copyright 2015-2023 Christoph Kappestein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Fusio\Impl\Backend\View\Statistic;

use Fusio\Engine\ContextInterface;
use Fusio\Impl\Table;
use PSX\Sql\ViewAbstract;

/**
* TestCoverage
*
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://www.fusio-project.org
*/
class TestCoverage extends ViewAbstract
{
public function getView(ContextInterface $context)
{
$status = [
Table\Test::STATUS_PENDING => 'Pending',
Table\Test::STATUS_SUCCESS => 'Success',
Table\Test::STATUS_WARNING => 'Warning',
Table\Test::STATUS_ERROR => 'Error',
];

$data = [];
$labels = [];

foreach ($status as $key => $label) {
$count = (int) $this->connection->fetchOne('SELECT COUNT(*) AS cnt FROM fusio_test WHERE tenant_id = :tenant AND category_id = :category AND status = :status', [
'tenant' => $context->getTenantId(),
'category' => $context->getUser()->getCategoryId(),
'status' => $key,
]);

$data[] = $count;
$labels[] = $label;
}

return [
'labels' => $labels,
'data' => [$data],
];
}
}
8 changes: 8 additions & 0 deletions src/Installation/NewInstallation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,14 @@ private static function getOperations(): array
parameters: ['startIndex' => TypeFactory::getInteger(), 'count' => TypeFactory::getInteger(), 'search' => TypeFactory::getString(), 'from' => TypeFactory::getDateTime(), 'to' => TypeFactory::getDateTime(), 'operationId' => TypeFactory::getInteger(), 'appId' => TypeFactory::getInteger(), 'userId' => TypeFactory::getInteger(), 'ip' => TypeFactory::getString(), 'userAgent' => TypeFactory::getString(), 'method' => TypeFactory::getString(), 'path' => TypeFactory::getString(), 'header' => TypeFactory::getString(), 'body' => TypeFactory::getString()],
throws: [401 => Model\Common\Message::class, 500 => Model\Common\Message::class],
),
'statistic.getTestCoverage' => new Operation(
action: Backend\Action\Statistic\GetTestCoverage::class,
httpMethod: 'GET',
httpPath: '/statistic/test_coverage',
httpCode: 200,
outgoing: Model\Backend\StatisticChart::class,
throws: [401 => Model\Common\Message::class, 500 => Model\Common\Message::class],
),
'statistic.getTimeAverage' => new Operation(
action: Backend\Action\Statistic\GetTimeAverage::class,
httpMethod: 'GET',
Expand Down
28 changes: 16 additions & 12 deletions tests/Backend/Api/Dashboard/resource/dashboard_get.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,22 @@
"test.listFoo (ms)"
]
},
"testCoverage": {
"labels": [
"Pending",
"Success",
"Warning",
"Error"
],
"data": [
[
0,
0,
0,
0
]
]
},
"latestApps": {
"entry": [
{
Expand Down Expand Up @@ -410,17 +426,5 @@
"date": "[datetime]"
}
]
},
"latestTransactions": {
"entry": [
{
"id": 1,
"user_id": 2,
"plan_id": 2,
"transactionId": "[transaction_id]",
"amount": 39.99,
"date": "[datetime]"
}
]
}
}
71 changes: 71 additions & 0 deletions tests/Backend/Api/Statistic/TestCoverageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* Fusio is an open source API management platform which helps to create innovative API solutions.
* For the current version and information visit <https://www.fusio-project.org/>
*
* Copyright 2015-2023 Christoph Kappestein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Fusio\Impl\Tests\Backend\Api\Statistic;

use Fusio\Impl\Tests\Fixture;
use PSX\Framework\Test\ControllerDbTestCase;

/**
* TestCoverageTest
*
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://www.fusio-project.org
*/
class TestCoverageTest extends ControllerDbTestCase
{
public function getDataSet(): array
{
return Fixture::getDataSet();
}

public function testGet()
{
$response = $this->sendRequest('/backend/statistic/test_coverage?from=2015-06-01T00:00:00&to=2015-06-30T23:59:59', 'GET', array(
'User-Agent' => 'Fusio TestCase',
'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf'
));

$body = (string) $response->getBody();

$expect = <<<JSON
{
"labels": [
"Pending",
"Success",
"Warning",
"Error"
],
"data": [
[
0,
0,
0,
0
]
]
}
JSON;

$this->assertEquals(200, $response->getStatusCode(), $body);
$this->assertJsonStringEqualsJsonString($expect, $body, $body);
}
}

0 comments on commit 1ecc443

Please sign in to comment.