From 9994dbd9db8c43f44651d675c6c150627fb4d026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 3 Dec 2023 00:43:34 +0100 Subject: [PATCH] Added tests to test the KICad API endpoints --- ...IController.php => KiCadApiController.php} | 4 +- src/DataFixtures/EDADataFixtures.php | 71 +++++ tests/Controller/KiCadApiControllerTest.php | 250 ++++++++++++++++++ 3 files changed, 323 insertions(+), 2 deletions(-) rename src/Controller/{KiCADAPIController.php => KiCadApiController.php} (95%) create mode 100644 src/DataFixtures/EDADataFixtures.php create mode 100644 tests/Controller/KiCadApiControllerTest.php diff --git a/src/Controller/KiCADAPIController.php b/src/Controller/KiCadApiController.php similarity index 95% rename from src/Controller/KiCADAPIController.php rename to src/Controller/KiCadApiController.php index 17be45ea..cd63e627 100644 --- a/src/Controller/KiCADAPIController.php +++ b/src/Controller/KiCadApiController.php @@ -33,7 +33,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/kicad-api/v1')] -class KiCADAPIController extends AbstractController +class KiCadApiController extends AbstractController { public function __construct( private readonly KiCADHelper $kiCADHelper, @@ -54,7 +54,7 @@ class KiCADAPIController extends AbstractController } #[Route('/categories.json', name: 'kicad_api_categories')] - public function categories(NodesListBuilder $nodesListBuilder): Response + public function categories(): Response { $this->denyAccessUnlessGranted('@categories.read'); diff --git a/src/DataFixtures/EDADataFixtures.php b/src/DataFixtures/EDADataFixtures.php new file mode 100644 index 00000000..1484f03e --- /dev/null +++ b/src/DataFixtures/EDADataFixtures.php @@ -0,0 +1,71 @@ +. + */ + +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(); + } +} \ No newline at end of file diff --git a/tests/Controller/KiCadApiControllerTest.php b/tests/Controller/KiCadApiControllerTest.php new file mode 100644 index 00000000..a66cb8a4 --- /dev/null +++ b/tests/Controller/KiCadApiControllerTest.php @@ -0,0 +1,250 @@ +. + */ + +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); + } + +} \ No newline at end of file