Skip to content

Commit

Permalink
Test Symfony Constraints (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez authored Nov 14, 2024
1 parent 09921b3 commit 869fe17
Show file tree
Hide file tree
Showing 18 changed files with 2,362 additions and 899 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/symfony.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3', '8.4']
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/flex": "^1.17",
"symfony/form": "5.4.*",
"symfony/framework-bundle": "5.4.*",
"symfony/http-client": "5.4.*",
"symfony/mailer": "5.4.*",
"symfony/monolog-bundle": "^3.7",
"symfony/runtime": "5.4.*",
Expand Down
2,695 changes: 1,826 additions & 869 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config/packages/test/framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

// Web Profiler
$framework->profiler([
'collect' => false
'collect' => false,
]);
};
60 changes: 60 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

declare(strict_types=1);

use App\Controller\BrowserController;
use App\Controller\DashboardController;
use App\Controller\DomCrawlerController;
use App\Controller\FormController;
use App\Controller\HomeController;
use App\Controller\HttpClientController;
use App\Controller\RegistrationController;
use App\Controller\SecurityController;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
Expand All @@ -28,4 +32,60 @@
$routes->add('app_register', '/register')
->controller(RegistrationController::class)
->methods(['GET', 'POST']);

$routes->add('app_browser_request_attr', '/request_attr')
->controller([BrowserController::class, 'requestWithAttribute'])
->methods(['GET']);

$routes->add('app_browser_response_cookie', '/response_cookie')
->controller([BrowserController::class, 'responseWithCookie'])
->methods(['GET']);

$routes->add('app_browser_response_json', '/response_json')
->controller([BrowserController::class, 'responseJsonFormat'])
->methods(['GET']);

$routes->add('app_browser_unprocessable_entity', '/unprocessable_entity')
->controller([BrowserController::class, 'unprocessableEntity'])
->methods(['GET']);

$routes->add('app_browser_redirect_home', '/redirect_home')
->controller([BrowserController::class, 'redirectToHome'])
->methods(['GET']);

$routes->add('app_dom_crawler_test_page', '/test_page')
->controller(DomCrawlerController::class)
->methods(['GET']);

$routes->add('app_form_test', '/test_form')
->controller(FormController::class)
->methods(['GET', 'POST']);

$routes->add('route_using_http_client', '/route-using-http-client')
->controller([HttpClientController::class, 'routeUsingHttpClient'])
->methods(['GET']);

$routes->add('internal_endpoint', '/internal-endpoint')
->controller([HttpClientController::class, 'internalEndpoint'])
->methods(['GET']);

$routes->add('route_making_multiple_requests', '/route-making-multiple-requests')
->controller([HttpClientController::class, 'routeMakingMultipleRequests'])
->methods(['GET']);

$routes->add('internal_endpoint_post', '/internal-endpoint-post')
->controller([HttpClientController::class, 'internalEndpointPost'])
->methods(['POST']);

$routes->add('route_should_not_make_specific_request', '/route-should-not-make-specific-request')
->controller([HttpClientController::class, 'routeShouldNotMakeSpecificRequest'])
->methods(['GET']);

$routes->add('internal_endpoint_not_desired', '/internal-endpoint-not-desired')
->controller([HttpClientController::class, 'internalEndpointNotDesired'])
->methods(['GET']);

$routes->add('send_email', '/send-email')
->controller(App\Controller\SendEmailController::class)
->methods(['GET']);
};
21 changes: 21 additions & 0 deletions resources/views/dom_crawler/test_page.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'layout.html.twig' %}

{% block body %}
<h1>{{ title }}</h1>

<label for="exampleCheckbox">Example Checkbox</label>
<input type="checkbox" id="exampleCheckbox" name="exampleCheckbox" {% if checked %}checked{% endif %}>

<label for="exampleInput">Example Input</label>
<input type="text" id="exampleInput" name="exampleInput" value="{{ inputValue }}">

<p class="info">This is a test paragraph with some text.</p>

<form id="testForm" action="{{ path('app_form_test') }}" method="post">
<div>
<label for="username">Username</label>
<input type="text" id="username" name="username" value="{{ usernameValue }}">
</div>
<button type="submit">Submit</button>
</form>
{% endblock %}
50 changes: 50 additions & 0 deletions src/Controller/BrowserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class BrowserController extends AbstractController
{
public function requestWithAttribute(Request $request): Response
{
$request->attributes->set('page', 'register');

return $this->render('blog/home.html.twig');
}

public function responseWithCookie(): Response
{
$response = new Response('TESTCOOKIE has been set.');
$response->headers->setCookie(new Cookie('TESTCOOKIE', 'codecept'));

return $response;
}

public function responseJsonFormat(): Response
{
return $this->json([
'status' => 'success',
'message' => "Expected format: 'json'.",
]);
}

public function unprocessableEntity(): Response
{
return $this->json([
'status' => 'error',
'message' => 'The request was well-formed but could not be processed.',
], Response::HTTP_UNPROCESSABLE_ENTITY);
}

public function redirectToHome(): RedirectResponse
{
return $this->redirectToRoute('index');
}
}
22 changes: 22 additions & 0 deletions src/Controller/DomCrawlerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class DomCrawlerController extends AbstractController
{
public function __invoke(): Response
{
return $this->render('dom_crawler/test_page.html.twig', [
'page_title' => 'Test Page',
'title' => 'Test Page',
'checked' => true,
'inputValue' => 'Expected Value',
'usernameValue' => '',
]);
}
}
30 changes: 30 additions & 0 deletions src/Controller/FormController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class FormController extends AbstractController
{
public function __invoke(Request $request): Response
{
$data = [
'page_title' => 'Test Page',
'checked' => false,
'inputValue' => '',
];
if ($request->isMethod('POST')) {
$data['usernameValue'] = $request->request->get('username', '');
$data['title'] = 'Form Sent';
} else {
$data['usernameValue'] = 'codeceptUser';
$data['title'] = 'Test Page';
}

return $this->render('dom_crawler/test_page.html.twig', $data);
}
}
82 changes: 82 additions & 0 deletions src/Controller/HttpClientController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class HttpClientController extends AbstractController
{
private HttpClientInterface $httpClient;

public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}

public function routeUsingHttpClient(): Response
{
$internalUrl = $this->generateUrl('internal_endpoint', [], UrlGeneratorInterface::ABSOLUTE_URL);

$response = $this->httpClient->request('GET', $internalUrl, [
'headers' => ['Accept' => 'application/json'],
]);

return new Response("Internal request completed successfully: {$response->getStatusCode()}");
}

public function internalEndpoint(): Response
{
return $this->json(['message' => 'Response from internal endpoint.']);
}

public function routeMakingMultipleRequests(): Response
{
$internalUrl = $this->generateUrl('internal_endpoint', [], UrlGeneratorInterface::ABSOLUTE_URL);
$internalUrlPost = $this->generateUrl('internal_endpoint_post', [], UrlGeneratorInterface::ABSOLUTE_URL);

$response1 = $this->httpClient->request('GET', $internalUrl, [
'headers' => ['Accept' => 'application/json'],
]);

$response2 = $this->httpClient->request('POST', $internalUrlPost, [
'headers' => ['Content-Type' => 'application/json'],
'json' => ['key' => 'value'],
]);

$response3 = $this->httpClient->request('GET', $internalUrl, [
'headers' => ['Accept' => 'application/json'],
]);

$message = sprintf(
"Request 1: %d\nRequest 2: %d\nRequest 3: %d",
$response1->getStatusCode(),
$response2->getStatusCode(),
$response3->getStatusCode()
);

return new Response($message);
}

public function internalEndpointPost(Request $request): Response
{
$data = json_decode($request->getContent(), true);

return $this->json(['received' => $data]);
}

public function routeShouldNotMakeSpecificRequest(): Response
{
return new Response('No specific internal requests were made.');
}

public function internalEndpointNotDesired(): Response
{
return $this->json(['message' => 'This endpoint should not be called.']);
}
}
29 changes: 29 additions & 0 deletions src/Controller/SendEmailController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use App\Utils\Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

final class SendEmailController extends AbstractController
{
public function __construct(
private readonly Mailer $mailer,
) {
}

public function __invoke(): Response
{
$user = new User();
$user->setEmail('[email protected]');

$this->mailer->sendConfirmationEmail($user);

return new JsonResponse(['message' => 'Email sent successfully'], Response::HTTP_OK);
}
}
2 changes: 1 addition & 1 deletion src/Utils/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(private MailerInterface $mailer)
public function sendConfirmationEmail(User $user): TemplatedEmail
{
$email = (new TemplatedEmail())
->from('[email protected]')
->from(new Address('[email protected]', 'No Reply'))
->to(new Address($user->getEmail()))
->subject('Account created successfully')
->attach('Example attachment')
Expand Down
Loading

0 comments on commit 869fe17

Please sign in to comment.