From 4216465b0d4bcdf432683a800d4294294d8b8eb7 Mon Sep 17 00:00:00 2001 From: Vladimir Sadicov Date: Wed, 13 Apr 2022 18:07:27 +0300 Subject: [PATCH 1/3] global refactoring adding typehints, allow sf6 and php >=8 --- composer.json | 19 ++++---- {src/Resources/config => config}/routing.xml | 0 {src/Resources/config => config}/services.xml | 0 {src/Resources/config => config}/twig.xml | 0 phpunit.xml.dist | 46 +++++++++---------- .../ChannelAuthenticatorInterface.php | 2 +- .../ChannelAuthenticatorPresenceInterface.php | 4 +- src/Controller/AuthController.php | 23 +++++----- src/DependencyInjection/Configuration.php | 11 +---- .../LopiPusherExtension.php | 2 +- src/LopiPusherBundle.php | 4 ++ src/PusherConfiguration.php | 8 ++-- tests/IntegrationTests/PusherAutowireTest.php | 2 +- .../PusherServiceDefinitionTest.php | 8 ++-- .../PusherTwigExtensionTest.php | 11 ++--- tests/LopiPusherTestKernel.php | 14 +++--- 16 files changed, 71 insertions(+), 83 deletions(-) rename {src/Resources/config => config}/routing.xml (100%) rename {src/Resources/config => config}/services.xml (100%) rename {src/Resources/config => config}/twig.xml (100%) diff --git a/composer.json b/composer.json index 1e66ba4..5b042f7 100644 --- a/composer.json +++ b/composer.json @@ -19,26 +19,23 @@ "source": "https://github.com/laupiFrpar/LopiPusherBundle" }, "require": { - "php": ">=7.2", + "php": ">=8.0.2", "ext-json": "*", - "pusher/pusher-php-server": "^4.0", - "symfony/config": "^3.4|^4.4|^5.0", - "symfony/dependency-injection": "^3.4|^4.4|^5.0", - "symfony/http-kernel": "^3.4|^4.4|^5.0" + "pusher/pusher-php-server": "^7.0", + "symfony/config": "^5.4|^6.0", + "symfony/dependency-injection": "^5.4|^6.0", + "symfony/http-kernel": "^5.4|^6.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.16", - "symfony/framework-bundle": "^3.4|^4.4|^5.0", - "symfony/phpunit-bridge": "^3.4|^4.4|^5.0", - "symfony/twig-bundle": "^3.4|^4.4|^5.0" + "symfony/framework-bundle": "^5.4|^6.0", + "symfony/phpunit-bridge": "^5.4|^6.0", + "symfony/twig-bundle": "^5.4|^6.0" }, "autoload": { "psr-4": { "Lopi\\Bundle\\PusherBundle\\": "src/" } }, "autoload-dev": { "psr-4": { "Lopi\\Bundle\\PusherBundle\\Tests\\": "tests/" } - }, - "conflict": { - "twig/twig": "<2.7" } } diff --git a/src/Resources/config/routing.xml b/config/routing.xml similarity index 100% rename from src/Resources/config/routing.xml rename to config/routing.xml diff --git a/src/Resources/config/services.xml b/config/services.xml similarity index 100% rename from src/Resources/config/services.xml rename to config/services.xml diff --git a/src/Resources/config/twig.xml b/config/twig.xml similarity index 100% rename from src/Resources/config/twig.xml rename to config/twig.xml diff --git a/phpunit.xml.dist b/phpunit.xml.dist index af432a3..0be89d3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,32 +1,30 @@ - - - - - - - - + bootstrap="vendor/autoload.php"> + + + src + + - - - ./tests - - + + + + + + + - - - src - - + + + ./tests + + - - - + + + diff --git a/src/Authenticator/ChannelAuthenticatorInterface.php b/src/Authenticator/ChannelAuthenticatorInterface.php index 1b84643..f15774c 100644 --- a/src/Authenticator/ChannelAuthenticatorInterface.php +++ b/src/Authenticator/ChannelAuthenticatorInterface.php @@ -20,5 +20,5 @@ interface ChannelAuthenticatorInterface * * @return bool */ - public function authenticate($socketId, $channelName); + public function authenticate(string $socketId, string $channelName): bool; } diff --git a/src/Authenticator/ChannelAuthenticatorPresenceInterface.php b/src/Authenticator/ChannelAuthenticatorPresenceInterface.php index ced4b0f..43489d6 100644 --- a/src/Authenticator/ChannelAuthenticatorPresenceInterface.php +++ b/src/Authenticator/ChannelAuthenticatorPresenceInterface.php @@ -19,12 +19,12 @@ interface ChannelAuthenticatorPresenceInterface extends ChannelAuthenticatorInte * * @return array */ - public function getUserInfo(); + public function getUserInfo(): array; /** * Return the user id when authenticated, used for presence channels. * * @return string */ - public function getUserId(); + public function getUserId(): string; } diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 40b37c2..2f656ed 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -22,13 +22,10 @@ */ class AuthController { - private $configuration; - private $authenticator; - - public function __construct(PusherConfiguration $configuration, ChannelAuthenticatorInterface $authenticator) - { - $this->configuration = $configuration; - $this->authenticator = $authenticator; + public function __construct( + private PusherConfiguration $configuration, + private ChannelAuthenticatorInterface $authenticator + ) { } /** @@ -67,12 +64,14 @@ public function authAction(Request $request): Response } /** - * Perform channel autentication. + * Perform channel authentication. * - * @param string $socketId The socket id + * @param string $socketId The socket id * @param string $channelName name of the channel to validate * - * @return array response auth data or null on access denied + * @return array|null response auth data or null on access denied + * + * @throws \JsonException */ private function authenticateChannel(string $socketId, string $channelName): ?array { @@ -83,11 +82,11 @@ private function authenticateChannel(string $socketId, string $channelName): ?ar return null; } - if (0 === strpos($channelName, 'presence') && $this->authenticator instanceof ChannelAuthenticatorPresenceInterface) { + if ($this->authenticator instanceof ChannelAuthenticatorPresenceInterface && str_starts_with($channelName, 'presence')) { $responseData['channel_data'] = json_encode([ 'user_id' => $this->authenticator->getUserId(), 'user_info' => $this->authenticator->getUserInfo(), - ]); + ], JSON_THROW_ON_ERROR); $data .= ':'.$responseData['channel_data']; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d95b797..22dedcc 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -19,18 +19,11 @@ class Configuration implements ConfigurationInterface { /** * Generates the configuration tree builder. - * - * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('lopi_pusher'); - - if (method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->getRootNode(); - } else { - $rootNode = $treeBuilder->root('lopi_pusher', 'array'); - } + $rootNode = $treeBuilder->getRootNode(); $rootNode ->validate() diff --git a/src/DependencyInjection/LopiPusherExtension.php b/src/DependencyInjection/LopiPusherExtension.php index db7e41f..e0d3b35 100644 --- a/src/DependencyInjection/LopiPusherExtension.php +++ b/src/DependencyInjection/LopiPusherExtension.php @@ -29,7 +29,7 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../config')); $loader->load('services.xml'); $pusherConfigurationDefinition = $container->getDefinition('lopi_pusher.pusher_configuration'); diff --git a/src/LopiPusherBundle.php b/src/LopiPusherBundle.php index 8689982..f2bd4f2 100644 --- a/src/LopiPusherBundle.php +++ b/src/LopiPusherBundle.php @@ -14,4 +14,8 @@ */ class LopiPusherBundle extends Bundle { + public function getPath(): string + { + return \dirname(__DIR__); + } } diff --git a/src/PusherConfiguration.php b/src/PusherConfiguration.php index 846d59b..dcc79e8 100644 --- a/src/PusherConfiguration.php +++ b/src/PusherConfiguration.php @@ -9,10 +9,10 @@ final class PusherConfiguration { - private $authKey; - private $secret; - private $appId; - private $options; + private string $authKey; + private string $secret; + private string $appId; + private array $options; public function __construct(array $config) { diff --git a/tests/IntegrationTests/PusherAutowireTest.php b/tests/IntegrationTests/PusherAutowireTest.php index d2913f3..1d9f7cc 100644 --- a/tests/IntegrationTests/PusherAutowireTest.php +++ b/tests/IntegrationTests/PusherAutowireTest.php @@ -17,7 +17,7 @@ final class PusherAutowireTest extends TestCase /** * @doesNotPerformAssertions */ - public function testPusherIsAutowiredByContainer() + public function testPusherIsAutowiredByContainer(): void { $builder = new ContainerBuilder(); $builder->autowire(PusherAutowireClass::class) diff --git a/tests/IntegrationTests/PusherServiceDefinitionTest.php b/tests/IntegrationTests/PusherServiceDefinitionTest.php index f1964d4..1e5425e 100644 --- a/tests/IntegrationTests/PusherServiceDefinitionTest.php +++ b/tests/IntegrationTests/PusherServiceDefinitionTest.php @@ -72,15 +72,15 @@ private function getConfiguredContainer(string $serviceId, array $bundleConfig = */ final class ChannelAuthenticator implements ChannelAuthenticatorPresenceInterface { - public function authenticate($socketId, $channelName) + public function authenticate(string $socketId, string $channelName): bool { } - public function getUserInfo() + public function getUserInfo(): array { } - public function getUserId() + public function getUserId(): string { } } @@ -92,7 +92,7 @@ final class DefinitionPublicCompilerPass implements CompilerPassInterface { public $definition; - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if ($container->hasDefinition($this->definition)) { $container->getDefinition($this->definition) diff --git a/tests/IntegrationTests/PusherTwigExtensionTest.php b/tests/IntegrationTests/PusherTwigExtensionTest.php index 5e6ef80..1ffb239 100644 --- a/tests/IntegrationTests/PusherTwigExtensionTest.php +++ b/tests/IntegrationTests/PusherTwigExtensionTest.php @@ -15,20 +15,17 @@ final class PusherTwigExtensionTest extends TestCase { - public function testExtensionIsLoaded() + public function testExtensionIsLoaded(): void { $kernel = new LopiPusherTestKernel(null, [new TwigBundle()], ['key' => 'test_key']); $kernel->boot(); $container = $kernel->getContainer(); + /** @var Environment $twig */ - $twig = $container->get('twig'); + $twig = $container->get(\Twig\Environment::class); $this->assertInstanceOf(PusherExtension::class, $twig->getExtension(PusherExtension::class)); - if (method_exists($this, 'assertStringContainsString')) { - $this->assertStringContainsString('test_key', $twig->render('sample.html.twig')); - } else { - $this->assertContains('test_key', $twig->render('sample.html.twig')); - } + $this->assertStringContainsString('test_key', $twig->render('sample.html.twig')); } } diff --git a/tests/LopiPusherTestKernel.php b/tests/LopiPusherTestKernel.php index 577c228..3be0c67 100644 --- a/tests/LopiPusherTestKernel.php +++ b/tests/LopiPusherTestKernel.php @@ -15,9 +15,9 @@ class LopiPusherTestKernel extends Kernel { - private $builder; - private $extraBundles; - private $config; + private ?ContainerBuilder $builder; + private array $extraBundles; + private array $config; public function __construct(ContainerBuilder $builder = null, array $bundles = [], array $config = []) { @@ -28,7 +28,7 @@ public function __construct(ContainerBuilder $builder = null, array $bundles = [ parent::__construct('test', true); } - public function registerBundles() + public function registerBundles(): iterable { return array_merge( [ @@ -39,7 +39,7 @@ public function registerBundles() ); } - public function registerContainerConfiguration(LoaderInterface $loader) + public function registerContainerConfiguration(LoaderInterface $loader): void { if (null === $this->builder) { $this->builder = new ContainerBuilder(); @@ -85,12 +85,12 @@ public function registerContainerConfiguration(LoaderInterface $loader) }); } - public function getCacheDir() + public function getCacheDir(): string { return sys_get_temp_dir().'/cache'.spl_object_hash($this); } - public function getLogDir() + public function getLogDir(): string { return sys_get_temp_dir().'/logs'.spl_object_hash($this); } From 871e959a3ccbb74c8b673ac2af921db3789b32e0 Mon Sep 17 00:00:00 2001 From: Vladimir Sadicov Date: Thu, 14 Apr 2022 10:34:10 +0300 Subject: [PATCH 2/3] move from travis to gh actions, update phpCSfixer --- .github/workflows/ci.yaml | 76 +++++++++++++++++++ .gitignore | 3 +- .php_cs.dist => .php-cs-fixer.dist.php | 4 +- .travis.yml | 32 -------- composer.json | 4 +- .../ChannelAuthenticatorInterface.php | 2 - .../ChannelAuthenticatorPresenceInterface.php | 4 - src/Controller/AuthController.php | 6 +- 8 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/ci.yaml rename .php_cs.dist => .php-cs-fixer.dist.php (90%) delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..0ea1a0d --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,76 @@ +name: Bundle CI +on: + push: + branches: ['main'] + pull_request: + schedule: + - cron: '0 */12 * * *' + +jobs: + static-analysis: + name: Static Analysis + runs-on: ubuntu-18.04 + + steps: + - name: "Checkout code" + uses: "actions/checkout@v2" + + - name: "Install PHP" + uses: "shivammathur/setup-php@v2" + with: + coverage: "none" + php-version: "8.0" + + - name: "Validate composer.json" + run: "composer validate --strict --no-check-lock" + + - name: "Composer install" + uses: "ramsey/composer-install@v1" + with: + composer-options: "--prefer-stable" + dependency-versions: 'highest' + + - name: Install PHP-CS-Fixer + run: composer global require friendsofphp/php-cs-fixer --prefer-dist --no-progress + + - name: Running php-cs-fixer + run: $HOME/.composer/vendor/bin/php-cs-fixer fix --config $GITHUB_WORKSPACE/.php-cs-fixer.dist.php --diff --dry-run + + tests: + name: "Tests ${{ matrix.php-version }} ${{ matrix.dependency-versions }} deps ${{ matrix.dependency-versions }}" + runs-on: ubuntu-18.04 + + strategy: + fail-fast: false + matrix: + # normal, highest, non-dev installs + php-version: ['8.0', '8.1'] + composer-options: ['--prefer-stable'] + dependency-versions: ['highest'] + include: + # testing lowest PHP version with lowest dependencies + - php-version: '8.0' + dependency-versions: 'lowest' + composer-options: '--prefer-lowest' + + steps: + - name: "Checkout code" + uses: "actions/checkout@v2" + + - name: "Install PHP" + uses: "shivammathur/setup-php@v2" + with: + coverage: "none" + php-version: "${{ matrix.php-version }}" + + - name: Install Global Dependencies + run: | + composer global require --no-progress --no-scripts --no-plugins symfony/flex >=1.x + - name: "Composer install" + uses: "ramsey/composer-install@v1" + with: + dependency-versions: "${{ matrix.dependency-versions }}" + composer-options: "--prefer-dist --no-progress" + + - name: All Tests + run: vendor/bin/simple-phpunit -c $GITHUB_WORKSPACE/phpunit.xml.dist diff --git a/.gitignore b/.gitignore index 49c5c8d..90e728f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,10 +15,11 @@ xcuserdata # svn & cvs .svn CVS -.php_cs.cache + vendor composer.lock *.sublime-workspace .phpunit.result.cache phpunit.xml +.php-cs-fixer.cache diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 90% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index 0421f78..7daf395 100644 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -4,11 +4,11 @@ exit(0); } -$finder = PhpCsFixer\Finder::create() +$finder = (new \PhpCsFixer\Finder()) ->in([__DIR__.'/src', __DIR__.'/tests']) ; -return PhpCsFixer\Config::create() +return (new \PhpCsFixer\Config()) ->setRules(array( '@Symfony' => true, '@Symfony:risky' => true, diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a50bc21..0000000 --- a/.travis.yml +++ /dev/null @@ -1,32 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - -matrix: - include: - - php: 7.3 - env: SYMFONY_REQUIRE=4.4.* - - php: 7.4 - env: SYMFONY_REQUIRE=5.1.* - -env: - - SYMFONY_REQUIRE=3.4.* - -before_install: - - find ~/.phpenv -name xdebug.ini -delete - - composer self-update - - composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-main - - composer update --prefer-source - -install: - - vendor/bin/simple-phpunit install - -script: - - vendor/bin/simple-phpunit - - vendor/bin/php-cs-fixer fix --dry-run --diff - -notifications: - email: laupi.frpar@gmail.com diff --git a/composer.json b/composer.json index 5b042f7..4df17c9 100644 --- a/composer.json +++ b/composer.json @@ -27,10 +27,10 @@ "symfony/http-kernel": "^5.4|^6.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.16", "symfony/framework-bundle": "^5.4|^6.0", "symfony/phpunit-bridge": "^5.4|^6.0", - "symfony/twig-bundle": "^5.4|^6.0" + "symfony/twig-bundle": "^5.4|^6.0", + "friendsofphp/php-cs-fixer": "^3.8" }, "autoload": { "psr-4": { "Lopi\\Bundle\\PusherBundle\\": "src/" } diff --git a/src/Authenticator/ChannelAuthenticatorInterface.php b/src/Authenticator/ChannelAuthenticatorInterface.php index f15774c..2e25cbf 100644 --- a/src/Authenticator/ChannelAuthenticatorInterface.php +++ b/src/Authenticator/ChannelAuthenticatorInterface.php @@ -17,8 +17,6 @@ interface ChannelAuthenticatorInterface /** * @param string $socketId The socket ID * @param string $channelName The channel name - * - * @return bool */ public function authenticate(string $socketId, string $channelName): bool; } diff --git a/src/Authenticator/ChannelAuthenticatorPresenceInterface.php b/src/Authenticator/ChannelAuthenticatorPresenceInterface.php index 43489d6..80732ae 100644 --- a/src/Authenticator/ChannelAuthenticatorPresenceInterface.php +++ b/src/Authenticator/ChannelAuthenticatorPresenceInterface.php @@ -16,15 +16,11 @@ interface ChannelAuthenticatorPresenceInterface extends ChannelAuthenticatorInte { /** * Returns an optional array of user info. - * - * @return array */ public function getUserInfo(): array; /** * Return the user id when authenticated, used for presence channels. - * - * @return string */ public function getUserId(): string; } diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index 2f656ed..53dcc15 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -66,7 +66,7 @@ public function authAction(Request $request): Response /** * Perform channel authentication. * - * @param string $socketId The socket id + * @param string $socketId The socket id * @param string $channelName name of the channel to validate * * @return array|null response auth data or null on access denied @@ -86,7 +86,7 @@ private function authenticateChannel(string $socketId, string $channelName): ?ar $responseData['channel_data'] = json_encode([ 'user_id' => $this->authenticator->getUserId(), 'user_info' => $this->authenticator->getUserInfo(), - ], JSON_THROW_ON_ERROR); + ], \JSON_THROW_ON_ERROR); $data .= ':'.$responseData['channel_data']; } @@ -100,7 +100,7 @@ private function authenticateChannel(string $socketId, string $channelName): ?ar * * @param string $data The data to hash */ - private function getCode($data): string + private function getCode(string $data): string { return hash_hmac('sha256', $data, $this->configuration->getSecret()); } From 5d87c0091e10b2fb7978836f8bffa5be8d8835fd Mon Sep 17 00:00:00 2001 From: Vladimir Sadicov Date: Thu, 14 Apr 2022 10:51:42 +0300 Subject: [PATCH 3/3] fix twig test --- tests/LopiPusherTestKernel.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/LopiPusherTestKernel.php b/tests/LopiPusherTestKernel.php index 3be0c67..ac3d519 100644 --- a/tests/LopiPusherTestKernel.php +++ b/tests/LopiPusherTestKernel.php @@ -77,6 +77,10 @@ public function registerContainerConfiguration(LoaderInterface $loader): void 'default_path' => __DIR__.'/Fixtures/templates', ] ); + + $container->setAlias(\Twig\Environment::class, 'twig') + ->setPublic(true) + ; } $container->register('kernel', static::class)