Skip to content

Commit

Permalink
Add support for symfony 6 (#83)
Browse files Browse the repository at this point in the history
* Add support for symfony 6

* Fix test kernel return types

* Test against PHP 8.1

* Fix test config

* Fix compatibility

* Fix controller reference

* Move linting into own task

* Disable original constructor in test

* Fix mock of uploaded file

* Fix tests

* Fix code style

* Update phpstan baseline
  • Loading branch information
alexander-schranz authored Jul 14, 2022
1 parent 3927c9e commit bbd4d90
Show file tree
Hide file tree
Showing 19 changed files with 1,104 additions and 108 deletions.
41 changes: 35 additions & 6 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ on:
- '[0-9]+.x'
- '[0-9]+.[0-9]+'
jobs:
test:
name: 'PHP ${{ matrix.php-version }} (${{ matrix.dependency-versions }}, Lint ${{ matrix.lint }})'
php:
name: 'PHP ${{ matrix.php-version }} (${{ matrix.dependency-versions }})'
runs-on: ubuntu-latest
env:
DATABASE_URL: 'mysql://root:root@127.0.0.1/sulu_redirect_test?serverVersion=5.7'
Expand All @@ -19,23 +19,27 @@ jobs:
dependency-versions: 'lowest'
php-extensions: 'mysql, gd'
tools: 'composer:v1'
lint: false
env:
SYMFONY_DEPRECATIONS_HELPER: disabled

- php-version: '7.4'
dependency-versions: 'highest'
php-extensions: 'mysql, imagick'
tools: 'composer:v2'
lint: true
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.0'
dependency-versions: 'highest'
php-extensions: 'mysql, imagick'
tools: 'composer:v2'
lint: false
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.1'
dependency-versions: 'highest'
php-extensions: 'mysql, imagick'
tools: 'composer:v2'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

Expand Down Expand Up @@ -65,7 +69,6 @@ jobs:
uses: ramsey/composer-install@v1
with:
dependency-versions: ${{matrix.dependency-versions}}
composer-options: ${{ matrix.composer-options }}

- name: Bootstrap test environment
run: composer bootstrap-test-environment
Expand All @@ -79,3 +82,29 @@ jobs:
- name: Execute test cases
run: time composer test
env: '${{ matrix.env }}'

php-lint:
name: 'PHP Lint'
runs-on: ubuntu-latest
env:
DATABASE_URL: 'mysql://root:root@127.0.0.1/sulu_redirect_test?serverVersion=5.7'

steps:
- name: Checkout project
uses: actions/checkout@v2

- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: 'mysql, imagick'
tools: 'composer:v2'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v1
with:
dependency-versions: highest

- name: Lint code
run: composer lint
5 changes: 4 additions & 1 deletion .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
->exclude(['var/cache'])
->in(__DIR__);

return PhpCsFixer\Config::create()
$config = new PhpCsFixer\Config();
$config
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
Expand All @@ -26,3 +27,5 @@
'phpdoc_types_order' => false,
])
->setFinder($finder);

return $config;
8 changes: 4 additions & 4 deletions Admin/RedirectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
*/
class RedirectAdmin extends Admin
{
const SECURITY_CONTEXT = 'sulu.modules.redirects';
public const SECURITY_CONTEXT = 'sulu.modules.redirects';

const LIST_VIEW = 'sulu_redirect.list';
public const LIST_VIEW = 'sulu_redirect.list';

const ADD_FORM_VIEW = 'sulu_redirect.add_form';
public const ADD_FORM_VIEW = 'sulu_redirect.add_form';

const EDIT_FORM_VIEW = 'sulu_redirect.edit_form';
public const EDIT_FORM_VIEW = 'sulu_redirect.edit_form';

/**
* @var ViewBuilderFactoryInterface
Expand Down
2 changes: 1 addition & 1 deletion Controller/RedirectRouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
class RedirectRouteController extends AbstractRestController implements ClassResourceInterface, SecuredControllerInterface
{
const RESULT_KEY = 'redirect_routes';
public const RESULT_KEY = 'redirect_routes';

public function getSecurityContext(): string
{
Expand Down
8 changes: 4 additions & 4 deletions Import/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
*/
class Converter implements ConverterInterface
{
const SOURCE = 'source';
public const SOURCE = 'source';

const TARGET = 'target';
public const TARGET = 'target';

const STATUS_CODE = 'statusCode';
public const STATUS_CODE = 'statusCode';

const ENABLED = 'enabled';
public const ENABLED = 'enabled';

/**
* @var RedirectRouteRepositoryInterface
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/router.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<argument type="service" id="router.request_context"/>
<argument type="service" id="sulu_redirect.routing.nested_matcher"/>
<argument type="service" id="sulu_redirect.routing.generator"/>
<argument>null</argument>
<argument></argument>
<argument type="service" id="event_dispatcher" on-invalid="ignore"/>
<argument type="service" id="sulu_redirect.routing.provider"/>

Expand Down
8 changes: 4 additions & 4 deletions Routing/RedirectRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getRouteCollectionForRequest(Request $request)
public function getRouteCollectionForRequest(Request $request): RouteCollection
{
// server encodes the url and symfony does not encode it
// symfony decodes this data here https://github.com/symfony/symfony/blob/v5.2.3/src/Symfony/Component/Routing/Matcher/UrlMatcher.php#L88
Expand All @@ -59,7 +59,7 @@ public function getRouteCollectionForRequest(Request $request)
$route = new Route(
$pathInfo,
[
'_controller' => 'sulu_redirect.controller.redirect:redirect',
'_controller' => 'sulu_redirect.controller.redirect::redirect',
'redirectRoute' => $redirectRoute,
],
[],
Expand All @@ -73,15 +73,15 @@ public function getRouteCollectionForRequest(Request $request)
/**
* {@inheritdoc}
*/
public function getRouteByName($name)
public function getRouteByName($name): Route
{
throw new RouteNotFoundException('RedirectRouteProvider does not support getRouteByName.');
}

/**
* {@inheritdoc}
*/
public function getRoutesByNames($names)
public function getRoutesByNames($names = null): iterable
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Application/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_ENV=test
DATABASE_URL=mysql://root:@127.0.0.1:3306/su_redirect_test
DATABASE_URL=mysql://root:@127.0.0.1:3306/su_redirect_test?serverVersion=5.7
DATABASE_CHARSET=utf8mb4
DATABASE_COLLATE=utf8mb4_unicode_ci
4 changes: 2 additions & 2 deletions Tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Kernel extends SuluTestKernel
/**
* {@inheritdoc}
*/
public function registerBundles()
public function registerBundles(): iterable
{
return array_merge(parent::registerBundles(), [new SuluRedirectBundle()]);
}
Expand All @@ -38,7 +38,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
$loader->load(__DIR__ . '/config/config_' . $this->getContext() . '.yml');
}

protected function getKernelParameters()
protected function getKernelParameters(): array
{
$parameters = parent::getKernelParameters();

Expand Down
2 changes: 1 addition & 1 deletion Tests/Application/bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
// umask(0000);

set_time_limit(0);

Expand Down
3 changes: 0 additions & 3 deletions Tests/Application/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ framework:
router:
utf8: true

swiftmailer:
disable_delivery: true

doctrine:
orm:
mappings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class RedirectRouteControllerTest extends SuluTestCase
{
const BASE_URL = '/admin/api/redirect-routes';
public const BASE_URL = '/admin/api/redirect-routes';

/**
* @var array
Expand Down
51 changes: 23 additions & 28 deletions Tests/Unit/Controller/RedirectRouteImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Sulu\Bundle\RedirectBundle\Import\Item;
use Sulu\Bundle\RedirectBundle\Import\Reader\ReaderNotFoundException;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\FileBag;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -31,7 +30,7 @@ class RedirectRouteImportControllerTest extends TestCase
/**
* @var string
*/
private $importPath = '/var/uploads/imports';
private $importPath = __DIR__ . '/../../Application/var/uploads/imports';

/**
* @var string
Expand All @@ -45,14 +44,8 @@ public function testImportAction()
$fileBag = $this->prophesize(FileBag::class);
$request->reveal()->files = $fileBag->reveal();

$file = new File($fileName = __DIR__ . '/import.csv');
$importFile = $file->getPathname();
$uploadedFile = $this->createUploadedFile(__DIR__ . '/import.csv');

$uploadedFile = $this->getMockBuilder(UploadedFile::class)
->setConstructorArgs([$importFile, $this->fileName, null, null, UPLOAD_ERR_NO_FILE])
->getMock();
$uploadedFile->method('getClientOriginalName')->willReturn($this->fileName);
$uploadedFile->method('move')->with($this->importPath, $this->fileName)->willReturn($file);
$fileBag->has('redirectRoutes')->willReturn(true);
$fileBag->get('redirectRoutes')->willReturn($uploadedFile);

Expand All @@ -63,10 +56,10 @@ public function testImportAction()
];

$import = $this->prophesize(FileImportInterface::class);
$import->import($file->getRealPath())->willReturn($items);
$import->import(Argument::any())->willReturn($items);

$controller = new RedirectRouteImportController($import->reveal(), $this->importPath);
$response = $controller->postAction($request->reveal(), $this->importPath);
$response = $controller->postAction($request->reveal());

$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(200, $response->getStatusCode());
Expand All @@ -86,20 +79,13 @@ public function testImportActionReaderNotFound()
$fileBag = $this->prophesize(FileBag::class);
$request->reveal()->files = $fileBag->reveal();

$file = new File($fileName = __DIR__ . '/import.csv');
$importFile = $file->getPathname();

$uploadedFile = $this->getMockBuilder(UploadedFile::class)
->setConstructorArgs([$importFile, $this->fileName, null, null, UPLOAD_ERR_NO_FILE])
->getMock();
$uploadedFile = $this->createUploadedFile(__DIR__ . '/import.csv');

$uploadedFile->method('getClientOriginalName')->willReturn($this->fileName);
$uploadedFile->method('move')->with($this->importPath, $this->fileName)->willReturn($file);
$fileBag->has('redirectRoutes')->willReturn(true);
$fileBag->get('redirectRoutes')->willReturn($uploadedFile);

$import = $this->prophesize(FileImportInterface::class);
$import->import($file->getRealPath())->willThrow(ReaderNotFoundException::class);
$import->import(Argument::any())->willThrow(ReaderNotFoundException::class);

$controller = new RedirectRouteImportController($import->reveal(), $this->importPath);
$response = $controller->postAction($request->reveal(), $this->importPath);
Expand All @@ -115,19 +101,13 @@ public function testImportActionConverterNotFound()
$fileBag = $this->prophesize(FileBag::class);
$request->reveal()->files = $fileBag->reveal();

$file = new File($fileName = __DIR__ . '/import.csv');
$importFile = $file->getPathname();
$uploadedFile = $this->createUploadedFile(__DIR__ . '/import.csv');

$uploadedFile = $this->getMockBuilder(UploadedFile::class)
->setConstructorArgs([$importFile, $this->fileName, null, null, UPLOAD_ERR_NO_FILE])
->getMock();
$uploadedFile->method('getClientOriginalName')->willReturn($this->fileName);
$uploadedFile->method('move')->with($this->importPath, $this->fileName)->willReturn($file);
$fileBag->has('redirectRoutes')->willReturn(true);
$fileBag->get('redirectRoutes')->willReturn($uploadedFile);

$import = $this->prophesize(FileImportInterface::class);
$import->import($file->getRealPath())->willThrow(ConverterNotFoundException::class);
$import->import(Argument::any())->willThrow(ConverterNotFoundException::class);

$controller = new RedirectRouteImportController($import->reveal(), $this->importPath);
$response = $controller->postAction($request->reveal(), $this->importPath);
Expand All @@ -154,4 +134,19 @@ public function testImportActionNoFile()
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals(400, $response->getStatusCode());
}

private function createUploadedFile(string $filePath): UploadedFile
{
$tempFilePath = \tempnam(\sys_get_temp_dir(), 'sulu_redirect_uploaded_');

if (!$tempFilePath) {
throw new \RuntimeException(\sprintf('Could not create temporary image in "%s".', __CLASS__));
}

\file_put_contents($tempFilePath, \file_get_contents($filePath));

$uploadedFile = new UploadedFile($tempFilePath, 'import.csv', null, null, true);

return $uploadedFile;
}
}
Loading

0 comments on commit bbd4d90

Please sign in to comment.