Added tests to test the KICad API endpoints

This commit is contained in:
Jan Böhmer 2023-12-03 00:43:34 +01:00
parent d976865e7a
commit 9994dbd9db
3 changed files with 323 additions and 2 deletions

View file

@ -33,7 +33,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
#[Route('/kicad-api/v1')] #[Route('/kicad-api/v1')]
class KiCADAPIController extends AbstractController class KiCadApiController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly KiCADHelper $kiCADHelper, private readonly KiCADHelper $kiCADHelper,
@ -54,7 +54,7 @@ class KiCADAPIController extends AbstractController
} }
#[Route('/categories.json', name: 'kicad_api_categories')] #[Route('/categories.json', name: 'kicad_api_categories')]
public function categories(NodesListBuilder $nodesListBuilder): Response public function categories(): Response
{ {
$this->denyAccessUnlessGranted('@categories.read'); $this->denyAccessUnlessGranted('@categories.read');

View file

@ -0,0 +1,71 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Part;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
class EDADataFixtures extends Fixture implements DependentFixtureInterface
{
public function getDependencies(): array
{
return [PartFixtures::class];
}
public function load(ObjectManager $manager): void
{
//Load elements from DB
$category1 = $manager->find(Category::class, 1);
$footprint1 = $manager->find(Footprint::class, 1);
$part1 = $manager->find(Part::class, 1);
//Put some data into category1 and foorprint1
$category1?->getEdaInfo()
->setExcludeFromBoard(true)
->setKicadSymbol('Category:1')
->setReferencePrefix('C')
;
$footprint1?->getEdaInfo()
->setKicadFootprint('Footprint:1')
;
//Put some data into part1 (which overrides the data from category1 and footprint1 on part1)
$part1?->getEdaInfo()
->setExcludeFromSim(false)
->setKicadSymbol('Part:1')
->setKicadFootprint('Part:1')
->setReferencePrefix('P')
;
//Flush the changes
$manager->flush();
}
}

View file

@ -0,0 +1,250 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Tests\Controller;
use App\DataFixtures\APITokenFixtures;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class KiCadApiControllerTest extends WebTestCase
{
private const BASE_URL = '/en/kicad-api/v1';
protected function createClientWithCredentials(string $token = APITokenFixtures::TOKEN_READONLY): KernelBrowser
{
return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
}
public function testRoot(): void
{
$client = $this->createClientWithCredentials();
$client->request('GET', self::BASE_URL.'/');
self::assertResponseIsSuccessful();
$content = $client->getResponse()->getContent();
self::assertJson($content);
//Check if the response contains the expected keys
$array = json_decode($content, true);
self::assertArrayHasKey('categories', $array);
self::assertArrayHasKey('parts', $array);
}
public function testCategories(): void
{
$client = $this->createClientWithCredentials();
$client->request('GET', self::BASE_URL.'/categories.json');
self::assertResponseIsSuccessful();
$content = $client->getResponse()->getContent();
self::assertJson($content);
$data = json_decode($content, true);
//There should be only one category, as the other ones contain no parts
self::assertCount(1, $data);
//Check if the response contains the expected keys
$category = $data[0];
self::assertArrayHasKey('name', $category);
self::assertArrayHasKey('id', $category);
}
public function testCategoryParts(): void
{
$client = $this->createClientWithCredentials();
$client->request('GET', self::BASE_URL.'/parts/category/1.json');
self::assertResponseIsSuccessful();
$content = $client->getResponse()->getContent();
self::assertJson($content);
$data = json_decode($content, true);
//There should be 3 parts in the category
self::assertCount(3, $data);
//Check if the response contains the expected keys
$part = $data[0];
self::assertArrayHasKey('name', $part);
self::assertArrayHasKey('id', $part);
self::assertArrayHasKey('description', $part);
}
public function testCategoryPartsForEmptyCategory(): void
{
$client = $this->createClientWithCredentials();
$client->request('GET', self::BASE_URL.'/parts/category/2.json');
//Response should still be successful, but the result should be empty
self::assertResponseIsSuccessful();
$content = $client->getResponse()->getContent();
self::assertJson($content);
self::assertEmpty(json_decode($content, true));
}
public function testPartDetailsPart1(): void
{
$client = $this->createClientWithCredentials();
$client->request('GET', self::BASE_URL.'/parts/1.json');
//Response should still be successful, but the result should be empty
self::assertResponseIsSuccessful();
$content = $client->getResponse()->getContent();
self::assertJson($content);
$data = json_decode($content, true);
$expected = array(
'id' => '1',
'name' => 'Part 1',
'symbolIdStr' => 'Part:1',
'exclude_from_bom' => 'False',
'exclude_from_board' => 'True',
'exclude_from_sim' => 'False',
'fields' =>
array(
'footprint' =>
array(
'value' => 'Part:1',
'visible' => 'False',
),
'reference' =>
array(
'value' => 'P',
'visible' => 'True',
),
'value' =>
array(
'value' => 'Part 1',
'visible' => 'True',
),
'keywords' =>
array(
'value' => '',
'visible' => 'False',
),
'datasheet' =>
array(
'value' => 'http://localhost/en/part/1/info',
'visible' => 'False',
),
'description' =>
array(
'value' => '',
'visible' => 'False',
),
'Category' =>
array(
'value' => 'Node 1',
'visible' => 'False',
),
'Manufacturing Status' =>
array(
'value' => '',
'visible' => 'False',
),
'Part-DB ID' =>
array(
'value' => '1',
'visible' => 'False',
),
),
);
self::assertEquals($expected, $data);
}
public function testPartDetailsPart2(): void
{
$client = $this->createClientWithCredentials();
$client->request('GET', self::BASE_URL.'/parts/1.json');
//Response should still be successful, but the result should be empty
self::assertResponseIsSuccessful();
$content = $client->getResponse()->getContent();
self::assertJson($content);
$data = json_decode($content, true);
//For part 2 things info should be taken from the category and footprint
$expected = array (
'id' => '1',
'name' => 'Part 1',
'symbolIdStr' => 'Part:1',
'exclude_from_bom' => 'False',
'exclude_from_board' => 'True',
'exclude_from_sim' => 'False',
'fields' =>
array (
'footprint' =>
array (
'value' => 'Part:1',
'visible' => 'False',
),
'reference' =>
array (
'value' => 'P',
'visible' => 'True',
),
'value' =>
array (
'value' => 'Part 1',
'visible' => 'True',
),
'keywords' =>
array (
'value' => '',
'visible' => 'False',
),
'datasheet' =>
array (
'value' => 'http://localhost/en/part/1/info',
'visible' => 'False',
),
'description' =>
array (
'value' => '',
'visible' => 'False',
),
'Category' =>
array (
'value' => 'Node 1',
'visible' => 'False',
),
'Manufacturing Status' =>
array (
'value' => '',
'visible' => 'False',
),
'Part-DB ID' =>
array (
'value' => '1',
'visible' => 'False',
),
),
);
self::assertEquals($expected, $data);
}
}