Skip to content

Commit

Permalink
Test Symfony Constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez committed Sep 28, 2024
1 parent 09921b3 commit 74a1c1e
Show file tree
Hide file tree
Showing 7 changed files with 1,785 additions and 749 deletions.
2,256 changes: 1,509 additions & 747 deletions composer.lock

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

use App\Controller\BrowserController;
use App\Controller\DashboardController;
use App\Controller\DomCrawlerController;
use App\Controller\HomeController;
use App\Controller\RegistrationController;
use App\Controller\SecurityController;
Expand All @@ -28,4 +30,28 @@
$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']);
};
13 changes: 13 additions & 0 deletions resources/views/dom_crawler/test_page.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% 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>
{% 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');
}
}
21 changes: 21 additions & 0 deletions src/Controller/DomCrawlerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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',
]);
}
}
113 changes: 111 additions & 2 deletions tests/Functional/BrowserCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,115 @@

final class BrowserCest
{
public function assertBrowserCookieValueSame(FunctionalTester $I)
{
$I->setCookie('TESTCOOKIE', 'codecept');
$I->assertBrowserCookieValueSame('TESTCOOKIE', 'codecept');
}

public function assertBrowserHasCookie(FunctionalTester $I)
{
$I->setCookie('TESTCOOKIE', 'codecept');
$I->assertBrowserHasCookie('TESTCOOKIE');
}

public function assertBrowserNotHasCookie(FunctionalTester $I)
{
$I->setCookie('TESTCOOKIE', 'codecept');
$I->resetCookie('TESTCOOKIE');
$I->assertBrowserNotHasCookie('TESTCOOKIE');
}

public function assertRequestAttributeValueSame(FunctionalTester $I)
{
$I->amOnPage('/request_attr');
$I->assertRequestAttributeValueSame('page', 'register');
}

public function assertResponseCookieValueSame(FunctionalTester $I)
{
$I->amOnPage('/response_cookie');
$I->assertResponseCookieValueSame('TESTCOOKIE', 'codecept');
}

public function assertResponseFormatSame(FunctionalTester $I)
{
$I->amOnPage('/response_json');
$I->assertResponseFormatSame('json');
}

public function assertResponseHasCookie(FunctionalTester $I)
{
$I->amOnPage('/response_cookie');
$I->assertResponseHasCookie('TESTCOOKIE');
}

public function assertResponseHasHeader(FunctionalTester $I)
{
$I->amOnPage('/response_json');
$I->assertResponseHasHeader('content-type');
}

public function assertResponseHeaderNotSame(FunctionalTester $I)
{
$I->amOnPage('/response_json');
$I->assertResponseHeaderNotSame('content-type', 'application/octet-stream');
}

public function assertResponseHeaderSame(FunctionalTester $I)
{
$I->amOnPage('/response_json');
$I->assertResponseHeaderSame('content-type', 'application/json');
}

public function assertResponseIsSuccessful(FunctionalTester $I)
{
$I->amOnPage('/');
$I->assertResponseIsSuccessful();
}

public function assertResponseIsUnprocessable(FunctionalTester $I)
{
$I->amOnPage('/unprocessable_entity');
$I->assertResponseIsUnprocessable();
}

public function assertResponseNotHasCookie(FunctionalTester $I)
{
$I->amOnPage('/');
$I->assertResponseNotHasCookie('TESTCOOKIE');
}

public function assertResponseNotHasHeader(FunctionalTester $I)
{
$I->amOnPage('/');
$I->assertResponseNotHasHeader('accept-charset');
}

public function assertResponseRedirects(FunctionalTester $I)
{
$I->stopFollowingRedirects();
$I->amOnPage('/redirect_home');
$I->assertResponseRedirects();
$I->assertResponseRedirects('/');
}

public function assertResponseStatusCodeSame(FunctionalTester $I)
{
$I->stopFollowingRedirects();
$I->amOnPage('/redirect_home');
$I->assertResponseStatusCodeSame(302);
}

public function assertRouteSame(FunctionalTester $I)
{
$I->amOnPage('/');
$I->assertRouteSame('index');

$I->amOnPage('/login');
$I->assertRouteSame('app_login');
}

public function seePageIsAvailable(FunctionalTester $I)
{
// With url parameter
Expand All @@ -30,10 +139,10 @@ public function submitSymfonyForm(FunctionalTester $I)
$I->submitSymfonyForm('registration_form', [
'[email]' => '[email protected]',
'[password]' => '123456',
'[agreeTerms]' => true
'[agreeTerms]' => true,
]);
$I->seeInRepository(User::class, [
'email' => '[email protected]'
'email' => '[email protected]',
]);
}
}
55 changes: 55 additions & 0 deletions tests/Functional/DomCrawlerCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Tests\Functional;

use App\Tests\Support\FunctionalTester;

final class DomCrawlerCest
{
public function _before(FunctionalTester $I): void
{
$I->amOnPage('/test_page');
}

public function assertCheckboxChecked(FunctionalTester $I): void
{
$I->assertCheckboxChecked('exampleCheckbox', 'The checkbox should be checked.');
}

public function assertCheckboxNotChecked(FunctionalTester $I): void
{
$I->assertCheckboxNotChecked('nonExistentCheckbox', 'This checkbox should not be checked.');
}

public function assertInputValueSame(FunctionalTester $I): void
{
$I->assertInputValueSame('exampleInput', 'Expected Value', 'The input value should be "Expected Value".');
}

public function assertPageTitleContains(FunctionalTester $I): void
{
$I->assertPageTitleContains('Test', 'The page title should contain "Test".');
}

public function assertPageTitleSame(FunctionalTester $I): void
{
$I->assertPageTitleSame('Test Page', 'The page title should be "Test Page".');
}

public function assertSelectorExists(FunctionalTester $I): void
{
$I->assertSelectorExists('h1', 'The <h1> element should be present.');
}

public function assertSelectorNotExists(FunctionalTester $I): void
{
$I->assertSelectorNotExists('.non-existent-class', 'This selector should not exist.');
}

public function assertSelectorTextSame(FunctionalTester $I): void
{
$I->assertSelectorTextSame('h1', 'Test Page', 'The text in the <h1> tag should be exactly "Test Page".');
}
}

0 comments on commit 74a1c1e

Please sign in to comment.