Created
July 17, 2026 19:27
-
-
Save NandoKstroNet/848e3937e209d53e59c053becd598671 to your computer and use it in GitHub Desktop.
APOIO: Curso Symfony 8 & TDD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Controller; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\HttpFoundation\JsonResponse; | |
| use Symfony\Component\Routing\Attribute\Route; | |
| final class ProductController extends AbstractController | |
| { | |
| #[Route('/api/products', name: 'app_product')] | |
| public function index(): JsonResponse | |
| { | |
| $products = $this->tmpData(); | |
| return $this->json(['data' => $products]); | |
| } | |
| #[Route('/api/products/{product}', name: 'app_product_show')] | |
| public function show(int $product): JsonResponse | |
| { | |
| $product = array_find($this->tmpData(), fn($line) => $product === $line['id']); | |
| return $this->json(['data' => $product]); | |
| } | |
| private function tmpData(): array | |
| { | |
| return [ | |
| ['id' => 1, 'name' => 'Product 1', 'price' => 1990], | |
| ['id' => 2, 'name' => 'Product 2', 'price' => 3990], | |
| ['id' => 3, 'name' => 'Product 3', 'price' => 4990], | |
| ['id' => 4, 'name' => 'Product 4', 'price' => 4990], | |
| ['id' => 5, 'name' => 'Product 5', 'price' => 5990], | |
| ['id' => 6, 'name' => 'Product 6', 'price' => 5990], | |
| ]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Tests; | |
| use PHPUnit\Framework\Attributes\Test; | |
| use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
| class ProductControllerTest extends WebTestCase | |
| { | |
| #[Test] | |
| public function shouldEndpointHasReturnAllProductsByGetRequest(): void | |
| { | |
| $client = static::createClient(); | |
| $client->request('GET', 'api/products'); | |
| $payload = json_decode($client->getResponse()->getContent(), true)['data']; | |
| $this->assertResponseIsSuccessful(); | |
| $this->assertEquals('Product 1', $payload[0]['name']); | |
| $this->assertEquals('Product 2', $payload[1]['name']); | |
| $this->assertEquals('Product 3', $payload[2]['name']); | |
| } | |
| #[Test] | |
| public function shouldEndpointHasReturnJustOneProductByGetRequest(): void | |
| { | |
| $client = static::createClient(); | |
| $client->request('GET', 'api/products/1'); | |
| $payload = json_decode($client->getResponse()->getContent(), true)['data']; | |
| $this->assertResponseIsSuccessful(); | |
| $this->assertEquals(1, $payload['id']); | |
| $this->assertEquals('Product 1', $payload['name']); | |
| $this->assertEquals(1990, $payload['price']); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment