Skip to content

Commit

Permalink
Merge pull request #1268 from tarlepp/refactor/phpstan-configuration
Browse files Browse the repository at this point in the history
Refactor/PHPStan setup + code cleanup
  • Loading branch information
tarlepp committed Jun 8, 2021
2 parents fdfcae3 + 7be5341 commit 5c333d8
Show file tree
Hide file tree
Showing 29 changed files with 68 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .env
Expand Up @@ -24,5 +24,5 @@ APP_SECRET=c200e4e3cdc72f0a12d3eada5f694526
###< symfony/framework-bundle ###

###> symfony-flex-backend ###
APPLICATION_CONFIG=/app/secrets/application.json
APPLICATION_CONFIG=./secrets/application.json
###< symfony-flex-backend ###
7 changes: 0 additions & 7 deletions .github/workflows/main.yml
Expand Up @@ -42,13 +42,6 @@ jobs:
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Configure application for CI run
run: |
cp .env.gh-actions .env
cp .env.gh-actions .env.test
make generate-jwt-keys
chmod 644 ./config/jwt/private.pem
- name: Validate composer.json and composer.lock
run: composer validate

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -210,7 +210,7 @@ psalm: ## Runs Psalm static analysis tool
ifeq ($(INSIDE_DOCKER), 1)
@echo "\033[32mRunning Psalm - A static analysis tool for PHP\033[39m"
@mkdir -p build
@@bin/console cache:clear --env=test
@@bin/console cache:clear
@php ./vendor/bin/psalm --version
@php ./vendor/bin/psalm --no-cache --report=./build/psalm.json
else
Expand All @@ -221,7 +221,7 @@ psalm-shepherd: ## Runs Psalm static analysis tool + report results to shepherd
ifeq ($(INSIDE_DOCKER), 1)
@echo "\033[32mRunning Psalm - A static analysis tool for PHP\033[39m"
@mkdir -p build
@@bin/console cache:clear --env=test
@@bin/console cache:clear
@php ./vendor/bin/psalm --version
@php ./vendor/bin/psalm --no-cache --shepherd --report=./build/psalm.json
else
Expand All @@ -232,7 +232,7 @@ psalm-github: ## Runs Psalm static analysis tool
ifeq ($(INSIDE_DOCKER), 1)
@echo "\033[32mRunning Psalm - A static analysis tool for PHP\033[39m"
@mkdir -p build
@@bin/console cache:clear --env=test
@@bin/console cache:clear
@php ./vendor/bin/psalm --version
@php ./vendor/bin/psalm --no-cache --shepherd --report=./build/psalm.json --output-format=github
else
Expand All @@ -242,7 +242,7 @@ endif
phpstan: ## Runs PHPStan static analysis tool
ifeq ($(INSIDE_DOCKER), 1)
@echo "\033[32mRunning PHPStan - PHP Static Analysis Tool\033[39m"
@@bin/console cache:clear --env=test
@@bin/console cache:clear
@./vendor/bin/phpstan --version
@./vendor/bin/phpstan
else
Expand Down
18 changes: 18 additions & 0 deletions config/services.yaml
Expand Up @@ -91,6 +91,24 @@ when@dev:
App\Utils\Tests\:
resource: '../src/Utils/Tests'

App\Tests\E2E\Rest\src\:
resource: '../tests/E2E/Rest/src/*'

App\Tests\E2E\Rest\src\Controller\:
resource: '../tests/E2E/Rest/src/Controller/*'
tags:
- 'controller.service_arguments'
- 'app.rest.controller'

App\Tests\E2E\Rest\Traits\Actions\src\:
resource: '../tests/E2E/Rest/Traits/Actions/src/*'
tags:
- 'controller.service_arguments'
- 'app.rest.controller'

App\Tests\Integration\Helpers\src\:
resource: '../tests/Integration/Helpers/src/*'

ProxyManager\Factory\AccessInterceptorValueHolderFactory:
class: ProxyManager\Factory\AccessInterceptorValueHolderFactory

Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Expand Up @@ -5,7 +5,8 @@ parameters:
- tests
editorUrl: '%%file%%:%%line%%'
checkGenericClassInNonGenericObjectType: false

symfony:
container_xml_path: var/cache/dev/App_KernelDevDebugContainer.xml
includes:
- tools/02_phpstan/vendor/phpstan/phpstan/conf/bleedingEdge.neon
- tools/02_phpstan/vendor/phpstan/phpstan-phpunit/extension.neon
Expand Down
1 change: 0 additions & 1 deletion tests/E2E/Controller/HealthzControllerTest.php
Expand Up @@ -38,7 +38,6 @@ public function testThatHealthzRouteReturns200(): void
*/
public function testThatHealthzRouteDoesNotMakeRequestLog(): void
{
/** @var LogRequestResource $resource */
$resource = static::getContainer()->get(LogRequestResource::class);

$expectedLogCount = $resource->count();
Expand Down
1 change: 0 additions & 1 deletion tests/E2E/Controller/Profile/GroupsControllerTest.php
Expand Up @@ -170,7 +170,6 @@ public function dataProviderTestThatGroupsActionReturnExpected(): Generator
*/
public function dataProviderTestThatGroupsActionReturnExpectedWithValidApiKey(): Generator
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

foreach ($rolesService->getRoles() as $role) {
Expand Down
1 change: 0 additions & 1 deletion tests/E2E/Controller/Profile/IndexControllerTest.php
Expand Up @@ -155,7 +155,6 @@ public function dataProviderTestThatGetTokenReturnsJwtWithValidCredentials(): Ge
*/
public function dataProviderTestThatProfileActionReturnsExpected(): Generator
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

foreach ($rolesService->getRoles() as $role) {
Expand Down
1 change: 0 additions & 1 deletion tests/E2E/Controller/Profile/RolesControllerTest.php
Expand Up @@ -158,7 +158,6 @@ public function dataProviderTestThatRolesActionReturnsExpected(): Generator
*/
public function dataProviderTestThatRolesActionReturnsExpectedWithValidApiKey(): Generator
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

foreach ($rolesService->getRoles() as $role) {
Expand Down
2 changes: 0 additions & 2 deletions tests/E2E/Controller/User/UserRolesControllerTest.php
Expand Up @@ -127,7 +127,6 @@ public function dataProviderTestThatGetRolesActionsReturns403ForInvalidUser(): G
*/
public function dataProviderTestThatGetUserRolesReturns200ForUserHimself(): Generator
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

yield [
Expand Down Expand Up @@ -180,7 +179,6 @@ public function dataProviderTestThatGetUserRolesReturns200ForUserHimself(): Gene
*/
public function dataProviderTestThatGetRolesReturns200ForRootRoleUser(): Generator
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

yield [LoadUserData::$uuids['john'], '[]'];
Expand Down
1 change: 0 additions & 1 deletion tests/E2E/Controller/VersionControllerTest.php
Expand Up @@ -40,7 +40,6 @@ public function testThatVersionRouteReturns200(): void
*/
public function testThatVersionRouteDoesNotMakeRequestLog(): void
{
/** @var LogRequestResource $resource */
$resource = static::getContainer()->get(LogRequestResource::class);

$expectedLogCount = $resource->count();
Expand Down
24 changes: 1 addition & 23 deletions tests/E2E/Rest/ResourceLifeCycleTest.php
Expand Up @@ -14,7 +14,6 @@
use App\Utils\Tests\WebTestCase;
use Generator;
use Throwable;
use UnexpectedValueException;
use function sprintf;

/**
Expand All @@ -25,27 +24,6 @@
*/
class ResourceLifeCycleTest extends WebTestCase
{
private ?RoleRepository $repository = null;

/**
* @throws Throwable
*/
protected function setUp(): void
{
parent::setUp();

static::bootKernel();

$testResource = static::$kernel->getContainer()->get(ResourceForLifeCycleTests::class);

/** @psalm-suppress TypeDoesNotContainType */
if (!($testResource instanceof ResourceForLifeCycleTests)) {
throw new UnexpectedValueException('Invalid resource class');
}

$this->repository = $testResource->getRepository();
}

/**
* @dataProvider dataProviderTestThatModifiedEntityIsNotFlushedIfLifeCycleMethodThrowsAnException
*
Expand Down Expand Up @@ -80,6 +58,6 @@ public function dataProviderTestThatModifiedEntityIsNotFlushedIfLifeCycleMethodT

private function getRepository(): RoleRepository
{
return $this->repository ?? throw new UnexpectedValueException('Invalid resource class');
return static::getContainer()->get(ResourceForLifeCycleTests::class)->getRepository();
}
}
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Throwable;
use function assert;
use function iterator_to_array;

/**
Expand All @@ -32,18 +31,6 @@
*/
class LoggedInUserValueResolverTest extends KernelTestCase
{
private ?UserRepository $repository = null;

protected function setUp(): void
{
parent::setUp();

/** @var UserRepository $repository */
$repository = static::getContainer()->get(UserRepository::class);

$this->repository = $repository;
}

/**
* @dataProvider dataProviderValidUsers
*
Expand Down Expand Up @@ -126,8 +113,6 @@ public function dataProviderValidUsers(): Generator

private function getRepository(): UserRepository
{
assert($this->repository instanceof UserRepository);

return $this->repository;
return static::getContainer()->get(UserRepository::class);
}
}
22 changes: 3 additions & 19 deletions tests/Functional/Repository/HealthzRepositoryTest.php
Expand Up @@ -14,7 +14,6 @@
use Exception;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Throwable;
use function assert;

/**
* Class HealthzRepositoryTest
Expand All @@ -24,8 +23,6 @@
*/
class HealthzRepositoryTest extends KernelTestCase
{
private ?HealthzRepository $repository = null;

/**
* @throws Throwable
*/
Expand All @@ -40,24 +37,13 @@ public static function tearDownAfterClass(): void
parent::tearDownAfterClass();
}

/**
* @throws Throwable
*/
protected function setUp(): void
{
parent::setUp();

/** @var HealthzRepository $repository */
$repository = static::getContainer()->get(HealthzRepository::class);

$this->repository = $repository;
}

/**
* @throws Throwable
*/
public function testThatReadValueMethodReturnsExpectedWithEmptyDatabase(): void
{
static::bootKernel();

PhpUnitUtil::loadFixtures(static::$kernel);

static::assertNull($this->getRepository()->read());
Expand Down Expand Up @@ -96,8 +82,6 @@ public function testThatCleanupMethodClearsDatabaseReturnsExpected(): void

private function getRepository(): HealthzRepository
{
assert($this->repository instanceof HealthzRepository);

return $this->repository;
return static::getContainer()->get(HealthzRepository::class);
}
}
5 changes: 1 addition & 4 deletions tests/Functional/Repository/LogRequestRepositoryTest.php
Expand Up @@ -25,9 +25,6 @@ class LogRequestRepositoryTest extends KernelTestCase
*/
public function testThatCleanHistoryReturnsExpected(): void
{
/** @var LogRequestRepository $repository */
$repository = static::getContainer()->get(LogRequestRepository::class);

static::assertSame(0, $repository->cleanHistory());
static::assertSame(0, static::getContainer()->get(LogRequestRepository::class)->cleanHistory());
}
}
1 change: 0 additions & 1 deletion tests/Functional/Repository/RoleRepositoryTest.php
Expand Up @@ -40,7 +40,6 @@ public static function tearDownAfterClass(): void
*/
public function testThatResetMethodDeletesAllRecords(): void
{
/** @var RoleRepository $repository */
$repository = static::getContainer()->get(RoleRepository::class);

static::assertSame(5, $repository->countAdvanced());
Expand Down
11 changes: 7 additions & 4 deletions tests/Functional/Repository/UserRepositoryTest.php
Expand Up @@ -128,11 +128,14 @@ public function testThatResetMethodDeletesAllRecords(): void

private function getRepository(): UserRepository
{
static::bootKernel();
static $cache;

if ($cache === null) {
static::bootKernel();

/** @var UserRepository $repository */
$repository = static::getContainer()->get(UserRepository::class);
$cache = static::getContainer()->get(UserRepository::class);
}

return $repository;
return $cache;
}
}
Expand Up @@ -141,7 +141,6 @@ public function testThatSupportsClassReturnsExpected(bool $expected, string $cla
*/
public function dataProviderTestThatGetApiKeyReturnsExpected(): array
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

$iterator = static fn (string $role): array => [$rolesService->getShort($role)];
Expand Down
10 changes: 2 additions & 8 deletions tests/Functional/Security/Provider/SecurityUserFactoryTest.php
Expand Up @@ -129,17 +129,11 @@ public function dataProviderTestThatLoadUserByIdentifierReturnsExpectedUserInsta

private function getSecurityUserFactory(): SecurityUserFactory
{
/** @var SecurityUserFactory $securityUserFactory */
$securityUserFactory = static::getContainer()->get(SecurityUserFactory::class);

return $securityUserFactory;
return static::getContainer()->get(SecurityUserFactory::class);
}

private function getUserRepository(): UserRepository
{
/** @var UserRepository $userRepository */
$userRepository = static::getContainer()->get(UserRepository::class);

return $userRepository;
return static::getContainer()->get(UserRepository::class);
}
}
8 changes: 2 additions & 6 deletions tests/Integration/Entity/ApiKeyTest.php
Expand Up @@ -45,7 +45,7 @@ public function testThatGetRolesContainsExpectedRole(): void
* @dataProvider dataProviderTestThatApiKeyHasExpectedRoles
*
* @phpstan-param StringableArrayObject<array<int, string>> $expectedRoles
* @phpstan-param StringableArrayObject<array<mixed>> $criteria
* @phpstan-param StringableArrayObject<array> $criteria
* @psalm-param StringableArrayObject $expectedRoles
* @psalm-param StringableArrayObject $criteria
*
Expand All @@ -55,10 +55,7 @@ public function testThatApiKeyHasExpectedRoles(
StringableArrayObject $expectedRoles,
StringableArrayObject $criteria
): void {
/** @var ApiKeyRepository $repository */
$repository = static::getContainer()->get(ApiKeyRepository::class);

$apiKey = $repository->findOneBy($criteria->getArrayCopy());
$apiKey = static::getContainer()->get(ApiKeyRepository::class)->findOneBy($criteria->getArrayCopy());

static::assertInstanceOf(ApiKey::class, $apiKey);
static::assertSame($expectedRoles->getArrayCopy(), $apiKey->getRoles());
Expand All @@ -69,7 +66,6 @@ public function testThatApiKeyHasExpectedRoles(
*/
public function dataProviderTestThatApiKeyHasExpectedRoles(): Generator
{
/** @var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

foreach ($rolesService->getRoles() as $role) {
Expand Down

0 comments on commit 5c333d8

Please sign in to comment.