diff --git a/tests/Controller/BatchEdaControllerTest.php b/tests/Controller/BatchEdaControllerTest.php new file mode 100644 index 00000000..040eddb3 --- /dev/null +++ b/tests/Controller/BatchEdaControllerTest.php @@ -0,0 +1,88 @@ +. + */ + +declare(strict_types=1); + +namespace App\Tests\Controller; + +use App\Entity\UserSystem\User; +use PHPUnit\Framework\Attributes\Group; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +#[Group("slow")] +#[Group("DB")] +class BatchEdaControllerTest extends WebTestCase +{ + private function loginAsUser($client, string $username): void + { + $entityManager = $client->getContainer()->get('doctrine')->getManager(); + $userRepository = $entityManager->getRepository(User::class); + $user = $userRepository->findOneBy(['name' => $username]); + + if (!$user) { + $this->markTestSkipped("User {$username} not found"); + } + + $client->loginUser($user); + } + + public function testBatchEdaPageLoads(): void + { + $client = static::createClient(); + $this->loginAsUser($client, 'admin'); + + // Request with part IDs in session — the page expects ids[] query param + $client->request('GET', '/en/tools/batch_eda_edit', ['ids' => [1, 2, 3]]); + + self::assertResponseIsSuccessful(); + } + + public function testBatchEdaPageWithoutPartsRedirects(): void + { + $client = static::createClient(); + $this->loginAsUser($client, 'admin'); + + // Request without part IDs should redirect + $client->request('GET', '/en/tools/batch_eda_edit'); + + self::assertResponseRedirects(); + } + + public function testBatchEdaFormSubmission(): void + { + $client = static::createClient(); + $this->loginAsUser($client, 'admin'); + + // Load the form page first + $crawler = $client->request('GET', '/en/tools/batch_eda_edit', ['ids' => [1, 2]]); + + self::assertResponseIsSuccessful(); + + // Find the form and submit it with reference prefix applied + $form = $crawler->selectButton('batch_eda[submit]')->form(); + $form['batch_eda[apply_reference_prefix]'] = true; + $form['batch_eda[reference_prefix]'] = 'R'; + + $client->submit($form); + + // Should redirect after successful submission + self::assertResponseRedirects(); + } +} diff --git a/tests/Controller/KiCadApiV2ControllerTest.php b/tests/Controller/KiCadApiV2ControllerTest.php new file mode 100644 index 00000000..19a7c416 --- /dev/null +++ b/tests/Controller/KiCadApiV2ControllerTest.php @@ -0,0 +1,176 @@ +. + */ + +declare(strict_types=1); + +namespace App\Tests\Controller; + +use App\DataFixtures\APITokenFixtures; +use Symfony\Bundle\FrameworkBundle\KernelBrowser; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; + +class KiCadApiV2ControllerTest extends WebTestCase +{ + private const BASE_URL = '/en/kicad-api/v2'; + + 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); + + $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); + self::assertCount(1, $data); + + $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); + self::assertCount(3, $data); + + $part = $data[0]; + self::assertArrayHasKey('name', $part); + self::assertArrayHasKey('id', $part); + self::assertArrayHasKey('description', $part); + } + + public function testCategoryPartsMinimal(): void + { + $client = $this->createClientWithCredentials(); + $client->request('GET', self::BASE_URL.'/parts/category/1.json?minimal=true'); + + self::assertResponseIsSuccessful(); + $content = $client->getResponse()->getContent(); + self::assertJson($content); + + $data = json_decode($content, true); + self::assertCount(3, $data); + } + + public function testPartDetailsHasVolatileFields(): void + { + $client = $this->createClientWithCredentials(); + $client->request('GET', self::BASE_URL.'/parts/1.json'); + + self::assertResponseIsSuccessful(); + $content = $client->getResponse()->getContent(); + self::assertJson($content); + + $data = json_decode($content, true); + + // V2 should have volatile flag on Stock field + self::assertArrayHasKey('fields', $data); + self::assertArrayHasKey('Stock', $data['fields']); + self::assertArrayHasKey('volatile', $data['fields']['Stock']); + self::assertEquals('True', $data['fields']['Stock']['volatile']); + } + + public function testPartDetailsV2VsV1Difference(): void + { + $client = $this->createClientWithCredentials(); + + // Get v1 response + $client->request('GET', '/en/kicad-api/v1/parts/1.json'); + self::assertResponseIsSuccessful(); + $v1Data = json_decode($client->getResponse()->getContent(), true); + + // Get v2 response + $client->request('GET', self::BASE_URL.'/parts/1.json'); + self::assertResponseIsSuccessful(); + $v2Data = json_decode($client->getResponse()->getContent(), true); + + // V1 should NOT have volatile on Stock + self::assertArrayNotHasKey('volatile', $v1Data['fields']['Stock']); + + // V2 should have volatile on Stock + self::assertArrayHasKey('volatile', $v2Data['fields']['Stock']); + + // Both should have the same stock value + self::assertEquals($v1Data['fields']['Stock']['value'], $v2Data['fields']['Stock']['value']); + } + + public function testCategoriesHasCacheHeaders(): void + { + $client = $this->createClientWithCredentials(); + $client->request('GET', self::BASE_URL.'/categories.json'); + + self::assertResponseIsSuccessful(); + $response = $client->getResponse(); + self::assertNotNull($response->headers->get('ETag')); + self::assertStringContainsString('max-age=', $response->headers->get('Cache-Control')); + } + + public function testConditionalRequestReturns304(): void + { + $client = $this->createClientWithCredentials(); + $client->request('GET', self::BASE_URL.'/categories.json'); + + $etag = $client->getResponse()->headers->get('ETag'); + self::assertNotNull($etag); + + $client->request('GET', self::BASE_URL.'/categories.json', [], [], [ + 'HTTP_IF_NONE_MATCH' => $etag, + ]); + + self::assertResponseStatusCodeSame(304); + } + + public function testUnauthenticatedAccessDenied(): void + { + $client = static::createClient(); + $client->request('GET', self::BASE_URL.'/categories.json'); + + // Should redirect to login (302) since not authenticated + self::assertResponseRedirects(); + } +}