Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip magic methods in Activity classes #572

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Internal/Declaration/Reader/ActivityReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ private function getMethodGroups(ClassNode $graph, \ReflectionMethod $root): arr
continue;
}

if ($attribute === null && $this->isMagic($method)) {
continue;
}

//
// The name of the activity must be generated based on the
// optional prefix on the #[ActivityInterface] attribute and
Expand Down
10 changes: 10 additions & 0 deletions src/Internal/Declaration/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
abstract class Reader
{
private const MAGIC_METHODS = [
'__construct', '__destruct', '__call', '__callStatic', '__get', '__set', '__isset', '__unset', '__sleep',
'__wakeup', '__serialize', '__unserialize', '__toString', '__invoke', '__set_state', '__clone', '__debugInfo',
];

protected ReaderInterface $reader;

public function __construct(ReaderInterface $reader)
Expand All @@ -35,4 +40,9 @@ protected function isValidMethod(\ReflectionMethod $method): bool
{
return !$method->isStatic() && $method->isPublic();
}

protected function isMagic(\ReflectionMethod $method): bool
{
return \in_array($method->getName(), self::MAGIC_METHODS, true);
}
}
37 changes: 22 additions & 15 deletions tests/Unit/Activity/ActivityPrototypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
use Spiral\Attributes\Composite\SelectiveReader;
use Temporal\Internal\Declaration\Reader\ActivityReader;
use Temporal\Tests\Unit\AbstractUnit;
use WeakReference;

final class ActivityPrototypeTestCase extends AbstractUnit
{
private ActivityReader $activityReader;

protected function setUp(): void
public function testNoMagicMethods(): void
{
$this->activityReader = new ActivityReader(new SelectiveReader([new AnnotationReader(), new AttributeReader()]));
parent::setUp();
$protos = $this->activityReader->fromClass(MagicActivity::class);

self::assertCount(1, $protos);
self::assertSame('MagicActivity.Do', $protos[0]->getID());
}

public function testInstanceLeaks(): void
Expand All @@ -27,11 +28,11 @@ public function testInstanceLeaks(): void
$proto = $this->activityReader
->fromClass(DummyActivity::class)[0];

$refProto = WeakReference::create($proto);
$refInstance = WeakReference::create($proto->getInstance());
$refHandler = WeakReference::create($proto->getHandler());
$refInstanceHandler = WeakReference::create($proto->getInstance()->getHandler());
$refActivity = WeakReference::create($proto->getInstance()->getContext());
$refProto = \WeakReference::create($proto);
$refInstance = \WeakReference::create($proto->getInstance());
$refHandler = \WeakReference::create($proto->getHandler());
$refInstanceHandler = \WeakReference::create($proto->getInstance()->getHandler());
$refActivity = \WeakReference::create($proto->getInstance()->getContext());

unset($proto, $instance);

Expand All @@ -49,8 +50,8 @@ public function testProtoWithInstanceImmutabilityAndLeaks(): void
->fromClass(DummyActivity::class)[0];
$newProto = $proto->withInstance($instance);
// References
$refProto = WeakReference::create($proto);
$refNewProto = WeakReference::create($newProto);
$refProto = \WeakReference::create($proto);
$refNewProto = \WeakReference::create($newProto);

// New object is result of clone operation
$this->assertNotSame($proto, $newProto);
Expand Down Expand Up @@ -80,7 +81,7 @@ public function testGetInstanceFromClass(): void
public function testGetInstanceFromFactory(): void
{
$proto = $this->activityReader->fromClass(DummyActivity::class)[0];
$protoWithFactory = $proto->withFactory(fn () => new DummyActivity());
$protoWithFactory = $proto->withFactory(static fn() => new DummyActivity());

$this->assertInstanceOf(DummyActivity::class, $protoWithFactory->getInstance()->getContext());
}
Expand All @@ -97,7 +98,7 @@ public function testLocalActivityFlag(): void
public function testFactoryCreatesNewInstances(): void
{
$proto = $this->activityReader->fromClass(DummyActivity::class)[0];
$protoWithFactory = $proto->withFactory(fn () => new DummyActivity());
$protoWithFactory = $proto->withFactory(static fn() => new DummyActivity());

$this->assertEquals($protoWithFactory->getInstance()->getContext(), $protoWithFactory->getInstance()->getContext());
$this->assertNotSame($protoWithFactory->getInstance()->getContext(), $protoWithFactory->getInstance()->getContext());
Expand All @@ -106,7 +107,7 @@ public function testFactoryCreatesNewInstances(): void
public function testFactoryAcceptsReflectionClassOfActivity(): void
{
$proto = $this->activityReader->fromClass(DummyActivity::class)[0];
$protoWithFactory = $proto->withFactory(fn (\ReflectionClass $reflectionClass) => $reflectionClass->newInstance());
$protoWithFactory = $proto->withFactory(static fn(\ReflectionClass $reflectionClass) => $reflectionClass->newInstance());

$this->assertEquals($protoWithFactory->getInstance()->getContext(), $protoWithFactory->getInstance()->getContext());
$this->assertNotSame($protoWithFactory->getInstance()->getContext(), $protoWithFactory->getInstance()->getContext());
Expand All @@ -115,9 +116,15 @@ public function testFactoryAcceptsReflectionClassOfActivity(): void
public function testGetFactory(): void
{
$proto = $this->activityReader->fromClass(DummyActivity::class)[0];
$protoWithFactory = $proto->withFactory(fn (\ReflectionClass $reflectionClass) => $reflectionClass->newInstance());
$protoWithFactory = $proto->withFactory(static fn(\ReflectionClass $reflectionClass) => $reflectionClass->newInstance());

$this->assertNull($proto->getFactory());
$this->assertNotNull($protoWithFactory->getFactory());
}

protected function setUp(): void
{
$this->activityReader = new ActivityReader(new SelectiveReader([new AnnotationReader(), new AttributeReader()]));
parent::setUp();
}
}
4 changes: 1 addition & 3 deletions tests/Unit/Activity/DummyActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@
final class DummyActivity
{
#[ActivityMethod(name: "DoNothing")]
public function doNothing(): void
{
}
public function doNothing(): void {}
}
19 changes: 19 additions & 0 deletions tests/Unit/Activity/MagicActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Unit\Activity;

use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;

#[ActivityInterface(prefix: 'MagicActivity.')]
final class MagicActivity
{
public function __construct() {}

#[ActivityMethod(name: "Do")]
public function __invoke(): void {}

public function __destruct() {}
}
66 changes: 32 additions & 34 deletions tests/Unit/Router/InvokeActivityTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Temporal\Tests\Unit\Router;

use React\Promise\Deferred;
use RuntimeException;
use Spiral\Attributes\AnnotationReader;
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Composite\SelectiveReader;
Expand Down Expand Up @@ -34,6 +33,38 @@ final class InvokeActivityTestCase extends AbstractUnit
private InvokeActivity $router;
private ActivityContext $activityContext;

public function testFinalizerIsCalledOnSuccessActivityInvocation(): void
{
$finalizerWasCalled = false;
$this->services->activities->addFinalizer(
static function () use (&$finalizerWasCalled): void {
$finalizerWasCalled = true;
},
);

$this->activityContext->getInfo()->type->name = 'DummyActivityDoNothing';
$request = new Request('DummyActivityDoNothing', EncodedValues::fromValues([]));
$this->router->handle($request, [], new Deferred());
$this->assertTrue($finalizerWasCalled);
}

public function testFinalizerIsCalledOnFailedActivityInvocation(): void
{
$finalizerWasCalled = false;
$this->services->activities->addFinalizer(
function (\Throwable $error) use (&$finalizerWasCalled): void {
$finalizerWasCalled = true;
$this->assertInstanceOf(\RuntimeException::class, $error);
$this->assertSame('Failed', $error->getMessage());
},
);

$this->activityContext->getInfo()->type->name = 'DummyActivityDoFail';
$request = new Request('DummyActivityDoFail', EncodedValues::fromValues([]));
$this->router->handle($request, [], new Deferred());
$this->assertTrue($finalizerWasCalled);
}

protected function setUp(): void
{
$rpc = $this->createMock(RPCConnectionInterface::class);
Expand Down Expand Up @@ -70,37 +101,4 @@ protected function setUp(): void

parent::setUp();
}


public function testFinalizerIsCalledOnSuccessActivityInvocation(): void
{
$finalizerWasCalled = false;
$this->services->activities->addFinalizer(
function () use (&$finalizerWasCalled) {
$finalizerWasCalled = true;
}
);

$this->activityContext->getInfo()->type->name = 'DummyActivityDoNothing';
$request = new Request('DummyActivityDoNothing', EncodedValues::fromValues([]));
$this->router->handle($request, [], new Deferred());
$this->assertTrue($finalizerWasCalled);
}

public function testFinalizerIsCalledOnFailedActivityInvocation(): void
{
$finalizerWasCalled = false;
$this->services->activities->addFinalizer(
function (\Throwable $error) use (&$finalizerWasCalled) {
$finalizerWasCalled = true;
$this->assertInstanceOf(RuntimeException::class, $error);
$this->assertSame('Failed', $error->getMessage());
}
);

$this->activityContext->getInfo()->type->name = 'DummyActivityDoFail';
$request = new Request('DummyActivityDoFail', EncodedValues::fromValues([]));
$this->router->handle($request, [], new Deferred());
$this->assertTrue($finalizerWasCalled);
}
}
Loading