Skip to content

Commit

Permalink
Allow Symfony 7
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas authored and karser committed Dec 18, 2023
1 parent f2cc87e commit 4f7a106
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 55 deletions.
74 changes: 33 additions & 41 deletions .github/workflows/code_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,65 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.1', '7.2', '7.4', '8.0', '8.1', '8.2']
symfony: ['^3.4', '^4.0', '^5.0']
php: ['7.1', '7.2', '7.4', '8.0', '8.1', '8.2', '8.3']
symfony: ['^3.4', '^4.0', '^5.0', '^6.0', '^7.0']
exclude:
- symfony: ^5.0
php: 7.1
- symfony: ^6.0
php: 7.1
- symfony: ^6.0
php: 7.2
- symfony: ^6.0
php: 7.4
- symfony: ^7.0
php: 7.1
- symfony: ^7.0
php: 7.2
- symfony: ^7.0
php: 7.4
- symfony: ^7.0
php: 8.0
- symfony: ^7.0
php: 8.1
include:
- symfony: ^6.0
php: 8.0
- symfony: ^6.0
php: 8.1
- symfony: ^6.0
php: 8.2
- symfony: ^7.0
php: 8.2
- symfony: ^7.0
php: 8.3
fail-fast: false
name: PHPUnit (PHP ${{ matrix.php }}) (Symfony ${{ matrix.symfony }})
steps:

- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: flex
coverage: none # disable xdebug, pcov

- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache Composer
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json **/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php }}-composer-
- name: Validate composer.json
run: composer validate --ansi --strict

- name: Restrict Symfony version
if: matrix.symfony != ''
run: |
composer global require --no-progress --no-scripts --no-plugins "symfony/flex"
composer global config --no-plugins allow-plugins.symfony/flex true
composer config extra.symfony.require "${{ matrix.symfony }}"
- name: Install PHP dependencies
uses: ramsey/composer-install@v2
env:
SYMFONY_REQUIRE: ${{ matrix.symfony }}

# remove this after support for symfony 3 is dropped
- name: Remove PhpUnit 10 support for old Symfony Versions
if: matrix.symfony == '^3.4' || matrix.symfony == '^4.0'
run: composer require --no-update "phpunit/phpunit:^7|^8|^9"

- name: Install PHP dependencies
run: composer install --no-interaction

- name: Validate composer.json
run: composer validate --ansi --strict

- name: Run tests
run: vendor/bin/phpunit

Expand All @@ -77,28 +81,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.3
coverage: xdebug

- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache Composer
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json **/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php }}-composer-
- name: Install PHP dependencies
run: composer install --no-interaction
uses: ramsey/composer-install@v2

- name: Run code coverage
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
Expand Down
24 changes: 20 additions & 4 deletions Tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function testFormJavascriptPresent_ifEnabled()
$view = $template->render(['form' => $form->createView()]);

//THEN
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
if (TestKernel::VERSION_ID >= 60400) {
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]">', $view);
} else {
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
}
self::assertStringContainsString('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&hl=en&onload=recaptchaCallback_form_captcha" async defer nonce=""></script>', $view);
self::assertStringContainsString('var recaptchaCallback_form_captcha', $view);
self::assertStringContainsString("document.getElementById('form_captcha').value = token;", $view);
Expand All @@ -55,7 +59,11 @@ public function testHyphenConvertedToUnderscore()
$view = $template->render(['form' => $form->createView()]);

//THEN
self::assertStringContainsString('<input type="hidden" id="form_capt-cha" name="form[capt-cha]" />', $view);
if (TestKernel::VERSION_ID >= 60400) {
self::assertStringContainsString('<input type="hidden" id="form_capt-cha" name="form[capt-cha]">', $view);
} else {
self::assertStringContainsString('<input type="hidden" id="form_capt-cha" name="form[capt-cha]" />', $view);
}
self::assertStringContainsString('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&hl=en&onload=recaptchaCallback_form_capt_cha" async defer nonce=""></script>', $view);
self::assertStringContainsString('var recaptchaCallback_form_capt_cha', $view);
self::assertStringContainsString("document.getElementById('form_capt-cha').value = token;", $view);
Expand All @@ -72,7 +80,11 @@ public function testFormJavascriptAbsent_ifDisabled()
$view = $template->render(['form' => $form->createView()]);

//THEN
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
if (TestKernel::VERSION_ID >= 60400) {
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]">', $view);
} else {
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
}
self::assertStringNotContainsString('<script src="https://www.google.com/recaptcha/api.js?render=key"></script>', $view);
self::assertStringNotContainsString("document.getElementById('form_captcha').value = token;", $view);
}
Expand Down Expand Up @@ -193,7 +205,11 @@ public function testFormJavascriptNoncePresent_ifSet()
$view = $template->render(['form' => $form->createView()]);

//THEN
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
if (TestKernel::VERSION_ID >= 60400) {
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]">', $view);
} else {
self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view);
}
self::assertStringContainsString('<script type="text/javascript" nonce="csp_nonce">', $view);
self::assertStringContainsString('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&hl=en&onload=recaptchaCallback_form_captcha" async defer nonce="csp_nonce"></script>', $view);
self::assertStringContainsString('var recaptchaCallback_form_captcha', $view);
Expand Down
6 changes: 4 additions & 2 deletions Tests/Validator/Constraints/Recaptcha3ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

Expand All @@ -20,11 +21,12 @@ class Recaptcha3ValidatorTest extends ConstraintValidatorTestCase

public function setUp(): void
{
$this->resolver = $this->getMockBuilder(IpResolverInterface::class)->getMock();
$this->resolver = $this->createMock(IpResolverInterface::class);

parent::setUp();
}

protected function createValidator()
protected function createValidator(): ConstraintValidatorInterface
{
$this->recaptcha = new RecaptchaMock();
return new Recaptcha3Validator($this->recaptcha, $enabled = true, $this->resolver);
Expand Down
1 change: 1 addition & 0 deletions Tests/fixtures/config/symfony6.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
framework:
http_method_override: false
session:
storage_factory_id: session.storage.factory.mock_file
4 changes: 3 additions & 1 deletion Validator/Constraints/Recaptcha3.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ final class Recaptcha3 extends Constraint
{
const INVALID_FORMAT_ERROR = '7147ffdb-0af4-4f7a-bd5e-e9dcfa6d7a2d';

protected static $errorNames = [
protected const ERROR_NAMES = [
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',
];

protected static $errorNames = self::ERROR_NAMES;

public $message = 'Your computer or network may be sending automated queries';
public $messageMissingValue = 'The captcha value is missing';

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@
"require": {
"php": ">=7.1",
"google/recaptcha": "^1.2",
"symfony/form": "^3.4|^4.0|^5.0|^6.0",
"symfony/framework-bundle": "^3.4.26|^4.2.7|^5.0|^6.0",
"symfony/expression-language": "^3.4|^4.0|^5.0|^6.0",
"symfony/yaml": "^3.4|^4.0|^5.0|^6.0",
"symfony/validator": "^3.4|^4.0|^5.0|^6.0",
"symfony/twig-bundle": "^3.4|^4.0|^5.0|^6.0",
"symfony/form": "^3.4|^4.0|^5.0|^6.0|^7.0",
"symfony/framework-bundle": "^3.4.26|^4.2.7|^5.0|^6.0|^7.0",
"symfony/expression-language": "^3.4|^4.0|^5.0|^6.0|^7.0",
"symfony/yaml": "^3.4|^4.0|^5.0|^6.0|^7.0",
"symfony/validator": "^3.4|^4.0|^5.0|^6.0|^7.0",
"symfony/twig-bundle": "^3.4|^4.0|^5.0|^6.0|^7.0",
"twig/twig": "^2.9|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^7|^8|^9|^10",
"symfony/http-client": "^4.3|^5.0|^6.0"
"symfony/http-client": "^4.3|^5.0|^6.0|^7.0"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 4f7a106

Please sign in to comment.