diff --git a/.php_cs.dist b/.php_cs.dist index cb5d0a8..e50b9e9 100755 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -36,7 +36,7 @@ $config = PhpCsFixer\Config::create() 'php_unit_internal_class' => false, 'php_unit_test_class_requires_covers' => false, 'no_superfluous_phpdoc_tags' => true, - // 'static_lambda' => true, + 'static_lambda' => true, ]) ->setFinder($finder) ; diff --git a/phpstan.neon b/phpstan.neon index 4e57279..6a93043 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -8,6 +8,12 @@ parameters: ignoreErrors: # Symfony DI - '#Cannot call method scalarNode\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null.#' + - "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./" # Symfony Contracts - '#Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required.#' + + # PHPUnit + - + message: '#Property .*::\$.* has no typehint specified.#' + path: tests/ diff --git a/src/Action/AuthErrorAction.php b/src/Action/AuthErrorAction.php index 4c95085..4236fcf 100644 --- a/src/Action/AuthErrorAction.php +++ b/src/Action/AuthErrorAction.php @@ -74,7 +74,7 @@ public function __invoke(): Response $event = new AuthFailedEvent(); $this->eventDispatcher->dispatch($event, Core23LastFmEvents::AUTH_ERROR); - if ($response = $event->getResponse()) { + if (null !== $response = $event->getResponse()) { return $response; } diff --git a/src/Action/AuthSuccessAction.php b/src/Action/AuthSuccessAction.php index 80dd7d5..1ba1e85 100644 --- a/src/Action/AuthSuccessAction.php +++ b/src/Action/AuthSuccessAction.php @@ -78,7 +78,7 @@ public function __invoke(): Response $event = new AuthSuccessEvent($session); $this->eventDispatcher->dispatch($event, Core23LastFmEvents::AUTH_SUCCESS); - if ($response = $event->getResponse()) { + if (null !== $response = $event->getResponse()) { return $response; } diff --git a/src/Action/CheckAuthAction.php b/src/Action/CheckAuthAction.php index ba8ef33..fc34792 100644 --- a/src/Action/CheckAuthAction.php +++ b/src/Action/CheckAuthAction.php @@ -44,9 +44,9 @@ public function __construct(RouterInterface $router, SessionManagerInterface $se public function __invoke(Request $request): RedirectResponse { - $token = $request->query->get('token'); + $token = (string) $request->query->get('token', ''); - if (!$token) { + if ('' === $token) { return new RedirectResponse($this->generateUrl('core23_lastfm_auth')); } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 33f61e1..af0ee89 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -22,7 +22,7 @@ public function getConfigTreeBuilder() $treeBuilder = new TreeBuilder('core23_lastfm'); // Keep compatibility with symfony/config < 4.2 - if (!method_exists($treeBuilder, 'getRootNode')) { + if (!method_exists(TreeBuilder::class, 'getRootNode')) { $rootNode = $treeBuilder->root('core23_lastfm'); } else { $rootNode = $treeBuilder->getRootNode(); diff --git a/tests/Action/AuthErrorActionTest.php b/tests/Action/AuthErrorActionTest.php index cb7fa3a..8768622 100644 --- a/tests/Action/AuthErrorActionTest.php +++ b/tests/Action/AuthErrorActionTest.php @@ -10,16 +10,13 @@ namespace Core23\LastFmBundle\Tests\Action; use Core23\LastFmBundle\Action\AuthErrorAction; -use Core23\LastFmBundle\Core23LastFmEvents; -use Core23\LastFmBundle\Event\AuthFailedEvent; use Core23\LastFmBundle\Session\SessionManagerInterface; +use Core23\LastFmBundle\Tests\EventDispatcher\TestEventDispatcher; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Twig\Environment; final class AuthErrorActionTest extends TestCase @@ -37,7 +34,7 @@ protected function setUp(): void $this->twig = $this->prophesize(Environment::class); $this->router = $this->prophesize(RouterInterface::class); $this->sessionManager = $this->prophesize(SessionManagerInterface::class); - $this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $this->eventDispatcher = new TestEventDispatcher(); } public function testExecute(): void @@ -50,15 +47,11 @@ public function testExecute(): void ->shouldBeCalled() ; - $this->eventDispatcher->dispatch(Argument::type(AuthFailedEvent::class), Core23LastFmEvents::AUTH_ERROR) - ->shouldBeCalled() - ; - $action = new AuthErrorAction( $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); $response = $action(); @@ -79,17 +72,13 @@ public function testExecuteWithCaughtEvent(): void $eventResponse = new Response(); - $this->eventDispatcher->dispatch(Argument::type(AuthFailedEvent::class), Core23LastFmEvents::AUTH_ERROR) - ->will(function ($args) use ($eventResponse) { - $args[0]->setResponse($eventResponse); - }) - ; + $this->eventDispatcher->setResponse($eventResponse); $action = new AuthErrorAction( $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); $response = $action(); @@ -112,7 +101,7 @@ public function testExecuteWithNoAuth(): void $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); static::assertInstanceOf(RedirectResponse::class, $action()); diff --git a/tests/Action/AuthSuccessActionTest.php b/tests/Action/AuthSuccessActionTest.php index 0162c3a..5b6fdba 100644 --- a/tests/Action/AuthSuccessActionTest.php +++ b/tests/Action/AuthSuccessActionTest.php @@ -11,16 +11,13 @@ use Core23\LastFm\Session\SessionInterface; use Core23\LastFmBundle\Action\AuthSuccessAction; -use Core23\LastFmBundle\Core23LastFmEvents; -use Core23\LastFmBundle\Event\AuthSuccessEvent; use Core23\LastFmBundle\Session\SessionManagerInterface; +use Core23\LastFmBundle\Tests\EventDispatcher\TestEventDispatcher; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Twig\Environment; final class AuthSuccessActionTest extends TestCase @@ -38,7 +35,7 @@ protected function setUp(): void $this->twig = $this->prophesize(Environment::class); $this->router = $this->prophesize(RouterInterface::class); $this->sessionManager = $this->prophesize(SessionManagerInterface::class); - $this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $this->eventDispatcher = new TestEventDispatcher(); } public function testExecute(): void @@ -55,10 +52,6 @@ public function testExecute(): void ->willReturn('FooUser') ; - $this->eventDispatcher->dispatch(Argument::type(AuthSuccessEvent::class), Core23LastFmEvents::AUTH_SUCCESS) - ->shouldBeCalled() - ; - $this->twig->render('@Core23LastFm/Auth/success.html.twig', [ 'name' => 'FooUser', ])->shouldBeCalled(); @@ -67,7 +60,7 @@ public function testExecute(): void $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); $response = $action(); @@ -92,17 +85,13 @@ public function testExecuteWithCaughtEvent(): void $eventResponse = new Response(); - $this->eventDispatcher->dispatch(Argument::type(AuthSuccessEvent::class), Core23LastFmEvents::AUTH_SUCCESS) - ->will(function ($args) use ($eventResponse) { - $args[0]->setResponse($eventResponse); - }) - ; + $this->eventDispatcher->setResponse($eventResponse); $action = new AuthSuccessAction( $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); $response = $action(); @@ -125,7 +114,7 @@ public function testExecuteNoAuth(): void $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); static::assertInstanceOf(RedirectResponse::class, $action()); @@ -149,7 +138,7 @@ public function testExecuteNoSession(): void $this->twig->reveal(), $this->router->reveal(), $this->sessionManager->reveal(), - $this->eventDispatcher->reveal() + $this->eventDispatcher ); static::assertInstanceOf(RedirectResponse::class, $action()); diff --git a/tests/Core23LastFmBundleTest.php b/tests/Core23LastFmBundleTest.php index dd42025..4318d4b 100644 --- a/tests/Core23LastFmBundleTest.php +++ b/tests/Core23LastFmBundleTest.php @@ -12,17 +12,9 @@ use Core23\LastFmBundle\Core23LastFmBundle; use Core23\LastFmBundle\DependencyInjection\Core23LastFmExtension; use PHPUnit\Framework\TestCase; -use Symfony\Component\HttpKernel\Bundle\BundleInterface; final class Core23LastFmBundleTest extends TestCase { - public function testItIsInstantiable(): void - { - $bundle = new Core23LastFmBundle(); - - static::assertInstanceOf(BundleInterface::class, $bundle); - } - public function testGetContainerExtension(): void { $bundle = new Core23LastFmBundle(); diff --git a/tests/Event/AuthFailedEventTest.php b/tests/Event/AuthFailedEventTest.php index c057b7a..824ec30 100644 --- a/tests/Event/AuthFailedEventTest.php +++ b/tests/Event/AuthFailedEventTest.php @@ -12,17 +12,9 @@ use Core23\LastFmBundle\Event\AuthFailedEvent; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Response; -use Symfony\Contracts\EventDispatcher\Event; final class AuthFailedEventTest extends TestCase { - public function testCreation(): void - { - $event = new AuthFailedEvent(); - - static::assertInstanceOf(Event::class, $event); - } - public function testGetResponse(): void { $event = new AuthFailedEvent(); diff --git a/tests/Event/AuthSuccessEventTest.php b/tests/Event/AuthSuccessEventTest.php index e00e3ed..69c709f 100644 --- a/tests/Event/AuthSuccessEventTest.php +++ b/tests/Event/AuthSuccessEventTest.php @@ -13,19 +13,9 @@ use Core23\LastFmBundle\Event\AuthSuccessEvent; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Response; -use Symfony\Contracts\EventDispatcher\Event; final class AuthSuccessEventTest extends TestCase { - public function testCreation(): void - { - $session = $this->prophesize(SessionInterface::class); - - $event = new AuthSuccessEvent($session->reveal()); - - static::assertInstanceOf(Event::class, $event); - } - public function testGetUsername(): void { $session = $this->prophesize(SessionInterface::class); diff --git a/tests/EventDispatcher/TestEventDispatcher.php b/tests/EventDispatcher/TestEventDispatcher.php new file mode 100644 index 0000000..0228fa0 --- /dev/null +++ b/tests/EventDispatcher/TestEventDispatcher.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Core23\LastFmBundle\Tests\EventDispatcher; + +use Core23\LastFmBundle\Event\AuthFailedEvent; +use Core23\LastFmBundle\Event\AuthSuccessEvent; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; + +final class TestEventDispatcher implements EventDispatcherInterface +{ + private $response; + + public function dispatch($event) + { + if ($event instanceof AuthFailedEvent || $event instanceof AuthSuccessEvent) { + $event->setResponse($this->response); + } + } + + public function setResponse(Response $response): void + { + $this->response = $response; + } +} diff --git a/tests/Session/SessionManagerTest.php b/tests/Session/SessionManagerTest.php index ed7494a..ec2e33f 100644 --- a/tests/Session/SessionManagerTest.php +++ b/tests/Session/SessionManagerTest.php @@ -10,7 +10,6 @@ namespace Core23\LastFmBundle\Tests\Session; use Core23\LastFm\Session\Session as LastFmSession; -use Core23\LastFm\Session\SessionInterface; use Core23\LastFmBundle\Session\SessionManager; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Session\Session; @@ -71,8 +70,6 @@ public function testStore(): void $manager = new SessionManager($session->reveal()); $manager->store($lastfmSession); - - static::assertTrue(true); } public function testClear(): void @@ -97,7 +94,6 @@ public function testGetSession(): void $manager = new SessionManager($session->reveal()); - /** @var SessionInterface $lastfmSession */ $lastfmSession = $manager->getSession(); static::assertNotNull($lastfmSession); diff --git a/vendor-bin/phpstan/composer.json b/vendor-bin/phpstan/composer.json index d247e05..e13413d 100644 --- a/vendor-bin/phpstan/composer.json +++ b/vendor-bin/phpstan/composer.json @@ -1,9 +1,12 @@ { "require": { + "ekino/phpstan-banned-code": "^0.1", "jangregor/phpstan-prophecy": "^0.4", "phpstan/extension-installer": "^1.0", "phpstan/phpstan": "^0.11", - "phpstan/phpstan-phpunit": "^0.11" + "phpstan/phpstan-phpunit": "^0.11", + "phpstan/phpstan-strict-rules": "^0.11", + "phpstan/phpstan-symfony": "^0.11" }, "conflict": { "phpunit/phpunit": ">=8.0"