Skip to content

Commit

Permalink
Fix psalm errors (wip)
Browse files Browse the repository at this point in the history
Signed-off-by: tux-rampage <tuxrampage@gmail.com>
  • Loading branch information
tux-rampage committed Nov 4, 2020
1 parent b244beb commit d04a7cf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
36 changes: 22 additions & 14 deletions test/CodeGenerator/AbstractInjectorTest.php
Expand Up @@ -33,10 +33,10 @@ class AbstractInjectorTest extends TestCase
{
use ProphecyTrait;

/** @var InjectorInterface|ObjectProphecy */
/** @var ObjectProphecy<InjectorInterface> */
private $decoratedInjectorProphecy;

/** @var ContainerInterface|ObjectProphecy */
/** @var ObjectProphecy<ContainerInterface> */
private $containerProphecy;

protected function setUp(): void
Expand All @@ -47,15 +47,22 @@ protected function setUp(): void
parent::setUp();
}

/**
* @param callable():array<string, class-string<FactoryInterface>|FactoryInterface> $factoriesProvider
*/
public function createTestSubject(callable $factoriesProvider, bool $withContainer = true): AbstractInjector
{
$injector = $this->decoratedInjectorProphecy->reveal();
$container = $withContainer ? $this->containerProphecy->reveal() : null;

return new class ($factoriesProvider, $injector, $container) extends AbstractInjector
{
/** @var callable():array<string, class-string<FactoryInterface>|FactoryInterface> */
private $provider;

/**
* @param callable():array<string, class-string<FactoryInterface>|FactoryInterface> $provider
*/
public function __construct(
callable $provider,
InjectorInterface $injector,
Expand All @@ -72,7 +79,7 @@ protected function loadFactoryList(): void
};
}

public function testImplementsContract()
public function testImplementsContract(): void
{
$prophecy = $this->prophesize(InvokableInterface::class);
$prophecy->__invoke()
Expand All @@ -85,10 +92,11 @@ public function testImplementsContract()
$this->assertInstanceOf(InjectorInterface::class, $subject);
}

public function testCanCreateReturnsTrueWhenAFactoryIsAvailable()
public function testCanCreateReturnsTrueWhenAFactoryIsAvailable(): void
{
$className = uniqid('SomeClass');
$provider = function () use ($className) {
$provider = function () use ($className): array {
/** @psalm-var array<string, class-string<FactoryInterface>|FactoryInterface> */
return [$className => 'SomeClassFactory'];
};

Expand All @@ -100,11 +108,11 @@ public function testCanCreateReturnsTrueWhenAFactoryIsAvailable()
$this->assertTrue($subject->canCreate($className));
}

public function testCanCreateUsesDecoratedInjectorWithoutFactory()
public function testCanCreateUsesDecoratedInjectorWithoutFactory(): void
{
$missingClass = uniqid('SomeClass');
$existingClass = uniqid('SomeOtherClass');
$provider = function () {
$provider = function (): array {
return [];
};

Expand All @@ -124,13 +132,13 @@ public function testCanCreateUsesDecoratedInjectorWithoutFactory()
$this->assertFalse($subject->canCreate($missingClass));
}

public function testCreateUsesFactory()
public function testCreateUsesFactory(): void
{
$factory = $this->prophesize(FactoryInterface::class);
$className = uniqid('SomeClass');
$params = ['someArg' => uniqid()];
$expected = new stdClass();
$provider = function () use ($className, $factory) {
$provider = function () use ($className, $factory): array {
return [$className => $factory->reveal()];
};

Expand All @@ -150,12 +158,12 @@ public function testCreateUsesFactory()
$this->assertSame($expected, $subject->create($className, $params));
}

public function testCreateUsesDecoratedInjectorIfNoFactoryIsAvailable()
public function testCreateUsesDecoratedInjectorIfNoFactoryIsAvailable(): void
{
$className = uniqid('SomeClass');
$expected = new stdClass();
$params = ['someArg' => uniqid()];
$provider = function () {
$provider = function (): array {
return [];
};

Expand All @@ -168,12 +176,12 @@ public function testCreateUsesDecoratedInjectorIfNoFactoryIsAvailable()
$this->assertSame($expected, $subject->create($className, $params));
}

public function testConstructionWithoutContainerUsesDefaultContainer()
public function testConstructionWithoutContainerUsesDefaultContainer(): void
{
$factory = $this->prophesize(FactoryInterface::class);
$className = uniqid('SomeClass');
$expected = new stdClass();
$provider = function () use ($className, $factory) {
$provider = function () use ($className, $factory): array {
return [$className => $factory->reveal()];
};

Expand All @@ -185,7 +193,7 @@ public function testConstructionWithoutContainerUsesDefaultContainer()
$this->assertSame($expected, $subject->create($className));
}

public function testFactoryIsCreatedFromClassNameString()
public function testFactoryIsCreatedFromClassNameString(): void
{
$subject = $this->createTestSubject(function () {
return ['SomeClass' => StdClassFactory::class];
Expand Down
10 changes: 5 additions & 5 deletions test/CodeGenerator/FactoryGeneratorTest.php
Expand Up @@ -25,10 +25,10 @@
class FactoryGeneratorTest extends TestCase
{
use GeneratorTestTrait;

private const DEFAULT_NAMESPACE = 'LaminasTest\Di\Generated\Factory';

public function testGenerateCreatesFiles()
public function testGenerateCreatesFiles(): void
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
Expand All @@ -40,7 +40,7 @@ public function testGenerateCreatesFiles()
$this->assertFileExists($this->dir . '/Factory/LaminasTest/Di/TestAsset/RequiresAFactory.php');
}

public function testGenerateBuildsUpClassMap()
public function testGenerateBuildsUpClassMap(): void
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
Expand All @@ -59,7 +59,7 @@ public function testGenerateBuildsUpClassMap()
$this->assertEquals($expected, $generator->getClassmap());
}

public function testGenerateForClassWithoutParams()
public function testGenerateForClassWithoutParams(): void
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
Expand All @@ -74,7 +74,7 @@ public function testGenerateForClassWithoutParams()
);
}

public function testGenerateForClassWithParams()
public function testGenerateForClassWithParams(): void
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
Expand Down
4 changes: 2 additions & 2 deletions test/CodeGenerator/InjectorGeneratorTest.php
Expand Up @@ -98,7 +98,7 @@ public function testSetCustomNamespace(): void
$this->assertEquals($expected, $generator->getNamespace());
}

public function testGeneratorLogsDebugForEachClass()
public function testGeneratorLogsDebugForEachClass(): void
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
Expand All @@ -113,7 +113,7 @@ public function testGeneratorLogsDebugForEachClass()
$logger->debug(Argument::containingString(TestAsset\B::class))->shouldHaveBeenCalled();
}

public function testGeneratorLogsErrorWhenFactoryGenerationFailed()
public function testGeneratorLogsErrorWhenFactoryGenerationFailed(): void
{
$config = new Config();
$resolver = new DependencyResolver(new RuntimeDefinition(), $config);
Expand Down
17 changes: 11 additions & 6 deletions test/LegacyConfigTest.php
Expand Up @@ -19,13 +19,15 @@
use SplFileInfo;
use stdClass;

use function is_array;

/**
* @coversDefaultClass Laminas\Di\LegacyConfig
*/
class LegacyConfigTest extends TestCase
{
/**
* @return array<string, array{0: array}>
* @return array<string, array{0: array, 1: array}>
*/
public function provideMigrationConfigFixtures(): array
{
Expand All @@ -35,6 +37,8 @@ public function provideMigrationConfigFixtures(): array
/** @var SplFileInfo $file */
foreach ($iterator as $file) {
$key = $file->getBasename('.php');

/** @var array{config: array, expected: array} $data */
$data = include $file->getPathname();

$values[$key] = [
Expand All @@ -49,15 +53,15 @@ public function provideMigrationConfigFixtures(): array
/**
* @dataProvider provideMigrationConfigFixtures
*/
public function testLegacyConfigMigration(array $config, array $expected)
public function testLegacyConfigMigration(array $config, array $expected): void
{
$instance = new LegacyConfig($config);
$this->assertEquals($expected, $instance->toArray());
}

public function testFQParamNamesTriggerDeprecated()
public function testFQParamNamesTriggerDeprecated(): void
{
$this->expectDeprecation(DeprecatedError::class);
$this->expectDeprecation();

new LegacyConfig([
'instance' => [
Expand All @@ -70,16 +74,17 @@ public function testFQParamNamesTriggerDeprecated()
]);
}

public function testConstructWithTraversable()
public function testConstructWithTraversable(): void
{
/** @var array{config: array, expected: array} $spec */
$spec = include __DIR__ . '/_files/legacy-configs/common.php';
$config = new ArrayIterator($spec['config']);
$instance = new LegacyConfig($config);

$this->assertEquals($spec['expected'], $instance->toArray());
}

public function testConstructWithInvalidConfigThrowsException()
public function testConstructWithInvalidConfigThrowsException(): void
{
$this->expectException(Exception\InvalidArgumentException::class);
new LegacyConfig(new stdClass());
Expand Down
2 changes: 1 addition & 1 deletion test/ModuleTest.php
Expand Up @@ -19,7 +19,7 @@
*/
class ModuleTest extends TestCase
{
public function testModuleProvidesServiceConfiguration()
public function testModuleProvidesServiceConfiguration(): void
{
$module = new Module();
$configProvider = new ConfigProvider();
Expand Down

0 comments on commit d04a7cf

Please sign in to comment.