Skip to content

Commit

Permalink
Fix phpstan errors for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Mar 22, 2021
1 parent 9e415f6 commit 4e769b1
Show file tree
Hide file tree
Showing 77 changed files with 457 additions and 275 deletions.
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@
"matthiasnoback/symfony-config-test": "^4.2",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12.29",
"phpstan/phpstan-symfony": "^0.12.10",
"phpstan/phpstan": "^0.12.82",
"phpstan/phpstan-phpunit": "^0.12.18",
"phpstan/phpstan-symfony": "^0.12.21",
"psalm/plugin-symfony": "^2.0",
"psr/event-dispatcher": "^1.0",
"sonata-project/intl-bundle": "^2.4",
Expand Down Expand Up @@ -107,5 +108,13 @@
"psr-4": {
"Sonata\\AdminBundle\\Tests\\": "tests/"
}
},
"scripts": {
"post-install-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/simple-phpunit install"
],
"post-update-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/simple-phpunit install"
]
}
}
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
parameters:
level: 5
bootstrapFiles:
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
paths:
- src
- tests
excludes_analyse:
# temporarily ignore template files
- src/Resources/**.tpl.php
Expand Down
2 changes: 0 additions & 2 deletions src/Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,6 @@ public function setSubClasses(array $subClasses): void;

/**
* Returns true if the admin has the sub classes.
*
* @phpstan-param class-string<T> $name
*/
public function hasSubClass(string $name): bool;

Expand Down
8 changes: 8 additions & 0 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ final class Pool
*/
private $adminClasses = [];

/**
* @param string[] $adminServices
* @param array<string, array<string, mixed>> $adminGroups
* @param array<class-string, string[]> $adminClasses
*
* @phpstan-param array<string, array<string, mixed>> $adminGroups
* @psalm-param array<string, Group> $adminGroups
*/
public function __construct(
ContainerInterface $container,
array $adminServices = [],
Expand Down
2 changes: 1 addition & 1 deletion src/FieldDescription/FieldDescriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function hasAdmin(): bool;
/**
* Merge option values related to the provided option name.
*
* @param array<string, mixed> $options
* @param mixed[] $options
*
* @throws \RuntimeException
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function setLabel($label): void;
*/
public function getDefaultOptions(): array;

/**
* @return array<string, mixed>
*/
public function getOptions(): array;

/**
* @param mixed $default
*
Expand All @@ -80,7 +85,7 @@ public function initialize(string $name, array $options = []): void;
public function getFieldName(): string;

/**
* @return array<string, string> array of mappings
* @return array<array<string, mixed>> array of mappings
*/
public function getParentAssociationMappings(): array;

Expand Down
4 changes: 2 additions & 2 deletions src/Form/DataTransformer/ModelToIdPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public function reverseTransform($value)
}

/**
* @param object|object[]|null $value
* @param object|object[]|\ArrayAccess<string|int, object>|null $value
*
* @throws \InvalidArgumentException
*
* @return array<string|int, int|string|array<string>>
*
* @phpstan-param T|T[]|null $value
* @phpstan-param T|T[]|\ArrayAccess<string|int, T>|null $value
*/
public function transform($value)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Route/RouteCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ interface RouteCollectionInterface
* @param array<string, mixed> $defaults
* @param array<string, string> $requirements
* @param array<string, mixed> $options
* @param array<string, string> $schemes
* @param array<string, string> $methods
* @param string[] $schemes
* @param string[] $methods
*
* @return static
*/
Expand Down
14 changes: 7 additions & 7 deletions tests/Action/AppendFormFieldElementActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\AppendFormFieldElementAction;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Admin\AdminHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
Expand All @@ -37,34 +37,34 @@ final class AppendFormFieldElementActionTest extends TestCase
private $pool;

/**
* @var Environment
* @var Environment&MockObject
*/
private $twig;

/**
* @var GetShortObjectDescriptionAction
* @var AppendFormFieldElementAction
*/
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

/**
* @var AdminHelper
* @var AdminHelper&MockObject
*/
private $helper;

protected function setUp(): void
{
$this->twig = $this->createStub(Environment::class);
$this->twig = $this->createMock(Environment::class);
$this->admin = $this->createMock(AdminInterface::class);
$this->admin->expects($this->once())->method('setRequest');
$container = new Container();
$container->set('sonata.post.admin', $this->admin);
$this->pool = new Pool($container, ['sonata.post.admin']);
$this->helper = $this->createStub(AdminHelper::class);
$this->helper = $this->createMock(AdminHelper::class);
$this->action = new AppendFormFieldElementAction(
$this->twig,
$this->pool,
Expand Down
3 changes: 2 additions & 1 deletion tests/Action/DashboardActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\DashboardAction;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
Expand All @@ -26,7 +27,7 @@
class DashboardActionTest extends TestCase
{
/**
* @var MutableTemplateRegistryInterface
* @var MutableTemplateRegistryInterface&Stub
*/
private $templateRegistry;

Expand Down
3 changes: 2 additions & 1 deletion tests/Action/GetShortObjectDescriptionActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand Down Expand Up @@ -42,7 +43,7 @@ final class GetShortObjectDescriptionActionTest extends TestCase
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

Expand Down
11 changes: 5 additions & 6 deletions tests/Action/RetrieveAutocompleteItemsActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
Expand All @@ -39,12 +38,12 @@ final class RetrieveAutocompleteItemsActionTest extends TestCase
private $pool;

/**
* @var GetShortObjectDescriptionAction
* @var RetrieveAutocompleteItemsAction
*/
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

Expand Down Expand Up @@ -230,12 +229,12 @@ public function testRetrieveAutocompleteItemsComplexProperty(): void
$this->assertSame('{"status":"OK","more":false,"items":[{"id":"123","label":"FOO"}]}', $response->getContent());
}

private function configureAutocompleteItemsDatagrid(): Stub
private function configureAutocompleteItemsDatagrid(): MockObject
{
$model = new \stdClass();

$targetAdmin = $this->createMock(AdminInterface::class);
$datagrid = $this->createStub(DatagridInterface::class);
$datagrid = $this->createMock(DatagridInterface::class);
$metadata = $this->createStub(MetadataInterface::class);
$pager = $this->createStub(PagerInterface::class);
$fieldDescription = $this->createStub(FieldDescriptionInterface::class);
Expand Down
14 changes: 7 additions & 7 deletions tests/Action/RetrieveFormFieldElementActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction;
use Sonata\AdminBundle\Action\RetrieveFormFieldElementAction;
use Sonata\AdminBundle\Admin\AdminHelper;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand All @@ -37,34 +37,34 @@ final class RetrieveFormFieldElementActionTest extends TestCase
private $pool;

/**
* @var GetShortObjectDescriptionAction
* @var RetrieveFormFieldElementAction
*/
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

/**
* @var Environment
* @var Environment&MockObject
*/
private $twig;

/**
* @var AdminHelper
* @var AdminHelper&MockObject
*/
private $helper;

protected function setUp(): void
{
$this->twig = $this->createStub(Environment::class);
$this->twig = $this->createMock(Environment::class);
$this->admin = $this->createMock(AdminInterface::class);
$this->admin->expects($this->once())->method('setRequest');
$container = new Container();
$container->set('sonata.post.admin', $this->admin);
$this->pool = new Pool($container, ['sonata.post.admin']);
$this->helper = $this->createStub(AdminHelper::class);
$this->helper = $this->createMock(AdminHelper::class);
$this->action = new RetrieveFormFieldElementAction(
$this->twig,
$this->pool,
Expand Down
5 changes: 3 additions & 2 deletions tests/Action/SearchActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\SearchAction;
Expand Down Expand Up @@ -50,7 +51,7 @@ final class SearchActionTest extends TestCase
private $action;

/**
* @var Stub&Environment
* @var MockObject&Environment
*/
private $twig;

Expand All @@ -70,7 +71,7 @@ protected function setUp(): void

$this->breadcrumbsBuilder = $this->createStub(BreadcrumbsBuilderInterface::class);
$this->searchHandler = new SearchHandler(true);
$this->twig = $this->createStub(Environment::class);
$this->twig = $this->createMock(Environment::class);

$this->action = new SearchAction(
$this->pool,
Expand Down
11 changes: 6 additions & 5 deletions tests/Action/SetObjectFieldValueActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\SetObjectFieldValueAction;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand Down Expand Up @@ -52,17 +53,17 @@ final class SetObjectFieldValueActionTest extends TestCase
private $action;

/**
* @var AdminInterface
* @var AdminInterface&MockObject
*/
private $admin;

/**
* @var ValidatorInterface
* @var ValidatorInterface&MockObject
*/
private $validator;

/**
* @var ModelManagerInterface
* @var ModelManagerInterface&MockObject
*/
private $modelManager;

Expand Down Expand Up @@ -93,8 +94,8 @@ protected function setUp(): void
$container->set($this->adminCode, $this->admin);
$this->pool = new Pool($container, [$this->adminCode]);
$this->admin->expects($this->once())->method('setRequest');
$this->validator = $this->createStub(ValidatorInterface::class);
$this->modelManager = $this->createStub(ModelManagerInterface::class);
$this->validator = $this->createMock(ValidatorInterface::class);
$this->modelManager = $this->createMock(ModelManagerInterface::class);
$this->resolver = new DataTransformerResolver();
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
$this->action = new SetObjectFieldValueAction(
Expand Down
2 changes: 1 addition & 1 deletion tests/Admin/AdminHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function testAppendFormFieldElementWithoutFormFieldDescriptionInAdminAndN

$this->expectException(\TypeError::class);
$this->expectExceptionMessage(sprintf('Collection must be an instance of %s, "%s" given.', Collection::class, \gettype(null)));
$this->helper->appendFormFieldElement($admin, $foo, 'test_bar')[1];
$this->helper->appendFormFieldElement($admin, $foo, 'test_bar');
}

public function testAppendFormFieldElementWithCollection(): void
Expand Down
Loading

0 comments on commit 4e769b1

Please sign in to comment.