Skip to content

Commit

Permalink
Merge pull request #1002 from spiral/bugfix/fix-reactor-tests
Browse files Browse the repository at this point in the history
[spiral/reactor] Fix Psalm issues and tests in Reactor
  • Loading branch information
butschster authored Oct 17, 2023
2 parents 4376d23 + 0e95b00 commit 0936229
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/Reactor/src/AbstractDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

/**
* Generic element declaration.
*
* @template T of ClassLike
*/
abstract class AbstractDeclaration implements DeclarationInterface, NamedInterface, \Stringable
{
use Traits\CommentAware;
use Traits\NameAware;
use Traits\AttributeAware;

/**
* @var T
*/
protected ClassLike $element;

public function __toString(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Reactor/src/ClassDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Spiral\Reactor\Partial\TraitUse;
use Spiral\Reactor\Traits;

/**
* @extends AbstractDeclaration<ClassType>
*/
class ClassDeclaration extends AbstractDeclaration implements AggregableInterface
{
use Traits\ConstantsAware;
Expand Down
3 changes: 3 additions & 0 deletions src/Reactor/src/EnumDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Spiral\Reactor\Partial\TraitUse;
use Spiral\Reactor\Traits;

/**
* @extends AbstractDeclaration<EnumType>
*/
class EnumDeclaration extends AbstractDeclaration implements AggregableInterface
{
use Traits\ConstantsAware;
Expand Down
3 changes: 3 additions & 0 deletions src/Reactor/src/InterfaceDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Spiral\Reactor\Partial\Method;
use Spiral\Reactor\Traits;

/**
* @extends AbstractDeclaration<InterfaceType>
*/
class InterfaceDeclaration extends AbstractDeclaration implements AggregableInterface
{
use Traits\ConstantsAware;
Expand Down
3 changes: 3 additions & 0 deletions src/Reactor/src/Partial/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Spiral\Reactor\NamedInterface;
use Spiral\Reactor\Traits;

/**
* @property NetteParameter $element
*/
class Parameter implements NamedInterface, AggregableInterface
{
use Traits\AttributeAware;
Expand Down
3 changes: 3 additions & 0 deletions src/Reactor/src/Partial/PromotedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Nette\PhpGenerator\PromotedParameter as NettePromotedParameter;
use Spiral\Reactor\Traits;

/**
* @property NettePromotedParameter $element
*/
final class PromotedParameter extends Parameter
{
use Traits\CommentAware;
Expand Down
3 changes: 3 additions & 0 deletions src/Reactor/src/TraitDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Spiral\Reactor\Partial\TraitUse;
use Spiral\Reactor\Traits;

/**
* @extends AbstractDeclaration<TraitType>
*/
class TraitDeclaration extends AbstractDeclaration implements AggregableInterface
{
use Traits\ConstantsAware;
Expand Down
4 changes: 2 additions & 2 deletions src/Reactor/src/Traits/TraitsAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function getTrait(string $name): TraitUse
return $this->getTraits()->get($name);
}

public function addTrait(string $name, array|bool|null $deprecatedParam = null): TraitUse
public function addTrait(string $name): TraitUse
{
return TraitUse::fromElement($this->element->addTrait($name, $deprecatedParam));
return TraitUse::fromElement($this->element->addTrait($name));
}

public function removeTrait(string $name): static
Expand Down
1 change: 1 addition & 0 deletions src/Reactor/tests/FileDeclarationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function testAddUse(): void
{
$file = new FileDeclaration();
$file->addUse('Foo\\Bar');
$file->addClass('Test')->addImplement('Foo\\Bar');

$this->assertStringContainsString('use Foo\\Bar;', (string) $file);
}
Expand Down
22 changes: 15 additions & 7 deletions src/Reactor/tests/Partial/PhpNamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ public function testAddUse(): void

$this->assertEmpty($namespace->getUses());

$namespace->addUse('foo');
$namespace->addUse('bar', 'baz');
$this->assertSame(['baz' => 'bar', 'foo' => 'foo'], $namespace->getUses());
$this->assertStringContainsString('use bar as baz;', $namespace->__toString());
$this->assertStringContainsString('use foo;', $namespace->__toString());
$namespace->addUse('Some\\Other');
$namespace->addUse('Other\\Some', 'Baz');
$namespace->addClass('Test')->setExtends('Some\\Other')->addImplement('Baz');

$namespace->removeUse('bar');
$this->assertSame(['foo' => 'foo'], $namespace->getUses());
$this->assertSame(['Baz' => 'Other\\Some', 'Other' => 'Some\\Other'], $namespace->getUses());

$this->assertStringContainsString('use Other\\Some as Baz;', $namespace->__toString());
$this->assertStringContainsString('use Some\\Other;', $namespace->__toString());

$namespace->removeUse('Other\\Some');
$this->assertSame(['Other' => 'Some\\Other'], $namespace->getUses());
}

public function testUseFunction(): void
Expand All @@ -62,6 +65,8 @@ public function testUseFunction(): void

$namespace->addUseFunction('foo');
$namespace->addUseFunction('bar', 'baz');
$namespace->addClass('Test')->addMethod('test')->addBody('foo();baz();');

$this->assertSame(['baz' => 'bar', 'foo' => 'foo'], $namespace->getUses(NettePhpNamespace::NameFunction));
$this->assertStringContainsString('use function bar as baz;', $namespace->__toString());
$this->assertStringContainsString('use function foo;', $namespace->__toString());
Expand All @@ -78,6 +83,8 @@ public function testUseConstant(): void

$namespace->addUseConstant('foo');
$namespace->addUseConstant('bar', 'baz');
$namespace->addClass('Test')->addMethod('test')->addBody('foo::some;baz::some;');

$this->assertSame(['baz' => 'bar', 'foo' => 'foo'], $namespace->getUses(NettePhpNamespace::NameConstant));
$this->assertStringContainsString('use const bar as baz;', $namespace->__toString());
$this->assertStringContainsString('use const foo;', $namespace->__toString());
Expand Down Expand Up @@ -175,6 +182,7 @@ public function testAddEnum(): void
public function testRender(): void
{
$namespace = new PhpNamespace('Foo\\Bar');
$namespace->addClass('Test');

$this->assertStringContainsString('namespace Foo\\Bar;', $namespace->__toString());
}
Expand Down

0 comments on commit 0936229

Please sign in to comment.